Skip to content

Commit

Permalink
Hide sync library option when sync is disabled (#1275)
Browse files Browse the repository at this point in the history
* Hide sync library option when sync is disabled

- Add isSyncEnabled parameter to LibraryToolbar and LibraryRegularToolbar
- Pass isSyncEnabled from LibraryTab to LibraryToolbar
- Conditionally display sync library action based on isSyncEnabled
- Update LibraryContent to include isSyncEnabled parameter
- Adjust LibraryTab to handle sync-related functionality
- Remove direct dependency on SyncPreferences in LibraryToolbar

* Update LibraryScreenModel to react to sync service changes

- Replace static isSyncEnabled update with dynamic observation
- Use syncPreferences.syncService().changes() to update isSyncEnabled state
- Ensure isSyncEnabled is updated whenever the sync service changes

Co-authored-by: jobobby04 <[email protected]>

* Fix sync service state update in LibraryScreenModel

- Resolve ambiguity in lambda parameters
- Correctly update isSyncEnabled state based on syncService value

* Optimize sync service state updates in LibraryScreenModel

- Add distinctUntilChanged() to filter out consecutive duplicate emissions
- Simplify mutableState.update lambda for better readability
- Remove redundant boolean comparison in isSyncEnabled assignment

---------

Co-authored-by: jobobby04 <[email protected]>
  • Loading branch information
FuSan21 and jobobby04 authored Oct 14, 2024
1 parent 41e523e commit 697b0de
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fun LibraryToolbar(
onClickSyncNow: () -> Unit,
// SY -->
onClickSyncExh: (() -> Unit)?,
isSyncEnabled: Boolean,
// SY <--
searchQuery: String?,
onSearchQueryChange: (String?) -> Unit,
Expand All @@ -64,6 +65,7 @@ fun LibraryToolbar(
onClickSyncNow = onClickSyncNow,
// SY -->
onClickSyncExh = onClickSyncExh,
isSyncEnabled = isSyncEnabled,
// SY <--
scrollBehavior = scrollBehavior,
)
Expand All @@ -82,6 +84,7 @@ private fun LibraryRegularToolbar(
onClickSyncNow: () -> Unit,
// SY -->
onClickSyncExh: (() -> Unit)?,
isSyncEnabled: Boolean,
// SY <--
scrollBehavior: TopAppBarScrollBehavior?,
) {
Expand Down Expand Up @@ -128,10 +131,6 @@ private fun LibraryRegularToolbar(
title = stringResource(MR.strings.action_open_random_manga),
onClick = onClickOpenRandomManga,
),
AppBar.OverflowAction(
title = stringResource(SYMR.strings.sync_library),
onClick = onClickSyncNow,
),
).builder().apply {
// SY -->
if (onClickSyncExh != null) {
Expand All @@ -142,6 +141,14 @@ private fun LibraryRegularToolbar(
),
)
}
if (isSyncEnabled) {
add(
AppBar.OverflowAction(
title = stringResource(SYMR.strings.sync_library),
onClick = onClickSyncNow,
),
)
}
// SY <--
}.build(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import eu.kanade.domain.base.BasePreferences
import eu.kanade.domain.chapter.interactor.SetReadStatus
import eu.kanade.domain.manga.interactor.UpdateManga
import eu.kanade.domain.source.service.SourcePreferences
import eu.kanade.domain.sync.SyncPreferences
import eu.kanade.presentation.components.SEARCH_DEBOUNCE_MILLIS
import eu.kanade.presentation.library.components.LibraryToolbarTitle
import eu.kanade.presentation.manga.DownloadAction
Expand Down Expand Up @@ -151,6 +152,7 @@ class LibraryScreenModel(
private val searchEngine: SearchEngine = Injekt.get(),
private val setCustomMangaInfo: SetCustomMangaInfo = Injekt.get(),
private val getMergedChaptersByMangaId: GetMergedChaptersByMangaId = Injekt.get(),
private val syncPreferences: SyncPreferences = Injekt.get(),
// SY <--
) : StateScreenModel<LibraryScreenModel.State>(State()) {

Expand Down Expand Up @@ -274,6 +276,13 @@ class LibraryScreenModel(
}
}
.launchIn(screenModelScope)
syncPreferences.syncService()
.changes()
.distinctUntilChanged()
.onEach { syncService ->
mutableState.update { it.copy(isSyncEnabled = syncService != 0) }
}
.launchIn(screenModelScope)
// SY <--
}

Expand Down Expand Up @@ -1360,6 +1369,7 @@ class LibraryScreenModel(
val dialog: Dialog? = null,
// SY -->
val showSyncExh: Boolean = false,
val isSyncEnabled: Boolean = false,
val ogCategories: List<Category> = emptyList(),
val groupType: Int = LibraryGroup.BY_DEFAULT,
// SY <--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ object LibraryTab : Tab {
},
// SY -->
onClickSyncExh = screenModel::openFavoritesSyncDialog.takeIf { state.showSyncExh },
isSyncEnabled = state.isSyncEnabled,
// SY <--
searchQuery = state.searchQuery,
onSearchQueryChange = screenModel::search,
Expand Down

0 comments on commit 697b0de

Please sign in to comment.