Migrate from Koin to Dagger/Hilt
This commit is contained in:
@@ -1,15 +0,0 @@
|
||||
package org.koitharu.kotatsu.widget
|
||||
|
||||
import androidx.room.InvalidationTracker
|
||||
import org.koin.android.ext.koin.androidContext
|
||||
import org.koin.androidx.viewmodel.dsl.viewModel
|
||||
import org.koin.dsl.module
|
||||
import org.koitharu.kotatsu.widget.shelf.ShelfConfigViewModel
|
||||
|
||||
val appWidgetModule
|
||||
get() = module {
|
||||
|
||||
single<InvalidationTracker.Observer> { WidgetUpdater(androidContext()) }
|
||||
|
||||
viewModel { ShelfConfigViewModel(get()) }
|
||||
}
|
||||
@@ -5,13 +5,17 @@ import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import androidx.room.InvalidationTracker
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
import org.koitharu.kotatsu.core.db.TABLE_FAVOURITES
|
||||
import org.koitharu.kotatsu.core.db.TABLE_HISTORY
|
||||
import org.koitharu.kotatsu.widget.recent.RecentWidgetProvider
|
||||
import org.koitharu.kotatsu.widget.shelf.ShelfWidgetProvider
|
||||
|
||||
class WidgetUpdater(
|
||||
private val context: Context
|
||||
@Singleton
|
||||
class WidgetUpdater @Inject constructor(
|
||||
@ApplicationContext private val context: Context,
|
||||
) : InvalidationTracker.Observer(TABLE_HISTORY, TABLE_FAVOURITES) {
|
||||
|
||||
override fun onInvalidated(tables: MutableSet<String>) {
|
||||
|
||||
@@ -2,11 +2,21 @@ package org.koitharu.kotatsu.widget.recent
|
||||
|
||||
import android.content.Intent
|
||||
import android.widget.RemoteViewsService
|
||||
import org.koin.android.ext.android.get
|
||||
import coil.ImageLoader
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import org.koitharu.kotatsu.history.domain.HistoryRepository
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
class RecentWidgetService : RemoteViewsService() {
|
||||
|
||||
@Inject
|
||||
lateinit var historyRepository: HistoryRepository
|
||||
|
||||
@Inject
|
||||
lateinit var coil: ImageLoader
|
||||
|
||||
override fun onGetViewFactory(intent: Intent): RemoteViewsFactory {
|
||||
return RecentListFactory(applicationContext, get(), get())
|
||||
return RecentListFactory(applicationContext, historyRepository, coil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,13 @@ import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.activity.viewModels
|
||||
import androidx.core.graphics.Insets
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.core.view.updateLayoutParams
|
||||
import androidx.core.view.updatePadding
|
||||
import com.google.android.material.R as materialR
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import org.koin.androidx.viewmodel.ext.android.viewModel
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.base.ui.BaseActivity
|
||||
import org.koitharu.kotatsu.base.ui.list.OnListItemClickListener
|
||||
@@ -20,12 +21,13 @@ import org.koitharu.kotatsu.databinding.ActivityCategoriesBinding
|
||||
import org.koitharu.kotatsu.utils.ext.getDisplayMessage
|
||||
import org.koitharu.kotatsu.widget.shelf.adapter.CategorySelectAdapter
|
||||
import org.koitharu.kotatsu.widget.shelf.model.CategoryItem
|
||||
import com.google.android.material.R as materialR
|
||||
|
||||
class ShelfConfigActivity : BaseActivity<ActivityCategoriesBinding>(),
|
||||
OnListItemClickListener<CategoryItem>, View.OnClickListener {
|
||||
class ShelfConfigActivity :
|
||||
BaseActivity<ActivityCategoriesBinding>(),
|
||||
OnListItemClickListener<CategoryItem>,
|
||||
View.OnClickListener {
|
||||
|
||||
private val viewModel by viewModel<ShelfConfigViewModel>()
|
||||
private val viewModel by viewModels<ShelfConfigViewModel>()
|
||||
|
||||
private lateinit var adapter: CategorySelectAdapter
|
||||
private lateinit var config: AppWidgetConfig
|
||||
@@ -44,7 +46,7 @@ class ShelfConfigActivity : BaseActivity<ActivityCategoriesBinding>(),
|
||||
binding.fabAdd.hide()
|
||||
val appWidgetId = intent?.getIntExtra(
|
||||
AppWidgetManager.EXTRA_APPWIDGET_ID,
|
||||
AppWidgetManager.INVALID_APPWIDGET_ID
|
||||
AppWidgetManager.INVALID_APPWIDGET_ID,
|
||||
) ?: AppWidgetManager.INVALID_APPWIDGET_ID
|
||||
if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
|
||||
finishAfterTransition()
|
||||
@@ -64,7 +66,7 @@ class ShelfConfigActivity : BaseActivity<ActivityCategoriesBinding>(),
|
||||
updateWidget()
|
||||
setResult(
|
||||
Activity.RESULT_OK,
|
||||
Intent().putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, config.widgetId)
|
||||
Intent().putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, config.widgetId),
|
||||
)
|
||||
finish()
|
||||
}
|
||||
@@ -84,12 +86,12 @@ class ShelfConfigActivity : BaseActivity<ActivityCategoriesBinding>(),
|
||||
binding.recyclerView.updatePadding(
|
||||
left = insets.left,
|
||||
right = insets.right,
|
||||
bottom = insets.bottom
|
||||
bottom = insets.bottom,
|
||||
)
|
||||
with(binding.toolbar) {
|
||||
updatePadding(
|
||||
left = insets.left,
|
||||
right = insets.right
|
||||
right = insets.right,
|
||||
)
|
||||
updateLayoutParams<ViewGroup.MarginLayoutParams> {
|
||||
topMargin = insets.top
|
||||
|
||||
@@ -2,6 +2,8 @@ package org.koitharu.kotatsu.widget.shelf
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
@@ -10,15 +12,16 @@ import org.koitharu.kotatsu.favourites.domain.FavouritesRepository
|
||||
import org.koitharu.kotatsu.utils.ext.asLiveDataDistinct
|
||||
import org.koitharu.kotatsu.widget.shelf.model.CategoryItem
|
||||
|
||||
class ShelfConfigViewModel(
|
||||
favouritesRepository: FavouritesRepository
|
||||
@HiltViewModel
|
||||
class ShelfConfigViewModel @Inject constructor(
|
||||
favouritesRepository: FavouritesRepository,
|
||||
) : BaseViewModel() {
|
||||
|
||||
private val selectedCategoryId = MutableStateFlow(0L)
|
||||
|
||||
val content: LiveData<List<CategoryItem>> = combine(
|
||||
favouritesRepository.observeCategories(),
|
||||
selectedCategoryId
|
||||
selectedCategoryId,
|
||||
) { categories, selectedId ->
|
||||
val list = ArrayList<CategoryItem>(categories.size + 1)
|
||||
list += CategoryItem(0L, null, selectedId == 0L)
|
||||
@@ -29,4 +32,4 @@ class ShelfConfigViewModel(
|
||||
}.asLiveDataDistinct(viewModelScope.coroutineContext + Dispatchers.Default, emptyList())
|
||||
|
||||
var checkedId: Long by selectedCategoryId::value
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,15 +3,25 @@ package org.koitharu.kotatsu.widget.shelf
|
||||
import android.appwidget.AppWidgetManager
|
||||
import android.content.Intent
|
||||
import android.widget.RemoteViewsService
|
||||
import org.koin.android.ext.android.get
|
||||
import coil.ImageLoader
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import javax.inject.Inject
|
||||
import org.koitharu.kotatsu.favourites.domain.FavouritesRepository
|
||||
|
||||
@AndroidEntryPoint
|
||||
class ShelfWidgetService : RemoteViewsService() {
|
||||
|
||||
@Inject
|
||||
lateinit var favouritesRepository: FavouritesRepository
|
||||
|
||||
@Inject
|
||||
lateinit var coil: ImageLoader
|
||||
|
||||
override fun onGetViewFactory(intent: Intent): RemoteViewsFactory {
|
||||
val widgetId = intent.getIntExtra(
|
||||
AppWidgetManager.EXTRA_APPWIDGET_ID,
|
||||
AppWidgetManager.INVALID_APPWIDGET_ID
|
||||
AppWidgetManager.INVALID_APPWIDGET_ID,
|
||||
)
|
||||
return ShelfListFactory(applicationContext, get(), get(), widgetId)
|
||||
return ShelfListFactory(applicationContext, favouritesRepository, coil, widgetId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user