Skip to content
This repository has been archived by the owner on Oct 3, 2022. It is now read-only.

Commit

Permalink
Add category updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jobobby04 committed Aug 13, 2022
1 parent c51f96a commit b209780
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ class NovelUpdateWorker(
val updatedChapters = arrayListOf<ChapterEntity>()

iNovelsRepository.loadLibraryNovelEntities().first().let { list ->
val categoryID = inputData.getInt(KEY_CATEGORY, -1)
if (categoryID >= 0) {
list.filter { it.category == categoryID }
} else list
}.let { list ->
if (onlyUpdateOngoing())
list.filter { it.status != Novel.Status.COMPLETED }
else list
Expand Down Expand Up @@ -446,7 +451,7 @@ class NovelUpdateWorker(
if (SDK_INT >= VERSION_CODES.M)
setRequiresDeviceIdle(updateOnlyIdle())
}.build()
).build()
).setInputData(data).build()
)
workerManager.getWorkInfosForUniqueWork(UPDATE_WORK_ID).await()[0].let {
Log.d(logID(), "State ${it.state}")
Expand All @@ -466,6 +471,6 @@ class NovelUpdateWorker(
const val KEY_CHAPTERS: String = "Novels"

const val KEY_NOVELS: Int = 0x00
const val KEY_CATEGORY: Int = 0x01
const val KEY_CATEGORY: String = "category"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package app.shosetsu.android.domain.usecases.start

import androidx.work.Data
import androidx.work.await
import app.shosetsu.android.backend.workers.onetime.NovelUpdateWorker
import app.shosetsu.android.backend.workers.onetime.NovelUpdateWorker.Manager
import app.shosetsu.android.common.ext.launchIO

Expand Down Expand Up @@ -32,15 +34,19 @@ class StartUpdateWorkerUseCase(
* Starts the update worker
* @param override if true then will override the current update loop
*/
operator fun invoke(override: Boolean = false) {
operator fun invoke(categoryID: Int, override: Boolean = false) {
launchIO {
if (manager.isRunning())
if (override)
manager.stop().await()
else
return@launchIO

manager.start()
if (categoryID >= 0) {
manager.start(Data(mapOf(NovelUpdateWorker.KEY_CATEGORY to categoryID)))
} else {
manager.start()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.produceState
Expand Down Expand Up @@ -167,7 +168,7 @@ class LibraryController
columnsInH,
hasSelected = hasSelected,
onRefresh = {
onRefresh()
onRefresh(it)
},
onOpen = { (id) ->
try {
Expand Down Expand Up @@ -289,7 +290,7 @@ class LibraryController
when (item.itemId) {
R.id.updater_now -> {
if (viewModel.isOnline())
viewModel.startUpdateManager()
viewModel.startUpdateManager(-1)
else displayOfflineSnackBar()
true
}
Expand Down Expand Up @@ -382,9 +383,9 @@ class LibraryController
fab.setIconResource(R.drawable.filter)
}

fun onRefresh() {
fun onRefresh(categoryID: Int) {
if (viewModel.isOnline())
viewModel.startUpdateManager()
viewModel.startUpdateManager(categoryID)
else displayOfflineSnackBar(R.string.generic_error_cannot_update_library_offline)
}
}
Expand All @@ -398,7 +399,7 @@ fun LibraryContent(
columnsInV: Int,
columnsInH: Int,
hasSelected: Boolean,
onRefresh: () -> Unit,
onRefresh: (Int) -> Unit,
onOpen: (LibraryNovelUI) -> Unit,
toggleSelection: (LibraryNovelUI) -> Unit,
toastNovel: ((LibraryNovelUI) -> Unit)?,
Expand Down Expand Up @@ -440,7 +441,7 @@ fun LibraryPager(
columnsInV: Int,
columnsInH: Int,
hasSelected: Boolean,
onRefresh: () -> Unit,
onRefresh: (Int) -> Unit,
onOpen: (LibraryNovelUI) -> Unit,
toggleSelection: (LibraryNovelUI) -> Unit,
toastNovel: ((LibraryNovelUI) -> Unit)?,
Expand Down Expand Up @@ -482,9 +483,12 @@ fun LibraryPager(
state = state,
modifier = Modifier.fillMaxSize()
) {
val items by produceState(emptyList(), library, it) {
val id by derivedStateOf {
library.categories[it].id
}
val items by produceState(emptyList(), library, it, id) {
value = withContext(Dispatchers.IO) {
library.novels[library.categories[it].id].orEmpty()
library.novels[id].orEmpty()
}
}
LibraryCategory(
Expand All @@ -493,7 +497,7 @@ fun LibraryPager(
columnsInV = columnsInV,
columnsInH = columnsInH,
hasSelected = hasSelected,
onRefresh = onRefresh,
onRefresh = { onRefresh(id) },
onOpen = onOpen,
toggleSelection = toggleSelection,
toastNovel = toastNovel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class ComposeUpdatesController : ShosetsuController(), HomeFragment {

fun onRefresh() {
if (viewModel.isOnline())
viewModel.startUpdateManager()
viewModel.startUpdateManager(-1)
else displayOfflineSnackBar(R.string.generic_error_cannot_update_library_offline)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ package app.shosetsu.android.viewmodel.base
interface StartUpdateManagerViewModel {

/** Starts the update manager, Will not start if it is running */
fun startUpdateManager()
fun startUpdateManager(categoryID: Int)
}
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,8 @@ class LibraryViewModel(

override fun isOnline(): Boolean = isOnlineUseCase()

override fun startUpdateManager() {
startUpdateWorkerUseCase(true)
override fun startUpdateManager(categoryID: Int) {
startUpdateWorkerUseCase(categoryID, true)
}

override fun removeSelectedFromLibrary() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class UpdatesViewModel(
}.shareIn(viewModelScope + Dispatchers.IO, SharingStarted.Lazily, 1)
}

override fun startUpdateManager() = startUpdateWorkerUseCase()
override fun startUpdateManager(categoryID: Int) = startUpdateWorkerUseCase(categoryID)

override fun isOnline(): Boolean = isOnlineUseCase()

Expand Down

0 comments on commit b209780

Please sign in to comment.