diff --git a/app/src/main/java/org/koitharu/kotatsu/core/backup/BackupRepository.kt b/app/src/main/java/org/koitharu/kotatsu/core/backup/BackupRepository.kt index 7212b8569..d0e60f54c 100644 --- a/app/src/main/java/org/koitharu/kotatsu/core/backup/BackupRepository.kt +++ b/app/src/main/java/org/koitharu/kotatsu/core/backup/BackupRepository.kt @@ -123,6 +123,7 @@ class BackupRepository(private val db: MangaDatabase) { jo.put("title", title) jo.put("order", order) jo.put("track", track) + jo.put("show_in_lib", isVisibleInLibrary) return jo } @@ -131,6 +132,7 @@ class BackupRepository(private val db: MangaDatabase) { jo.put("manga_id", mangaId) jo.put("category_id", categoryId) jo.put("created_at", createdAt) + jo.put("sort_key", sortKey) return jo } } \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/backup/RestoreRepository.kt b/app/src/main/java/org/koitharu/kotatsu/core/backup/RestoreRepository.kt index 4e20b955f..318d23fa6 100644 --- a/app/src/main/java/org/koitharu/kotatsu/core/backup/RestoreRepository.kt +++ b/app/src/main/java/org/koitharu/kotatsu/core/backup/RestoreRepository.kt @@ -103,11 +103,13 @@ class RestoreRepository(private val db: MangaDatabase) { title = json.getString("title"), order = json.getStringOrNull("order") ?: SortOrder.NEWEST.name, track = json.getBooleanOrDefault("track", true), + isVisibleInLibrary = json.getBooleanOrDefault("show_in_lib", true), ) private fun parseFavourite(json: JSONObject) = FavouriteEntity( mangaId = json.getLong("manga_id"), categoryId = json.getLong("category_id"), - createdAt = json.getLong("created_at") + createdAt = json.getLong("created_at"), + sortKey = json.getIntOrDefault("sort_key", 0), ) } \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/db/MangaDatabase.kt b/app/src/main/java/org/koitharu/kotatsu/core/db/MangaDatabase.kt index 82d5052aa..476dea694 100644 --- a/app/src/main/java/org/koitharu/kotatsu/core/db/MangaDatabase.kt +++ b/app/src/main/java/org/koitharu/kotatsu/core/db/MangaDatabase.kt @@ -36,7 +36,7 @@ import org.koitharu.kotatsu.tracker.data.TracksDao TrackEntity::class, TrackLogEntity::class, SuggestionEntity::class, BookmarkEntity::class, ScrobblingEntity::class, ], - version = 12, + version = 13, ) abstract class MangaDatabase : RoomDatabase() { @@ -79,6 +79,7 @@ fun MangaDatabase(context: Context): MangaDatabase = Room.databaseBuilder( Migration9To10(), Migration10To11(), Migration11To12(), + Migration12To13(), ).addCallback( DatabasePrePopulateCallback(context.resources) ).build() \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/db/migrations/Migration12To13.kt b/app/src/main/java/org/koitharu/kotatsu/core/db/migrations/Migration12To13.kt new file mode 100644 index 000000000..9f8fa723a --- /dev/null +++ b/app/src/main/java/org/koitharu/kotatsu/core/db/migrations/Migration12To13.kt @@ -0,0 +1,12 @@ +package org.koitharu.kotatsu.core.db.migrations + +import androidx.room.migration.Migration +import androidx.sqlite.db.SupportSQLiteDatabase + +class Migration12To13 : Migration(12, 13) { + + override fun migrate(database: SupportSQLiteDatabase) { + database.execSQL("ALTER TABLE favourite_categories ADD COLUMN `show_in_lib` INTEGER NOT NULL DEFAULT 1") + database.execSQL("ALTER TABLE favourites ADD COLUMN `sort_key` INTEGER NOT NULL DEFAULT 0") + } +} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/model/FavouriteCategory.kt b/app/src/main/java/org/koitharu/kotatsu/core/model/FavouriteCategory.kt index 798ec2fbd..307cb50cd 100644 --- a/app/src/main/java/org/koitharu/kotatsu/core/model/FavouriteCategory.kt +++ b/app/src/main/java/org/koitharu/kotatsu/core/model/FavouriteCategory.kt @@ -1,9 +1,9 @@ package org.koitharu.kotatsu.core.model import android.os.Parcelable -import java.util.* import kotlinx.parcelize.Parcelize import org.koitharu.kotatsu.parsers.model.SortOrder +import java.util.* @Parcelize data class FavouriteCategory( @@ -13,4 +13,5 @@ data class FavouriteCategory( val order: SortOrder, val createdAt: Date, val isTrackingEnabled: Boolean, + val isVisibleInLibrary: Boolean, ) : Parcelable \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/favourites/data/EntityMapping.kt b/app/src/main/java/org/koitharu/kotatsu/favourites/data/EntityMapping.kt index 7afa80efd..36308d4c6 100644 --- a/app/src/main/java/org/koitharu/kotatsu/favourites/data/EntityMapping.kt +++ b/app/src/main/java/org/koitharu/kotatsu/favourites/data/EntityMapping.kt @@ -12,4 +12,5 @@ fun FavouriteCategoryEntity.toFavouriteCategory(id: Long = categoryId.toLong()) order = SortOrder(order, SortOrder.NEWEST), createdAt = Date(createdAt), isTrackingEnabled = track, + isVisibleInLibrary = isVisibleInLibrary, ) \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/favourites/data/FavouriteCategoriesDao.kt b/app/src/main/java/org/koitharu/kotatsu/favourites/data/FavouriteCategoriesDao.kt index 96ca167fc..e3227561a 100644 --- a/app/src/main/java/org/koitharu/kotatsu/favourites/data/FavouriteCategoriesDao.kt +++ b/app/src/main/java/org/koitharu/kotatsu/favourites/data/FavouriteCategoriesDao.kt @@ -50,6 +50,9 @@ abstract class FavouriteCategoriesDao { @Query("UPDATE favourite_categories SET `track` = :isEnabled WHERE category_id = :id") abstract suspend fun updateTracking(id: Long, isEnabled: Boolean) + @Query("UPDATE favourite_categories SET `show_in_lib` = :isEnabled WHERE category_id = :id") + abstract suspend fun updateLibVisibility(id: Long, isEnabled: Boolean) + @Query("UPDATE favourite_categories SET sort_key = :sortKey WHERE category_id = :id") abstract suspend fun updateSortKey(id: Long, sortKey: Int) diff --git a/app/src/main/java/org/koitharu/kotatsu/favourites/data/FavouriteCategoryEntity.kt b/app/src/main/java/org/koitharu/kotatsu/favourites/data/FavouriteCategoryEntity.kt index 393a7847b..c6ff39f04 100644 --- a/app/src/main/java/org/koitharu/kotatsu/favourites/data/FavouriteCategoryEntity.kt +++ b/app/src/main/java/org/koitharu/kotatsu/favourites/data/FavouriteCategoryEntity.kt @@ -13,6 +13,7 @@ class FavouriteCategoryEntity( @ColumnInfo(name = "title") val title: String, @ColumnInfo(name = "order") val order: String, @ColumnInfo(name = "track") val track: Boolean, + @ColumnInfo(name = "show_in_lib") val isVisibleInLibrary: Boolean, ) { override fun equals(other: Any?): Boolean { @@ -27,6 +28,7 @@ class FavouriteCategoryEntity( if (title != other.title) return false if (order != other.order) return false if (track != other.track) return false + if (isVisibleInLibrary != other.isVisibleInLibrary) return false return true } @@ -38,6 +40,7 @@ class FavouriteCategoryEntity( result = 31 * result + title.hashCode() result = 31 * result + order.hashCode() result = 31 * result + track.hashCode() + result = 31 * result + isVisibleInLibrary.hashCode() return result } } \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/favourites/data/FavouriteEntity.kt b/app/src/main/java/org/koitharu/kotatsu/favourites/data/FavouriteEntity.kt index d79660a12..e2232b9b7 100644 --- a/app/src/main/java/org/koitharu/kotatsu/favourites/data/FavouriteEntity.kt +++ b/app/src/main/java/org/koitharu/kotatsu/favourites/data/FavouriteEntity.kt @@ -24,5 +24,29 @@ import org.koitharu.kotatsu.core.db.entity.MangaEntity class FavouriteEntity( @ColumnInfo(name = "manga_id", index = true) val mangaId: Long, @ColumnInfo(name = "category_id", index = true) val categoryId: Long, - @ColumnInfo(name = "created_at") val createdAt: Long -) \ No newline at end of file + @ColumnInfo(name = "sort_key") val sortKey: Int, + @ColumnInfo(name = "created_at") val createdAt: Long, +) { + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as FavouriteEntity + + if (mangaId != other.mangaId) return false + if (categoryId != other.categoryId) return false + if (sortKey != other.sortKey) return false + if (createdAt != other.createdAt) return false + + return true + } + + override fun hashCode(): Int { + var result = mangaId.hashCode() + result = 31 * result + categoryId.hashCode() + result = 31 * result + sortKey + result = 31 * result + createdAt.hashCode() + return result + } +} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/favourites/domain/FavouritesRepository.kt b/app/src/main/java/org/koitharu/kotatsu/favourites/domain/FavouritesRepository.kt index 95d027c4c..3f09d261e 100644 --- a/app/src/main/java/org/koitharu/kotatsu/favourites/domain/FavouritesRepository.kt +++ b/app/src/main/java/org/koitharu/kotatsu/favourites/domain/FavouritesRepository.kt @@ -29,11 +29,6 @@ class FavouritesRepository( .mapItems { it.manga.toManga(it.tags.toMangaTags()) } } - fun observeAllGrouped(order: SortOrder): Flow>> { - return db.favouritesDao.observeAll(order) - .map { list -> groupByCategory(list) } - } - suspend fun getManga(categoryId: Long): List { val entities = db.favouritesDao.findAll(categoryId) return entities.map { it.manga.toManga(it.tags.toMangaTags()) } @@ -89,6 +84,7 @@ class FavouritesRepository( categoryId = 0, order = sortOrder.name, track = isTrackerEnabled, + isVisibleInLibrary = true, ) val id = db.favouriteCategoriesDao.insert(entity) val category = entity.toFavouriteCategory(id) @@ -100,6 +96,10 @@ class FavouritesRepository( db.favouriteCategoriesDao.update(id, title, sortOrder.name, isTrackerEnabled) } + suspend fun updateCategory(id: Long, isVisibleInLibrary: Boolean) { + db.favouriteCategoriesDao.updateLibVisibility(id, isVisibleInLibrary) + } + suspend fun addCategory(title: String): FavouriteCategory { val entity = FavouriteCategoryEntity( title = title, @@ -108,6 +108,7 @@ class FavouritesRepository( categoryId = 0, order = SortOrder.NEWEST.name, track = true, + isVisibleInLibrary = true, ) val id = db.favouriteCategoriesDao.insert(entity) val category = entity.toFavouriteCategory(id) @@ -156,7 +157,12 @@ class FavouritesRepository( val tags = manga.tags.toEntities() db.tagsDao.upsert(tags) db.mangaDao.upsert(manga.toEntity(), tags) - val entity = FavouriteEntity(manga.id, categoryId, System.currentTimeMillis()) + val entity = FavouriteEntity( + mangaId = manga.id, + categoryId = categoryId, + createdAt = System.currentTimeMillis(), + sortKey = 0, + ) db.favouritesDao.insert(entity) } } @@ -184,16 +190,4 @@ class FavouritesRepository( .map { x -> SortOrder(x.order, SortOrder.NEWEST) } .distinctUntilChanged() } - - private fun groupByCategory(list: List): Map> { - val map = HashMap>() - for (item in list) { - val manga = item.manga.toManga(item.tags.toMangaTags()) - for (category in item.categories) { - map.getOrPut(category.toFavouriteCategory()) { ArrayList() } - .add(manga) - } - } - return map - } } \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/history/data/HistoryDao.kt b/app/src/main/java/org/koitharu/kotatsu/history/data/HistoryDao.kt index ce864a94d..77d7e431c 100644 --- a/app/src/main/java/org/koitharu/kotatsu/history/data/HistoryDao.kt +++ b/app/src/main/java/org/koitharu/kotatsu/history/data/HistoryDao.kt @@ -46,7 +46,7 @@ abstract class HistoryDao { abstract fun observeCount(): Flow @Query("SELECT percent FROM history WHERE manga_id = :id") - abstract fun findProgress(id: Long): Float? + abstract suspend fun findProgress(id: Long): Float? @Query("DELETE FROM history") abstract suspend fun clear() diff --git a/app/src/main/java/org/koitharu/kotatsu/library/LibraryModule.kt b/app/src/main/java/org/koitharu/kotatsu/library/LibraryModule.kt index 940c71198..a7dcb6be5 100644 --- a/app/src/main/java/org/koitharu/kotatsu/library/LibraryModule.kt +++ b/app/src/main/java/org/koitharu/kotatsu/library/LibraryModule.kt @@ -2,10 +2,15 @@ package org.koitharu.kotatsu.library import org.koin.androidx.viewmodel.dsl.viewModel import org.koin.dsl.module +import org.koitharu.kotatsu.library.domain.LibraryRepository import org.koitharu.kotatsu.library.ui.LibraryViewModel +import org.koitharu.kotatsu.library.ui.config.LibraryCategoriesConfigViewModel val libraryModule get() = module { + factory { LibraryRepository(get()) } + viewModel { LibraryViewModel(get(), get(), get(), get(), get()) } + viewModel { LibraryCategoriesConfigViewModel(get()) } } \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/library/domain/LibraryRepository.kt b/app/src/main/java/org/koitharu/kotatsu/library/domain/LibraryRepository.kt new file mode 100644 index 000000000..c698c36a3 --- /dev/null +++ b/app/src/main/java/org/koitharu/kotatsu/library/domain/LibraryRepository.kt @@ -0,0 +1,40 @@ +package org.koitharu.kotatsu.library.domain + +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.map +import org.koitharu.kotatsu.core.db.MangaDatabase +import org.koitharu.kotatsu.core.db.entity.toManga +import org.koitharu.kotatsu.core.db.entity.toMangaTags +import org.koitharu.kotatsu.core.model.FavouriteCategory +import org.koitharu.kotatsu.favourites.data.FavouriteManga +import org.koitharu.kotatsu.favourites.data.toFavouriteCategory +import org.koitharu.kotatsu.favourites.domain.FavouritesRepository +import org.koitharu.kotatsu.history.domain.HistoryRepository +import org.koitharu.kotatsu.parsers.model.Manga +import org.koitharu.kotatsu.parsers.model.SortOrder + +class LibraryRepository( + private val db: MangaDatabase, +) { + + + fun observeFavourites(order: SortOrder): Flow>> { + return db.favouritesDao.observeAll(order) + .map { list -> groupByCategory(list) } + } + + private fun groupByCategory(list: List): Map> { + val map = HashMap>() + for (item in list) { + val manga = item.manga.toManga(item.tags.toMangaTags()) + for (category in item.categories) { + if (!category.isVisibleInLibrary) { + continue + } + map.getOrPut(category.toFavouriteCategory()) { ArrayList() } + .add(manga) + } + } + return map + } +} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/library/ui/LibraryFragment.kt b/app/src/main/java/org/koitharu/kotatsu/library/ui/LibraryFragment.kt index 0160a3bf0..0334957ed 100644 --- a/app/src/main/java/org/koitharu/kotatsu/library/ui/LibraryFragment.kt +++ b/app/src/main/java/org/koitharu/kotatsu/library/ui/LibraryFragment.kt @@ -61,7 +61,7 @@ class LibraryFragment : BaseFragment(), LibraryListEvent ) binding.recyclerView.adapter = adapter binding.recyclerView.setHasFixedSize(true) - addMenuProvider(LibraryMenuProvider(view.context, viewModel)) + addMenuProvider(LibraryMenuProvider(view.context, childFragmentManager, viewModel)) viewModel.content.observe(viewLifecycleOwner, ::onListChanged) viewModel.onError.observe(viewLifecycleOwner, ::onError) diff --git a/app/src/main/java/org/koitharu/kotatsu/library/ui/LibraryMenuProvider.kt b/app/src/main/java/org/koitharu/kotatsu/library/ui/LibraryMenuProvider.kt index 31c832cea..0bce5a7bf 100644 --- a/app/src/main/java/org/koitharu/kotatsu/library/ui/LibraryMenuProvider.kt +++ b/app/src/main/java/org/koitharu/kotatsu/library/ui/LibraryMenuProvider.kt @@ -5,10 +5,11 @@ import android.view.Menu import android.view.MenuInflater import android.view.MenuItem import androidx.core.view.MenuProvider +import androidx.fragment.app.FragmentManager import com.google.android.material.dialog.MaterialAlertDialogBuilder import org.koitharu.kotatsu.R import org.koitharu.kotatsu.base.ui.dialog.RememberSelectionDialogListener -import org.koitharu.kotatsu.favourites.ui.categories.FavouriteCategoriesActivity +import org.koitharu.kotatsu.library.ui.config.LibraryCategoriesConfigSheet import org.koitharu.kotatsu.utils.ext.startOfDay import java.util.* import java.util.concurrent.TimeUnit @@ -16,6 +17,7 @@ import com.google.android.material.R as materialR class LibraryMenuProvider( private val context: Context, + private val fragmentManager: FragmentManager, private val viewModel: LibraryViewModel, ) : MenuProvider { @@ -30,7 +32,7 @@ class LibraryMenuProvider( true } R.id.action_categories -> { - context.startActivity(FavouriteCategoriesActivity.newIntent(context)) + LibraryCategoriesConfigSheet.show(fragmentManager) true } else -> false diff --git a/app/src/main/java/org/koitharu/kotatsu/library/ui/LibraryViewModel.kt b/app/src/main/java/org/koitharu/kotatsu/library/ui/LibraryViewModel.kt index ff1c49f45..ea7fc5c13 100644 --- a/app/src/main/java/org/koitharu/kotatsu/library/ui/LibraryViewModel.kt +++ b/app/src/main/java/org/koitharu/kotatsu/library/ui/LibraryViewModel.kt @@ -16,10 +16,10 @@ import org.koitharu.kotatsu.core.os.ShortcutsRepository import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.prefs.ListMode import org.koitharu.kotatsu.core.ui.DateTimeAgo -import org.koitharu.kotatsu.favourites.domain.FavouritesRepository import org.koitharu.kotatsu.history.domain.HistoryRepository import org.koitharu.kotatsu.history.domain.MangaWithHistory import org.koitharu.kotatsu.history.domain.PROGRESS_NONE +import org.koitharu.kotatsu.library.domain.LibraryRepository import org.koitharu.kotatsu.library.ui.model.LibrarySectionModel import org.koitharu.kotatsu.list.domain.ListExtraProvider import org.koitharu.kotatsu.list.ui.model.* @@ -34,8 +34,8 @@ import java.util.* private const val HISTORY_MAX_SEGMENTS = 2 class LibraryViewModel( + private val repository: LibraryRepository, private val historyRepository: HistoryRepository, - private val favouritesRepository: FavouritesRepository, private val shortcutsRepository: ShortcutsRepository, private val trackingRepository: TrackingRepository, private val settings: AppSettings, @@ -45,7 +45,7 @@ class LibraryViewModel( val content: LiveData> = combine( historyRepository.observeAllWithHistory(), - favouritesRepository.observeAllGrouped(SortOrder.NEWEST), + repository.observeFavourites(SortOrder.NEWEST), ) { history, favourites -> mapList(history, favourites) }.catch { e -> diff --git a/app/src/main/java/org/koitharu/kotatsu/library/ui/config/LibraryCategoriesConfigAdapter.kt b/app/src/main/java/org/koitharu/kotatsu/library/ui/config/LibraryCategoriesConfigAdapter.kt new file mode 100644 index 000000000..0abdc7535 --- /dev/null +++ b/app/src/main/java/org/koitharu/kotatsu/library/ui/config/LibraryCategoriesConfigAdapter.kt @@ -0,0 +1,32 @@ +package org.koitharu.kotatsu.library.ui.config + +import androidx.recyclerview.widget.DiffUtil +import com.hannesdorfmann.adapterdelegates4.AsyncListDifferDelegationAdapter +import org.koitharu.kotatsu.base.ui.list.OnListItemClickListener +import org.koitharu.kotatsu.core.model.FavouriteCategory + +class LibraryCategoriesConfigAdapter( + listener: OnListItemClickListener, +) : AsyncListDifferDelegationAdapter(DiffCallback()) { + + init { + delegatesManager.addDelegate(libraryCategoryAD(listener)) + } + + class DiffCallback : DiffUtil.ItemCallback() { + + override fun areItemsTheSame(oldItem: FavouriteCategory, newItem: FavouriteCategory): Boolean { + return oldItem.id == newItem.id + } + + override fun areContentsTheSame(oldItem: FavouriteCategory, newItem: FavouriteCategory): Boolean { + return oldItem.isVisibleInLibrary == newItem.isVisibleInLibrary && oldItem.title == newItem.title + } + + override fun getChangePayload(oldItem: FavouriteCategory, newItem: FavouriteCategory): Any? { + return if (oldItem.isVisibleInLibrary == newItem.isVisibleInLibrary) { + super.getChangePayload(oldItem, newItem) + } else Unit + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/library/ui/config/LibraryCategoriesConfigSheet.kt b/app/src/main/java/org/koitharu/kotatsu/library/ui/config/LibraryCategoriesConfigSheet.kt new file mode 100644 index 000000000..a7d793ea3 --- /dev/null +++ b/app/src/main/java/org/koitharu/kotatsu/library/ui/config/LibraryCategoriesConfigSheet.kt @@ -0,0 +1,56 @@ +package org.koitharu.kotatsu.library.ui.config + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.core.view.isVisible +import androidx.fragment.app.FragmentManager +import org.koin.androidx.viewmodel.ext.android.viewModel +import org.koitharu.kotatsu.R +import org.koitharu.kotatsu.base.ui.BaseBottomSheet +import org.koitharu.kotatsu.base.ui.list.OnListItemClickListener +import org.koitharu.kotatsu.core.model.FavouriteCategory +import org.koitharu.kotatsu.databinding.SheetBaseBinding +import org.koitharu.kotatsu.utils.BottomSheetToolbarController + +class LibraryCategoriesConfigSheet : BaseBottomSheet(), OnListItemClickListener, + View.OnClickListener { + + private val viewModel by viewModel() + + override fun onInflateView(inflater: LayoutInflater, container: ViewGroup?): SheetBaseBinding { + return SheetBaseBinding.inflate(inflater, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + binding.toolbar.setNavigationOnClickListener { dismiss() } + binding.toolbar.setTitle(R.string.favourites_categories) + binding.buttonDone.isVisible = true + binding.buttonDone.setOnClickListener(this) + behavior?.addBottomSheetCallback(BottomSheetToolbarController(binding.toolbar)) + if (!resources.getBoolean(R.bool.is_tablet)) { + binding.toolbar.navigationIcon = null + } + val adapter = LibraryCategoriesConfigAdapter(this) + binding.recyclerView.adapter = adapter + + viewModel.content.observe(viewLifecycleOwner) { adapter.items = it } + } + + override fun onItemClick(item: FavouriteCategory, view: View) { + viewModel.toggleItem(item) + } + + override fun onClick(v: View?) { + dismiss() + } + + companion object { + + private const val TAG = "LibraryCategoriesConfigSheet" + + fun show(fm: FragmentManager) = LibraryCategoriesConfigSheet().show(fm, TAG) + } +} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/library/ui/config/LibraryCategoriesConfigViewModel.kt b/app/src/main/java/org/koitharu/kotatsu/library/ui/config/LibraryCategoriesConfigViewModel.kt new file mode 100644 index 000000000..88217c8b2 --- /dev/null +++ b/app/src/main/java/org/koitharu/kotatsu/library/ui/config/LibraryCategoriesConfigViewModel.kt @@ -0,0 +1,27 @@ +package org.koitharu.kotatsu.library.ui.config + +import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import org.koitharu.kotatsu.base.ui.BaseViewModel +import org.koitharu.kotatsu.core.model.FavouriteCategory +import org.koitharu.kotatsu.favourites.domain.FavouritesRepository +import org.koitharu.kotatsu.utils.ext.asLiveDataDistinct + +class LibraryCategoriesConfigViewModel( + private val favouritesRepository: FavouritesRepository, +) : BaseViewModel() { + + val content = favouritesRepository.observeCategories() + .asLiveDataDistinct(viewModelScope.coroutineContext + Dispatchers.Default, emptyList()) + + private var updateJob: Job? = null + + fun toggleItem(category: FavouriteCategory) { + val prevJob = updateJob + updateJob = launchJob(Dispatchers.Default) { + prevJob?.join() + favouritesRepository.updateCategory(category.id, !category.isVisibleInLibrary) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/library/ui/config/LibraryCategoryAD.kt b/app/src/main/java/org/koitharu/kotatsu/library/ui/config/LibraryCategoryAD.kt new file mode 100644 index 000000000..01987d6b1 --- /dev/null +++ b/app/src/main/java/org/koitharu/kotatsu/library/ui/config/LibraryCategoryAD.kt @@ -0,0 +1,22 @@ +package org.koitharu.kotatsu.library.ui.config + +import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding +import org.koitharu.kotatsu.base.ui.list.AdapterDelegateClickListenerAdapter +import org.koitharu.kotatsu.base.ui.list.OnListItemClickListener +import org.koitharu.kotatsu.core.model.FavouriteCategory +import org.koitharu.kotatsu.databinding.ItemCategoryCheckableMultipleBinding + +fun libraryCategoryAD( + listener: OnListItemClickListener, +) = adapterDelegateViewBinding( + { layoutInflater, parent -> ItemCategoryCheckableMultipleBinding.inflate(layoutInflater, parent, false) } +) { + + val eventListener = AdapterDelegateClickListenerAdapter(this, listener) + itemView.setOnClickListener(eventListener) + + bind { + binding.root.text = item.title + binding.root.isChecked = item.isVisibleInLibrary + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/item_category_checkable_multiple.xml b/app/src/main/res/layout/item_category_checkable_multiple.xml new file mode 100644 index 000000000..712a1a9e4 --- /dev/null +++ b/app/src/main/res/layout/item_category_checkable_multiple.xml @@ -0,0 +1,15 @@ + + \ No newline at end of file diff --git a/app/src/main/res/layout/sheet_base.xml b/app/src/main/res/layout/sheet_base.xml new file mode 100644 index 000000000..5746e4fd8 --- /dev/null +++ b/app/src/main/res/layout/sheet_base.xml @@ -0,0 +1,46 @@ + + + + + + + +