Add option to show favourite category on shelf

This commit is contained in:
Koitharu
2023-04-12 20:07:29 +03:00
parent c8053b2eb6
commit bd5b6beb72
8 changed files with 50 additions and 13 deletions

View File

@@ -23,8 +23,8 @@ abstract class FavouriteCategoriesDao {
suspend fun delete(id: Long) = setDeletedAt(id, System.currentTimeMillis())
@Query("UPDATE favourite_categories SET title = :title, `order` = :order, `track` = :tracker WHERE category_id = :id")
abstract suspend fun update(id: Long, title: String, order: String, tracker: Boolean)
@Query("UPDATE favourite_categories SET title = :title, `order` = :order, `track` = :tracker, `show_in_lib` = :onShelf WHERE category_id = :id")
abstract suspend fun update(id: Long, title: String, order: String, tracker: Boolean, onShelf: Boolean)
@Query("UPDATE favourite_categories SET `order` = :order WHERE category_id = :id")
abstract suspend fun updateOrder(id: Long, order: String)

View File

@@ -1,12 +1,18 @@
package org.koitharu.kotatsu.favourites.domain
import androidx.room.withTransaction
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.map
import org.koitharu.kotatsu.base.domain.ReversibleHandle
import org.koitharu.kotatsu.core.db.MangaDatabase
import org.koitharu.kotatsu.core.db.entity.*
import org.koitharu.kotatsu.core.db.entity.SortOrder
import org.koitharu.kotatsu.core.db.entity.toEntities
import org.koitharu.kotatsu.core.db.entity.toEntity
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.FavouriteCategoryEntity
import org.koitharu.kotatsu.favourites.data.FavouriteEntity
@@ -15,6 +21,8 @@ import org.koitharu.kotatsu.parsers.model.Manga
import org.koitharu.kotatsu.parsers.model.SortOrder
import org.koitharu.kotatsu.tracker.work.TrackerNotificationChannels
import org.koitharu.kotatsu.utils.ext.mapItems
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class FavouritesRepository @Inject constructor(
@@ -83,7 +91,12 @@ class FavouritesRepository @Inject constructor(
return db.favouriteCategoriesDao.find(id.toInt()).toFavouriteCategory()
}
suspend fun createCategory(title: String, sortOrder: SortOrder, isTrackerEnabled: Boolean): FavouriteCategory {
suspend fun createCategory(
title: String,
sortOrder: SortOrder,
isTrackerEnabled: Boolean,
isVisibleOnShelf: Boolean,
): FavouriteCategory {
val entity = FavouriteCategoryEntity(
title = title,
createdAt = System.currentTimeMillis(),
@@ -92,7 +105,7 @@ class FavouritesRepository @Inject constructor(
order = sortOrder.name,
track = isTrackerEnabled,
deletedAt = 0L,
isVisibleInLibrary = true,
isVisibleInLibrary = isVisibleOnShelf,
)
val id = db.favouriteCategoriesDao.insert(entity)
val category = entity.toFavouriteCategory(id)
@@ -100,8 +113,14 @@ class FavouritesRepository @Inject constructor(
return category
}
suspend fun updateCategory(id: Long, title: String, sortOrder: SortOrder, isTrackerEnabled: Boolean) {
db.favouriteCategoriesDao.update(id, title, sortOrder.name, isTrackerEnabled)
suspend fun updateCategory(
id: Long,
title: String,
sortOrder: SortOrder,
isTrackerEnabled: Boolean,
isVisibleOnShelf: Boolean,
) {
db.favouriteCategoriesDao.update(id, title, sortOrder.name, isTrackerEnabled, isVisibleOnShelf)
}
suspend fun updateCategory(id: Long, isVisibleInLibrary: Boolean) {

View File

@@ -77,6 +77,7 @@ class FavouritesCategoryEditActivity :
title = binding.editName.text?.toString()?.trim().orEmpty(),
sortOrder = getSelectedSortOrder(),
isTrackerEnabled = binding.switchTracker.isChecked,
isVisibleOnShelf = binding.switchShelf.isChecked,
)
}
}
@@ -112,6 +113,9 @@ class FavouritesCategoryEditActivity :
val sortText = getString((category?.order ?: SortOrder.NEWEST).titleRes)
binding.editSort.setText(sortText, false)
binding.switchTracker.isChecked = category?.isTrackingEnabled ?: true
binding.switchTracker.jumpDrawablesToCurrentState()
binding.switchShelf.isChecked = category?.isVisibleInLibrary ?: true
binding.switchShelf.jumpDrawablesToCurrentState()
}
private fun onError(e: Throwable) {
@@ -123,6 +127,7 @@ class FavouritesCategoryEditActivity :
binding.editSort.isEnabled = !isLoading
binding.editName.isEnabled = !isLoading
binding.switchTracker.isEnabled = !isLoading
binding.switchShelf.isEnabled = !isLoading
if (isLoading) {
binding.textViewError.isVisible = false
}

View File

@@ -48,13 +48,14 @@ class FavouritesCategoryEditViewModel @Inject constructor(
title: String,
sortOrder: SortOrder,
isTrackerEnabled: Boolean,
isVisibleOnShelf: Boolean,
) {
launchLoadingJob(Dispatchers.Default) {
check(title.isNotEmpty())
if (categoryId == NO_ID) {
repository.createCategory(title, sortOrder, isTrackerEnabled)
repository.createCategory(title, sortOrder, isTrackerEnabled, isVisibleOnShelf)
} else {
repository.updateCategory(categoryId, title, sortOrder, isTrackerEnabled)
repository.updateCategory(categoryId, title, sortOrder, isTrackerEnabled, isVisibleOnShelf)
}
onSaved.postCall(Unit)
}

View File

@@ -14,6 +14,7 @@ import androidx.recyclerview.widget.RecyclerView
import dagger.hilt.android.AndroidEntryPoint
import org.koitharu.kotatsu.base.ui.BaseActivity
import org.koitharu.kotatsu.databinding.ActivityShelfSettingsBinding
import com.google.android.material.R as materialR
@AndroidEntryPoint
class ShelfSettingsActivity :
@@ -28,7 +29,7 @@ class ShelfSettingsActivity :
setContentView(ActivityShelfSettingsBinding.inflate(layoutInflater))
supportActionBar?.run {
setDisplayHomeAsUpEnabled(true)
setHomeAsUpIndicator(com.google.android.material.R.drawable.abc_ic_clear_material)
setHomeAsUpIndicator(materialR.drawable.abc_ic_clear_material)
}
binding.buttonDone.setOnClickListener(this)
val settingsAdapter = ShelfSettingsAdapter(this)

View File

@@ -43,6 +43,7 @@ fun shelfSectionAD(
bind {
binding.textViewTitle.setText(item.section.titleResId)
binding.switchToggle.isChecked = item.isChecked
binding.switchToggle.jumpDrawablesToCurrentState()
}
}
@@ -63,6 +64,7 @@ fun shelfCategoryAD(
bind {
binding.root.text = item.title
binding.root.isChecked = item.isChecked
binding.root.jumpDrawablesToCurrentState()
}
}

View File

@@ -78,6 +78,14 @@
android:visibility="gone"
tools:visibility="visible" />
<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/switch_shelf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/show_on_shelf"
tools:visibility="visible" />
<TextView
android:id="@+id/textView_error"
android:layout_width="match_parent"

View File

@@ -432,4 +432,5 @@
<string name="folder_with_images_import_description">You can select a directory with archives or images. Each archive (or subdirectory) will be recognized as a chapter.</string>
<string name="speed">Speed</string>
<string name="restore_backup_description">Import a previously created backup of user data</string>
<string name="show_on_shelf">Show on the Shelf</string>
</resources>