mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-22 21:58:53 +01:00
Use @Inject constructors instead of providing deps in Module
This commit is contained in:
@@ -31,7 +31,6 @@ android {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compile fileTree(include: ['*.jar'], dir: 'libs')
|
|
||||||
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
|
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
|
||||||
exclude group: 'com.android.support', module: 'support-annotations'
|
exclude group: 'com.android.support', module: 'support-annotations'
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -2,18 +2,12 @@ package com.moez.QKSMS.common.di
|
|||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import com.moez.QKSMS.common.util.DateFormatter
|
|
||||||
import com.moez.QKSMS.common.util.NotificationManager
|
|
||||||
import com.moez.QKSMS.common.util.ThemeManager
|
|
||||||
import com.moez.QKSMS.data.repository.ContactRepository
|
|
||||||
import com.moez.QKSMS.data.repository.MessageRepository
|
|
||||||
import com.moez.QKSMS.data.sync.SyncManager
|
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
import dagger.Provides
|
import dagger.Provides
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
@Module
|
@Module
|
||||||
class AppModule(var application: Application) {
|
class AppModule(private var application: Application) {
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
@@ -21,40 +15,4 @@ class AppModule(var application: Application) {
|
|||||||
return application
|
return application
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
|
||||||
@Singleton
|
|
||||||
fun provideDateFormatter(context: Context): DateFormatter {
|
|
||||||
return DateFormatter(context)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Provides
|
|
||||||
@Singleton
|
|
||||||
fun provideSyncManager(context: Context, contacts: ContactRepository): SyncManager {
|
|
||||||
return SyncManager(context, contacts)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Provides
|
|
||||||
@Singleton
|
|
||||||
fun provideThemeMaager(): ThemeManager {
|
|
||||||
return ThemeManager()
|
|
||||||
}
|
|
||||||
|
|
||||||
@Provides
|
|
||||||
@Singleton
|
|
||||||
fun provideNotificationHelper(context: Context, themeManager: ThemeManager): NotificationManager {
|
|
||||||
return NotificationManager(context, themeManager)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Singleton
|
|
||||||
@Provides
|
|
||||||
fun provideMessageRepository(context: Context, notificationManager: NotificationManager): MessageRepository {
|
|
||||||
return MessageRepository(context, notificationManager)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Provides
|
|
||||||
@Singleton
|
|
||||||
fun provideContactRepository(context: Context): ContactRepository {
|
|
||||||
return ContactRepository(context)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -7,8 +7,11 @@ import com.moez.QKSMS.common.util.extensions.isSameWeek
|
|||||||
import com.moez.QKSMS.common.util.extensions.isSameYear
|
import com.moez.QKSMS.common.util.extensions.isSameYear
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
class DateFormatter(val context: Context) {
|
@Singleton
|
||||||
|
class DateFormatter @Inject constructor(val context: Context) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replace 12 hour format with 24 hour format if necessary
|
* Replace 12 hour format with 24 hour format if necessary
|
||||||
|
|||||||
@@ -16,9 +16,13 @@ import com.moez.QKSMS.receiver.MarkReadReceiver
|
|||||||
import com.moez.QKSMS.receiver.MarkSeenReceiver
|
import com.moez.QKSMS.receiver.MarkSeenReceiver
|
||||||
import com.moez.QKSMS.receiver.RemoteMessagingReceiver
|
import com.moez.QKSMS.receiver.RemoteMessagingReceiver
|
||||||
import com.moez.QKSMS.presentation.conversations.ConversationListActivity
|
import com.moez.QKSMS.presentation.conversations.ConversationListActivity
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@Singleton
|
||||||
class NotificationManager(private val context: Context, private val themeManager: ThemeManager) {
|
class NotificationManager @Inject constructor(
|
||||||
|
private val context: Context,
|
||||||
|
private val themeManager: ThemeManager) {
|
||||||
|
|
||||||
private val notificationManager = NotificationManagerCompat.from(context)
|
private val notificationManager = NotificationManagerCompat.from(context)
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
package com.moez.QKSMS.common.util
|
package com.moez.QKSMS.common.util
|
||||||
|
|
||||||
class ThemeManager {
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
class ThemeManager @Inject constructor() {
|
||||||
|
|
||||||
val color = 0xFF008389.toInt()
|
val color = 0xFF008389.toInt()
|
||||||
val bubbleColor = 0xFFF4F4F4.toInt()
|
val bubbleColor = 0xFFF4F4F4.toInt()
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.moez.QKSMS.data.datasource
|
||||||
|
|
||||||
|
interface MessageTransaction {
|
||||||
|
|
||||||
|
fun markSent(id: Long)
|
||||||
|
|
||||||
|
fun markFailed(id: Long)
|
||||||
|
|
||||||
|
fun markSeen()
|
||||||
|
|
||||||
|
fun markSeen(threadId: Long)
|
||||||
|
|
||||||
|
fun markRead(threadId: Long)
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package com.moez.QKSMS.data.datasource.native
|
||||||
|
|
||||||
|
import android.content.ContentValues
|
||||||
|
import android.content.Context
|
||||||
|
import android.net.Uri
|
||||||
|
import android.provider.BaseColumns
|
||||||
|
import android.provider.Telephony
|
||||||
|
import com.moez.QKSMS.common.util.extensions.asFlowable
|
||||||
|
import com.moez.QKSMS.data.datasource.MessageTransaction
|
||||||
|
import io.reactivex.schedulers.Schedulers
|
||||||
|
|
||||||
|
class NativeMessageTransaction(private val context: Context) : MessageTransaction {
|
||||||
|
|
||||||
|
override fun markSent(id: Long) {
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun markFailed(id: Long) {
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun markSeen() {
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun markSeen(threadId: Long) {
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun markRead(threadId: Long) {
|
||||||
|
// Messages in SMS ContentProvider
|
||||||
|
val projection = arrayOf(BaseColumns._ID)
|
||||||
|
val selection = "${Telephony.Sms.THREAD_ID} = $threadId AND (${Telephony.Sms.SEEN} = 0 OR ${Telephony.Sms.READ} = 0)"
|
||||||
|
val contentResolver = context.contentResolver
|
||||||
|
contentResolver.query(Telephony.Sms.Inbox.CONTENT_URI, projection, selection, null, null)
|
||||||
|
.asFlowable()
|
||||||
|
.subscribeOn(Schedulers.io())
|
||||||
|
.map { cursor -> cursor.getLong(0) }
|
||||||
|
.map { id -> Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, id.toString()) }
|
||||||
|
.subscribe { uri ->
|
||||||
|
val values = ContentValues()
|
||||||
|
values.put(Telephony.Sms.SEEN, true)
|
||||||
|
values.put(Telephony.Sms.READ, true)
|
||||||
|
contentResolver.update(uri, values, null, null)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO also need to mark MMS in ContentProvider as Read
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package com.moez.QKSMS.data.datasource.realm
|
||||||
|
|
||||||
|
import com.moez.QKSMS.data.datasource.MessageTransaction
|
||||||
|
import com.moez.QKSMS.data.model.Message
|
||||||
|
import io.reactivex.schedulers.Schedulers
|
||||||
|
import io.realm.Realm
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
class RealmMessageTransaction @Inject constructor() : MessageTransaction {
|
||||||
|
|
||||||
|
override fun markSent(id: Long) {
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun markFailed(id: Long) {
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun markSeen() {
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun markSeen(threadId: Long) {
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun markRead(threadId: Long) {
|
||||||
|
Schedulers.io().scheduleDirect {
|
||||||
|
val realm = Realm.getDefaultInstance()
|
||||||
|
val messages = realm.where(Message::class.java)
|
||||||
|
.equalTo("threadId", threadId)
|
||||||
|
.beginGroup()
|
||||||
|
.equalTo("read", false)
|
||||||
|
.or()
|
||||||
|
.equalTo("seen", false)
|
||||||
|
.endGroup()
|
||||||
|
.findAll()
|
||||||
|
|
||||||
|
realm.executeTransaction {
|
||||||
|
messages.forEach { message ->
|
||||||
|
message.seen = true
|
||||||
|
message.read = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
realm.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -12,9 +12,11 @@ import io.reactivex.Maybe
|
|||||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||||
import io.reactivex.schedulers.Schedulers
|
import io.reactivex.schedulers.Schedulers
|
||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@Singleton
|
||||||
class ContactRepository(val context: Context) {
|
class ContactRepository @Inject constructor(val context: Context) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uri for the MMS-SMS recipients table, where there exists only one column (address)
|
* Uri for the MMS-SMS recipients table, where there exists only one column (address)
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ import android.telephony.SmsManager
|
|||||||
import com.moez.QKSMS.common.util.NotificationManager
|
import com.moez.QKSMS.common.util.NotificationManager
|
||||||
import com.moez.QKSMS.common.util.extensions.asFlowable
|
import com.moez.QKSMS.common.util.extensions.asFlowable
|
||||||
import com.moez.QKSMS.common.util.extensions.insertOrUpdate
|
import com.moez.QKSMS.common.util.extensions.insertOrUpdate
|
||||||
|
import com.moez.QKSMS.data.datasource.native.NativeMessageTransaction
|
||||||
|
import com.moez.QKSMS.data.datasource.realm.RealmMessageTransaction
|
||||||
import com.moez.QKSMS.data.model.Conversation
|
import com.moez.QKSMS.data.model.Conversation
|
||||||
import com.moez.QKSMS.data.model.Message
|
import com.moez.QKSMS.data.model.Message
|
||||||
import com.moez.QKSMS.data.sync.MessageColumns
|
import com.moez.QKSMS.data.sync.MessageColumns
|
||||||
@@ -22,8 +24,17 @@ import io.reactivex.schedulers.Schedulers
|
|||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
import io.realm.RealmResults
|
import io.realm.RealmResults
|
||||||
import io.realm.Sort
|
import io.realm.Sort
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
class MessageRepository(val context: Context, val notificationManager: NotificationManager) {
|
@Singleton
|
||||||
|
class MessageRepository @Inject constructor(
|
||||||
|
private val context: Context,
|
||||||
|
private val notificationManager: NotificationManager,
|
||||||
|
private val realmMessageTransaction: RealmMessageTransaction) {
|
||||||
|
|
||||||
|
// TODO figure out why injecting this in the constructor breaks Dagger
|
||||||
|
private val nativeMessageTransaction: NativeMessageTransaction = NativeMessageTransaction(context)
|
||||||
|
|
||||||
fun getConversationMessagesAsync(): RealmResults<Message> {
|
fun getConversationMessagesAsync(): RealmResults<Message> {
|
||||||
return Realm.getDefaultInstance()
|
return Realm.getDefaultInstance()
|
||||||
@@ -140,44 +151,8 @@ class MessageRepository(val context: Context, val notificationManager: Notificat
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun markRead(threadId: Long) {
|
fun markRead(threadId: Long) {
|
||||||
// Messages in SMS ContentProvider
|
nativeMessageTransaction.markRead(threadId)
|
||||||
val projection = arrayOf(BaseColumns._ID)
|
realmMessageTransaction.markRead(threadId)
|
||||||
val selection = "${Sms.THREAD_ID} = $threadId AND (${Sms.SEEN} = 0 OR ${Sms.READ} = 0)"
|
|
||||||
val contentResolver = context.contentResolver
|
|
||||||
contentResolver.query(Sms.Inbox.CONTENT_URI, projection, selection, null, null)
|
|
||||||
.asFlowable()
|
|
||||||
.subscribeOn(Schedulers.io())
|
|
||||||
.map { cursor -> cursor.getLong(0) }
|
|
||||||
.map { id -> Uri.withAppendedPath(Sms.CONTENT_URI, id.toString()) }
|
|
||||||
.subscribe { uri ->
|
|
||||||
val values = ContentValues()
|
|
||||||
values.put(Sms.SEEN, true)
|
|
||||||
values.put(Sms.READ, true)
|
|
||||||
contentResolver.update(uri, values, null, null)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO also need to mark MMS in ContentProvider as Read
|
|
||||||
|
|
||||||
// Messages in Realm
|
|
||||||
Schedulers.io().scheduleDirect {
|
|
||||||
val realm = Realm.getDefaultInstance()
|
|
||||||
val messages = realm.where(Message::class.java)
|
|
||||||
.equalTo("threadId", threadId)
|
|
||||||
.beginGroup()
|
|
||||||
.equalTo("read", false)
|
|
||||||
.or()
|
|
||||||
.equalTo("seen", false)
|
|
||||||
.endGroup()
|
|
||||||
.findAll()
|
|
||||||
|
|
||||||
realm.executeTransaction {
|
|
||||||
messages.forEach { message ->
|
|
||||||
message.seen = true
|
|
||||||
message.read = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
realm.close()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun markSent(uri: Uri) {
|
fun markSent(uri: Uri) {
|
||||||
|
|||||||
@@ -4,16 +4,19 @@ import android.content.Context
|
|||||||
import android.database.Cursor
|
import android.database.Cursor
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.provider.Telephony
|
import android.provider.Telephony
|
||||||
|
import com.moez.QKSMS.common.util.extensions.asFlowable
|
||||||
import com.moez.QKSMS.data.model.Conversation
|
import com.moez.QKSMS.data.model.Conversation
|
||||||
import com.moez.QKSMS.data.model.Message
|
import com.moez.QKSMS.data.model.Message
|
||||||
import com.moez.QKSMS.data.repository.ContactRepository
|
import com.moez.QKSMS.data.repository.ContactRepository
|
||||||
import com.moez.QKSMS.common.util.extensions.asFlowable
|
|
||||||
import io.reactivex.Flowable
|
import io.reactivex.Flowable
|
||||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||||
import io.reactivex.schedulers.Schedulers
|
import io.reactivex.schedulers.Schedulers
|
||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
class SyncManager(val context: Context, private val contactsRepo: ContactRepository) {
|
@Singleton
|
||||||
|
class SyncManager @Inject constructor(val context: Context, private val contactsRepo: ContactRepository) {
|
||||||
|
|
||||||
fun copyToRealm(completionListener: () -> Unit) {
|
fun copyToRealm(completionListener: () -> Unit) {
|
||||||
|
|
||||||
|
|||||||
-2
@@ -6,7 +6,6 @@ import com.moez.QKSMS.common.di.AppComponentManager
|
|||||||
import com.moez.QKSMS.data.model.Message
|
import com.moez.QKSMS.data.model.Message
|
||||||
import com.moez.QKSMS.data.repository.MessageRepository
|
import com.moez.QKSMS.data.repository.MessageRepository
|
||||||
import com.moez.QKSMS.data.sync.SyncManager
|
import com.moez.QKSMS.data.sync.SyncManager
|
||||||
import com.moez.QKSMS.common.util.NotificationManager
|
|
||||||
import io.reactivex.subjects.PublishSubject
|
import io.reactivex.subjects.PublishSubject
|
||||||
import io.realm.RealmResults
|
import io.realm.RealmResults
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
@@ -14,7 +13,6 @@ import javax.inject.Inject
|
|||||||
class ConversationListViewModel : ViewModel() {
|
class ConversationListViewModel : ViewModel() {
|
||||||
|
|
||||||
@Inject lateinit var syncManager: SyncManager
|
@Inject lateinit var syncManager: SyncManager
|
||||||
@Inject lateinit var notificationManager: NotificationManager
|
|
||||||
@Inject lateinit var messageRepo: MessageRepository
|
@Inject lateinit var messageRepo: MessageRepository
|
||||||
|
|
||||||
val state: MutableLiveData<ConversationListViewState> = MutableLiveData()
|
val state: MutableLiveData<ConversationListViewState> = MutableLiveData()
|
||||||
|
|||||||
@@ -6,13 +6,11 @@ import android.content.Intent
|
|||||||
import android.support.v4.app.RemoteInput
|
import android.support.v4.app.RemoteInput
|
||||||
import com.moez.QKSMS.common.di.AppComponentManager
|
import com.moez.QKSMS.common.di.AppComponentManager
|
||||||
import com.moez.QKSMS.data.repository.MessageRepository
|
import com.moez.QKSMS.data.repository.MessageRepository
|
||||||
import com.moez.QKSMS.common.util.NotificationManager
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class RemoteMessagingReceiver : BroadcastReceiver() {
|
class RemoteMessagingReceiver : BroadcastReceiver() {
|
||||||
|
|
||||||
@Inject lateinit var messageRepo: MessageRepository
|
@Inject lateinit var messageRepo: MessageRepository
|
||||||
@Inject lateinit var notificationManager: NotificationManager
|
|
||||||
|
|
||||||
override fun onReceive(context: Context, intent: Intent) {
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
AppComponentManager.appComponent.inject(this)
|
AppComponentManager.appComponent.inject(this)
|
||||||
|
|||||||
Reference in New Issue
Block a user