2018-06-18 13:26:18 +02:00
|
|
|
// from https://proandroiddev.com/viewmodel-with-dagger2-architecture-components-2e06f06c9455
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky.di
|
|
|
|
|
2018-12-17 15:25:35 +01:00
|
|
|
import androidx.lifecycle.ViewModel
|
|
|
|
import androidx.lifecycle.ViewModelProvider
|
2022-01-11 19:55:17 +01:00
|
|
|
import com.keylesspalace.tusky.components.account.AccountViewModel
|
2022-09-02 16:52:47 +02:00
|
|
|
import com.keylesspalace.tusky.components.account.media.AccountMediaViewModel
|
2020-11-18 21:12:27 +01:00
|
|
|
import com.keylesspalace.tusky.components.announcements.AnnouncementsViewModel
|
2019-12-19 19:09:40 +01:00
|
|
|
import com.keylesspalace.tusky.components.compose.ComposeViewModel
|
2019-02-12 19:22:37 +01:00
|
|
|
import com.keylesspalace.tusky.components.conversation.ConversationsViewModel
|
2021-01-21 18:57:09 +01:00
|
|
|
import com.keylesspalace.tusky.components.drafts.DraftsViewModel
|
2022-11-05 17:37:20 +01:00
|
|
|
import com.keylesspalace.tusky.components.login.LoginWebViewViewModel
|
2019-06-09 16:55:34 +02:00
|
|
|
import com.keylesspalace.tusky.components.report.ReportViewModel
|
2022-03-20 20:21:42 +01:00
|
|
|
import com.keylesspalace.tusky.components.scheduled.ScheduledStatusViewModel
|
2019-07-19 20:10:20 +02:00
|
|
|
import com.keylesspalace.tusky.components.search.SearchViewModel
|
2022-01-11 19:00:29 +01:00
|
|
|
import com.keylesspalace.tusky.components.timeline.viewmodel.CachedTimelineViewModel
|
|
|
|
import com.keylesspalace.tusky.components.timeline.viewmodel.NetworkTimelineViewModel
|
2022-08-15 11:00:18 +02:00
|
|
|
import com.keylesspalace.tusky.components.viewthread.ViewThreadViewModel
|
2019-12-19 19:09:40 +01:00
|
|
|
import com.keylesspalace.tusky.viewmodel.AccountsInListViewModel
|
|
|
|
import com.keylesspalace.tusky.viewmodel.EditProfileViewModel
|
2019-03-16 13:36:16 +01:00
|
|
|
import com.keylesspalace.tusky.viewmodel.ListsViewModel
|
2018-06-18 13:26:18 +02:00
|
|
|
import dagger.Binds
|
|
|
|
import dagger.MapKey
|
|
|
|
import dagger.Module
|
|
|
|
import dagger.multibindings.IntoMap
|
|
|
|
import javax.inject.Inject
|
|
|
|
import javax.inject.Provider
|
|
|
|
import javax.inject.Singleton
|
|
|
|
import kotlin.reflect.KClass
|
|
|
|
|
|
|
|
@Singleton
|
|
|
|
class ViewModelFactory @Inject constructor(private val viewModels: MutableMap<Class<out ViewModel>, Provider<ViewModel>>) : ViewModelProvider.Factory {
|
2018-08-15 20:47:09 +02:00
|
|
|
@Suppress("UNCHECKED_CAST")
|
2018-06-18 13:26:18 +02:00
|
|
|
override fun <T : ViewModel> create(modelClass: Class<T>): T = viewModels[modelClass]?.get() as T
|
|
|
|
}
|
|
|
|
|
|
|
|
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
|
|
|
|
@kotlin.annotation.Retention(AnnotationRetention.RUNTIME)
|
|
|
|
@MapKey
|
|
|
|
internal annotation class ViewModelKey(val value: KClass<out ViewModel>)
|
|
|
|
|
|
|
|
@Module
|
|
|
|
abstract class ViewModelModule {
|
|
|
|
|
|
|
|
@Binds
|
|
|
|
internal abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory
|
|
|
|
|
|
|
|
@Binds
|
|
|
|
@IntoMap
|
|
|
|
@ViewModelKey(AccountViewModel::class)
|
|
|
|
internal abstract fun accountViewModel(viewModel: AccountViewModel): ViewModel
|
|
|
|
|
2018-08-15 20:47:09 +02:00
|
|
|
@Binds
|
|
|
|
@IntoMap
|
|
|
|
@ViewModelKey(EditProfileViewModel::class)
|
|
|
|
internal abstract fun editProfileViewModel(viewModel: EditProfileViewModel): ViewModel
|
|
|
|
|
2019-02-12 19:22:37 +01:00
|
|
|
@Binds
|
|
|
|
@IntoMap
|
|
|
|
@ViewModelKey(ConversationsViewModel::class)
|
|
|
|
internal abstract fun conversationsViewModel(viewModel: ConversationsViewModel): ViewModel
|
|
|
|
|
2019-03-16 13:36:16 +01:00
|
|
|
@Binds
|
|
|
|
@IntoMap
|
|
|
|
@ViewModelKey(ListsViewModel::class)
|
|
|
|
internal abstract fun listsViewModel(viewModel: ListsViewModel): ViewModel
|
|
|
|
|
|
|
|
@Binds
|
|
|
|
@IntoMap
|
|
|
|
@ViewModelKey(AccountsInListViewModel::class)
|
|
|
|
internal abstract fun accountsInListViewModel(viewModel: AccountsInListViewModel): ViewModel
|
|
|
|
|
2019-06-09 16:55:34 +02:00
|
|
|
@Binds
|
|
|
|
@IntoMap
|
|
|
|
@ViewModelKey(ReportViewModel::class)
|
|
|
|
internal abstract fun reportViewModel(viewModel: ReportViewModel): ViewModel
|
|
|
|
|
2019-07-19 20:10:20 +02:00
|
|
|
@Binds
|
|
|
|
@IntoMap
|
|
|
|
@ViewModelKey(SearchViewModel::class)
|
|
|
|
internal abstract fun searchViewModel(viewModel: SearchViewModel): ViewModel
|
|
|
|
|
2019-12-19 19:09:40 +01:00
|
|
|
@Binds
|
|
|
|
@IntoMap
|
|
|
|
@ViewModelKey(ComposeViewModel::class)
|
|
|
|
internal abstract fun composeViewModel(viewModel: ComposeViewModel): ViewModel
|
|
|
|
|
2019-12-30 20:40:27 +01:00
|
|
|
@Binds
|
|
|
|
@IntoMap
|
2022-03-20 20:21:42 +01:00
|
|
|
@ViewModelKey(ScheduledStatusViewModel::class)
|
|
|
|
internal abstract fun scheduledStatusViewModel(viewModel: ScheduledStatusViewModel): ViewModel
|
2019-12-30 20:40:27 +01:00
|
|
|
|
2020-11-18 21:12:27 +01:00
|
|
|
@Binds
|
|
|
|
@IntoMap
|
|
|
|
@ViewModelKey(AnnouncementsViewModel::class)
|
|
|
|
internal abstract fun announcementsViewModel(viewModel: AnnouncementsViewModel): ViewModel
|
|
|
|
|
2021-01-21 18:57:09 +01:00
|
|
|
@Binds
|
|
|
|
@IntoMap
|
|
|
|
@ViewModelKey(DraftsViewModel::class)
|
|
|
|
internal abstract fun draftsViewModel(viewModel: DraftsViewModel): ViewModel
|
|
|
|
|
2021-06-11 20:15:40 +02:00
|
|
|
@Binds
|
|
|
|
@IntoMap
|
2022-01-11 19:00:29 +01:00
|
|
|
@ViewModelKey(CachedTimelineViewModel::class)
|
|
|
|
internal abstract fun cachedTimelineViewModel(viewModel: CachedTimelineViewModel): ViewModel
|
|
|
|
|
|
|
|
@Binds
|
|
|
|
@IntoMap
|
|
|
|
@ViewModelKey(NetworkTimelineViewModel::class)
|
|
|
|
internal abstract fun networkTimelineViewModel(viewModel: NetworkTimelineViewModel): ViewModel
|
2021-06-11 20:15:40 +02:00
|
|
|
|
2022-08-15 11:00:18 +02:00
|
|
|
@Binds
|
|
|
|
@IntoMap
|
|
|
|
@ViewModelKey(ViewThreadViewModel::class)
|
|
|
|
internal abstract fun viewThreadViewModel(viewModel: ViewThreadViewModel): ViewModel
|
2022-09-02 16:52:47 +02:00
|
|
|
|
|
|
|
@Binds
|
|
|
|
@IntoMap
|
|
|
|
@ViewModelKey(AccountMediaViewModel::class)
|
|
|
|
internal abstract fun accountMediaViewModel(viewModel: AccountMediaViewModel): ViewModel
|
2022-11-05 17:37:20 +01:00
|
|
|
|
|
|
|
@Binds
|
|
|
|
@IntoMap
|
|
|
|
@ViewModelKey(LoginWebViewViewModel::class)
|
|
|
|
internal abstract fun loginWebViewViewModel(viewModel: LoginWebViewViewModel): ViewModel
|
|
|
|
|
2021-06-28 21:13:24 +02:00
|
|
|
// Add more ViewModels here
|
2020-11-18 21:12:27 +01:00
|
|
|
}
|