migrate from mosby mvp to viewmodel + repository

This commit is contained in:
Moez Bhatti
2017-08-27 17:58:50 -07:00
parent edba4eadbe
commit a7222a220f
18 changed files with 87 additions and 116 deletions
@@ -0,0 +1,41 @@
package com.moez.QKSMS.data.model
import android.database.Cursor
import com.moez.QKSMS.data.sync.ConversationColumns
import io.reactivex.Flowable
import io.realm.RealmList
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
open class Conversation() : RealmObject() {
@PrimaryKey var id: Long = 0
var date: Long = 0
var messageCount: Int = 0
var recipients: RealmList<Recipient> = RealmList()
var snippet: String = ""
var snippetCs: String = ""
var read: Int = 0
var error: Int = 0
var hasAttachment: Int = 0
/**
* Instantiates a Conversation from the Android SMS ContentProvider
*/
constructor(cursor: Cursor) : this() {
id = cursor.getLong(ConversationColumns.ID)
date = cursor.getLong(ConversationColumns.DATE)
messageCount = cursor.getInt(ConversationColumns.MESSAGE_COUNT)
Flowable.fromIterable(cursor.getString(ConversationColumns.RECIPIENT_IDS).split(" "))
.map { id -> id.toLong() }
.map { id -> Recipient(id) }
.blockingSubscribe { recipient -> recipients.add(recipient) }
snippet = cursor.getString(ConversationColumns.SNIPPET) ?: ""
snippetCs = cursor.getString(ConversationColumns.SNIPPET_CS) ?: ""
read = cursor.getInt(ConversationColumns.READ)
error = cursor.getInt(ConversationColumns.ERROR)
hasAttachment = cursor.getInt(ConversationColumns.HAS_ATTACHMENT)
}
}
@@ -0,0 +1,73 @@
package com.moez.QKSMS.data.model
import android.database.Cursor
import android.provider.Telephony
import android.provider.Telephony.Mms
import android.provider.Telephony.Sms
import com.moez.QKSMS.data.sync.MessageColumns
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
open class Message() : RealmObject() {
@PrimaryKey var id: Long = 0
var threadId: Long = 0
var boxId: Int = 0
var type: String = ""
var body: String = ""
var date: Long = 0
var dateSent: Long = 0
var errorType: Int = 0
constructor(threadId: Long, cursor: Cursor, columnsMap: MessageColumns) : this() {
this.threadId = threadId
id = cursor.getLong(columnsMap.msgId)
type = cursor.getString(columnsMap.msgType)
val isMms = type == "mms"
body = cursor.getString(columnsMap.smsBody) ?: ""
errorType = cursor.getInt(columnsMap.mmsErrorType)
if (isMms) {
boxId = cursor.getInt(columnsMap.mmsMessageBox)
date = cursor.getLong(columnsMap.smsDate)
dateSent = cursor.getLong(columnsMap.smsDateSent)
} else {
boxId = cursor.getInt(columnsMap.smsType)
date = cursor.getLong(columnsMap.mmsDate)
dateSent = cursor.getLong(columnsMap.mmsDateSent)
}
}
fun isMms(): Boolean = type == "mms"
fun isSms(): Boolean = type == "sms"
fun isMe(): Boolean {
val isIncomingMms = isMms() && (boxId == Mms.MESSAGE_BOX_INBOX || boxId == Mms.MESSAGE_BOX_ALL)
val isIncomingSms = isSms() && (boxId == Sms.MESSAGE_TYPE_INBOX || boxId == Sms.MESSAGE_TYPE_ALL)
return !(isIncomingMms || isIncomingSms)
}
fun isOutgoingMessage(): Boolean {
val isOutgoingMms = isMms() && boxId == Mms.MESSAGE_BOX_OUTBOX
val isOutgoingSms = isSms() && (boxId == Sms.MESSAGE_TYPE_FAILED
|| boxId == Sms.MESSAGE_TYPE_OUTBOX
|| boxId == Sms.MESSAGE_TYPE_QUEUED)
return isOutgoingMms || isOutgoingSms
}
fun isSending(): Boolean {
return !isFailedMessage() && isOutgoingMessage()
}
fun isFailedMessage(): Boolean {
val isFailedMms = isMms() && errorType >= Telephony.MmsSms.ERR_TYPE_GENERIC_PERMANENT
val isFailedSms = isSms() && boxId == Sms.MESSAGE_TYPE_FAILED
return isFailedMms || isFailedSms
}
}
@@ -0,0 +1,17 @@
package com.moez.QKSMS.data.model
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
open class Recipient() : RealmObject() {
@PrimaryKey var id: Long = 0
/**
* For inflating the recipient using just the id
*/
constructor(id: Long) : this() {
this.id = id
}
}