Migrate More screen to Compose (#6990)
parent
8933b41937
commit
c25cffafc6
@ -0,0 +1,38 @@
|
||||
package eu.kanade.core.prefs
|
||||
|
||||
import androidx.compose.runtime.MutableState
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import com.fredporciuncula.flow.preferences.Preference
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
|
||||
class PreferenceMutableState<T>(
|
||||
private val preference: Preference<T>,
|
||||
scope: CoroutineScope,
|
||||
) : MutableState<T> {
|
||||
|
||||
private val state = mutableStateOf(preference.get())
|
||||
|
||||
init {
|
||||
preference.asFlow()
|
||||
.distinctUntilChanged()
|
||||
.onEach { state.value = it }
|
||||
.launchIn(scope)
|
||||
}
|
||||
|
||||
override var value: T
|
||||
get() = state.value
|
||||
set(value) {
|
||||
preference.set(value)
|
||||
}
|
||||
|
||||
override fun component1(): T {
|
||||
return state.value
|
||||
}
|
||||
|
||||
override fun component2(): (T) -> Unit {
|
||||
return { preference.set(it) }
|
||||
}
|
||||
}
|
@ -0,0 +1,131 @@
|
||||
package eu.kanade.presentation.more
|
||||
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.CloudOff
|
||||
import androidx.compose.material.icons.outlined.GetApp
|
||||
import androidx.compose.material.icons.outlined.HelpOutline
|
||||
import androidx.compose.material.icons.outlined.Info
|
||||
import androidx.compose.material.icons.outlined.Label
|
||||
import androidx.compose.material.icons.outlined.Settings
|
||||
import androidx.compose.material.icons.outlined.SettingsBackupRestore
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.vector.rememberVectorPainter
|
||||
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
|
||||
import androidx.compose.ui.input.nestedscroll.nestedScroll
|
||||
import androidx.compose.ui.platform.LocalUriHandler
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import eu.kanade.presentation.components.Divider
|
||||
import eu.kanade.presentation.components.PreferenceRow
|
||||
import eu.kanade.presentation.components.SwitchPreference
|
||||
import eu.kanade.presentation.util.quantityStringResource
|
||||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.ui.more.DownloadQueueState
|
||||
import eu.kanade.tachiyomi.ui.more.MoreController
|
||||
import eu.kanade.tachiyomi.ui.more.MorePresenter
|
||||
|
||||
@Composable
|
||||
fun MoreScreen(
|
||||
nestedScrollInterop: NestedScrollConnection,
|
||||
presenter: MorePresenter,
|
||||
onClickDownloadQueue: () -> Unit,
|
||||
onClickCategories: () -> Unit,
|
||||
onClickBackupAndRestore: () -> Unit,
|
||||
onClickSettings: () -> Unit,
|
||||
onClickAbout: () -> Unit,
|
||||
) {
|
||||
val uriHandler = LocalUriHandler.current
|
||||
val downloadQueueState by presenter.downloadQueueState.collectAsState()
|
||||
|
||||
LazyColumn(
|
||||
modifier = Modifier.nestedScroll(nestedScrollInterop),
|
||||
) {
|
||||
item {
|
||||
LogoHeader()
|
||||
}
|
||||
|
||||
item {
|
||||
SwitchPreference(
|
||||
preference = presenter.downloadedOnly,
|
||||
title = stringResource(R.string.label_downloaded_only),
|
||||
subtitle = stringResource(R.string.downloaded_only_summary),
|
||||
painter = rememberVectorPainter(Icons.Outlined.CloudOff),
|
||||
)
|
||||
}
|
||||
item {
|
||||
SwitchPreference(
|
||||
preference = presenter.incognitoMode,
|
||||
title = stringResource(R.string.pref_incognito_mode),
|
||||
subtitle = stringResource(R.string.pref_incognito_mode_summary),
|
||||
painter = painterResource(R.drawable.ic_glasses_24dp),
|
||||
)
|
||||
}
|
||||
|
||||
item { Divider() }
|
||||
|
||||
item {
|
||||
PreferenceRow(
|
||||
title = stringResource(R.string.label_download_queue),
|
||||
subtitle = when (downloadQueueState) {
|
||||
DownloadQueueState.Stopped -> null
|
||||
is DownloadQueueState.Paused -> {
|
||||
val pending = (downloadQueueState as DownloadQueueState.Paused).pending
|
||||
if (pending == 0) {
|
||||
stringResource(R.string.paused)
|
||||
} else {
|
||||
"${stringResource(R.string.paused)} • ${quantityStringResource(R.plurals.download_queue_summary, pending, pending)}"
|
||||
}
|
||||
}
|
||||
is DownloadQueueState.Downloading -> {
|
||||
val pending = (downloadQueueState as DownloadQueueState.Downloading).pending
|
||||
quantityStringResource(R.plurals.download_queue_summary, pending, pending)
|
||||
}
|
||||
},
|
||||
painter = rememberVectorPainter(Icons.Outlined.GetApp),
|
||||
onClick = { onClickDownloadQueue() },
|
||||
)
|
||||
}
|
||||
item {
|
||||
PreferenceRow(
|
||||
title = stringResource(R.string.categories),
|
||||
painter = rememberVectorPainter(Icons.Outlined.Label),
|
||||
onClick = { onClickCategories() },
|
||||
)
|
||||
}
|
||||
item {
|
||||
PreferenceRow(
|
||||
title = stringResource(R.string.label_backup),
|
||||
painter = rememberVectorPainter(Icons.Outlined.SettingsBackupRestore),
|
||||
onClick = { onClickBackupAndRestore() },
|
||||
)
|
||||
}
|
||||
|
||||
item { Divider() }
|
||||
|
||||
item {
|
||||
PreferenceRow(
|
||||
title = stringResource(R.string.label_settings),
|
||||
painter = rememberVectorPainter(Icons.Outlined.Settings),
|
||||
onClick = { onClickSettings() },
|
||||
)
|
||||
}
|
||||
item {
|
||||
PreferenceRow(
|
||||
title = stringResource(R.string.pref_category_about),
|
||||
painter = rememberVectorPainter(Icons.Outlined.Info),
|
||||
onClick = { onClickAbout() },
|
||||
)
|
||||
}
|
||||
item {
|
||||
PreferenceRow(
|
||||
title = stringResource(R.string.label_help),
|
||||
painter = rememberVectorPainter(Icons.Outlined.HelpOutline),
|
||||
onClick = { uriHandler.openUri(MoreController.URL_HELP) },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package eu.kanade.presentation.util
|
||||
|
||||
import androidx.annotation.PluralsRes
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
|
||||
/**
|
||||
* Load a quantity string resource.
|
||||
*
|
||||
* @param id the resource identifier
|
||||
* @param quantity The number used to get the string for the current language's plural rules.
|
||||
* @return the string data associated with the resource
|
||||
*/
|
||||
@Composable
|
||||
fun quantityStringResource(@PluralsRes id: Int, quantity: Int): String {
|
||||
val context = LocalContext.current
|
||||
return context.resources.getQuantityString(id, quantity, quantity)
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a quantity string resource with formatting.
|
||||
*
|
||||
* @param id the resource identifier
|
||||
* @param quantity The number used to get the string for the current language's plural rules.
|
||||
* @param formatArgs the format arguments
|
||||
* @return the string data associated with the resource
|
||||
*/
|
||||
@Composable
|
||||
fun quantityStringResource(@PluralsRes id: Int, quantity: Int, vararg formatArgs: Any): String {
|
||||
val context = LocalContext.current
|
||||
return context.resources.getQuantityString(id, quantity, *formatArgs)
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
package eu.kanade.tachiyomi.ui.more
|
||||
|
||||
import android.os.Bundle
|
||||
import eu.kanade.tachiyomi.data.download.DownloadManager
|
||||
import eu.kanade.tachiyomi.data.download.DownloadService
|
||||
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
||||
import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter
|
||||
import eu.kanade.tachiyomi.util.lang.launchIO
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import rx.Observable
|
||||
import rx.Subscription
|
||||
import rx.android.schedulers.AndroidSchedulers
|
||||
import rx.subscriptions.CompositeSubscription
|
||||
import uy.kohesive.injekt.Injekt
|
||||
import uy.kohesive.injekt.api.get
|
||||
|
||||
class MorePresenter(
|
||||
private val downloadManager: DownloadManager = Injekt.get(),
|
||||
preferencesHelper: PreferencesHelper = Injekt.get(),
|
||||
) : BasePresenter<MoreController>() {
|
||||
|
||||
val downloadedOnly = preferencesHelper.downloadedOnly().asState()
|
||||
val incognitoMode = preferencesHelper.incognitoMode().asState()
|
||||
|
||||
private var _state: MutableStateFlow<DownloadQueueState> = MutableStateFlow(DownloadQueueState.Stopped)
|
||||
val downloadQueueState: StateFlow<DownloadQueueState> = _state
|
||||
|
||||
private var isDownloading: Boolean = false
|
||||
private var downloadQueueSize: Int = 0
|
||||
private var untilDestroySubscriptions = CompositeSubscription()
|
||||
|
||||
override fun onCreate(savedState: Bundle?) {
|
||||
super.onCreate(savedState)
|
||||
|
||||
if (untilDestroySubscriptions.isUnsubscribed) {
|
||||
untilDestroySubscriptions = CompositeSubscription()
|
||||
}
|
||||
|
||||
initDownloadQueueSummary()
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
untilDestroySubscriptions.unsubscribe()
|
||||
}
|
||||
|
||||
private fun initDownloadQueueSummary() {
|
||||
// Handle running/paused status change
|
||||
DownloadService.runningRelay
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribeUntilDestroy { isRunning ->
|
||||
isDownloading = isRunning
|
||||
updateDownloadQueueState()
|
||||
}
|
||||
|
||||
// Handle queue progress updating
|
||||
downloadManager.queue.getUpdatedObservable()
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribeUntilDestroy {
|
||||
downloadQueueSize = it.size
|
||||
updateDownloadQueueState()
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateDownloadQueueState() {
|
||||
presenterScope.launchIO {
|
||||
val pendingDownloadExists = downloadQueueSize != 0
|
||||
_state.emit(
|
||||
when {
|
||||
!pendingDownloadExists -> DownloadQueueState.Stopped
|
||||
!isDownloading && !pendingDownloadExists -> DownloadQueueState.Paused(0)
|
||||
!isDownloading && pendingDownloadExists -> DownloadQueueState.Paused(downloadQueueSize)
|
||||
else -> DownloadQueueState.Downloading(downloadQueueSize)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun <T> Observable<T>.subscribeUntilDestroy(onNext: (T) -> Unit): Subscription {
|
||||
return subscribe(onNext).also { untilDestroySubscriptions.add(it) }
|
||||
}
|
||||
}
|
||||
|
||||
sealed class DownloadQueueState {
|
||||
object Stopped : DownloadQueueState()
|
||||
data class Paused(val pending: Int) : DownloadQueueState()
|
||||
data class Downloading(val pending: Int) : DownloadQueueState()
|
||||
}
|
Loading…
Reference in new issue