mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-17 11:38:51 +01:00
use dependency injection for conversation viewmodel
This commit is contained in:
@@ -2,11 +2,19 @@ package com.moez.QKSMS
|
||||
|
||||
import android.app.Application
|
||||
import com.bugsnag.android.Bugsnag
|
||||
import com.moez.QKSMS.dagger.AppComponent
|
||||
import com.moez.QKSMS.dagger.AppModule
|
||||
import com.moez.QKSMS.dagger.ConversationModule
|
||||
import com.moez.QKSMS.dagger.DaggerAppComponent
|
||||
import io.realm.Realm
|
||||
import io.realm.RealmConfiguration
|
||||
|
||||
|
||||
class QKApplication : Application() {
|
||||
|
||||
var appComponent: AppComponent? = null
|
||||
private set
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
|
||||
@@ -16,6 +24,11 @@ class QKApplication : Application() {
|
||||
Realm.setDefaultConfiguration(RealmConfiguration.Builder()
|
||||
.deleteRealmIfMigrationNeeded()
|
||||
.build())
|
||||
|
||||
appComponent = DaggerAppComponent.builder()
|
||||
.appModule(AppModule(this))
|
||||
.conversationModule(ConversationModule())
|
||||
.build()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,13 @@
|
||||
package com.moez.QKSMS.dagger
|
||||
|
||||
/**
|
||||
* Created by moez on 2017-08-28.
|
||||
*/
|
||||
import com.moez.QKSMS.ui.conversations.ConversationListFragment
|
||||
import dagger.Component
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
@Component(modules = arrayOf(AppModule::class, ConversationModule::class))
|
||||
interface AppComponent {
|
||||
|
||||
fun inject(fragment: ConversationListFragment)
|
||||
|
||||
}
|
||||
@@ -1,5 +1,18 @@
|
||||
package com.moez.QKSMS.dagger
|
||||
|
||||
/**
|
||||
* Created by moez on 2017-08-28.
|
||||
*/
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
class AppModule(var application: Application) {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideContext(): Context {
|
||||
return application
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,24 @@
|
||||
package com.moez.QKSMS.dagger
|
||||
|
||||
/**
|
||||
* Created by moez on 2017-08-28.
|
||||
*/
|
||||
import com.moez.QKSMS.data.repository.ConversationRepository
|
||||
import com.moez.QKSMS.ui.conversations.ConversationListViewModel
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
class ConversationModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideRepository(): ConversationRepository {
|
||||
return ConversationRepository()
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideViewModel(repository: ConversationRepository): ConversationListViewModel {
|
||||
return ConversationListViewModel(repository)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.moez.QKSMS.ui.base
|
||||
|
||||
import android.arch.lifecycle.LifecycleFragment
|
||||
import com.moez.QKSMS.QKApplication
|
||||
import com.moez.QKSMS.dagger.AppComponent
|
||||
|
||||
abstract class QkFragment : LifecycleFragment() {
|
||||
|
||||
@@ -8,4 +10,8 @@ abstract class QkFragment : LifecycleFragment() {
|
||||
return super.getContext() as QkActivity
|
||||
}
|
||||
|
||||
fun getAppComponent(): AppComponent? {
|
||||
return (context.application as QKApplication).appComponent
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,18 +8,19 @@ import android.widget.ProgressBar
|
||||
import com.moez.QKSMS.R
|
||||
import com.moez.QKSMS.data.sync.SyncManager
|
||||
import com.moez.QKSMS.ui.base.QkFragment
|
||||
import javax.inject.Inject
|
||||
|
||||
class ConversationListFragment : QkFragment() {
|
||||
|
||||
@Inject lateinit var viewModel: ConversationListViewModel
|
||||
|
||||
private var progress: ProgressBar? = null
|
||||
private var conversationList: RecyclerView? = null
|
||||
private var viewModel: ConversationListViewModel? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
getAppComponent()?.inject(this)
|
||||
setHasOptionsMenu(true)
|
||||
|
||||
viewModel = ConversationListViewModel()
|
||||
}
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
@@ -29,7 +30,7 @@ class ConversationListFragment : QkFragment() {
|
||||
|
||||
conversationList = view.findViewById(R.id.conversation_list)
|
||||
conversationList?.layoutManager = LinearLayoutManager(context)
|
||||
conversationList?.adapter = ConversationAdapter(context, viewModel?.conversations)
|
||||
conversationList?.adapter = ConversationAdapter(context, viewModel.conversations)
|
||||
|
||||
return view
|
||||
}
|
||||
|
||||
@@ -3,10 +3,8 @@ package com.moez.QKSMS.ui.conversations
|
||||
import android.arch.lifecycle.ViewModel
|
||||
import com.moez.QKSMS.data.repository.ConversationRepository
|
||||
|
||||
class ConversationListViewModel : ViewModel() {
|
||||
class ConversationListViewModel(repository: ConversationRepository) : ViewModel() {
|
||||
|
||||
private val repository = ConversationRepository()
|
||||
|
||||
var conversations = repository.getConversations()
|
||||
val conversations = repository.getConversations()
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user