Merge pull request #103 from amir1376/fix/stop-all-action

fix stop all action not disabled when nothing active
This commit is contained in:
AmirHossein Abdolmotallebi 2024-10-10 11:33:43 +03:30 committed by GitHub
commit a093ab917f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 22 deletions

View File

@ -18,6 +18,7 @@ import ir.amirab.downloader.queue.activeQueuesFlow
import ir.amirab.downloader.queue.inactiveQueuesFlow
import com.abdownloadmanager.utils.extractors.linkextractor.DownloadCredentialFromStringExtractor
import ir.amirab.util.UrlUtils
import ir.amirab.util.flow.combineStateFlows
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
@ -27,6 +28,15 @@ private val appComponent = Di.get<AppComponent>()
private val scope = Di.get<CoroutineScope>()
private val downloadSystem = appComponent.downloadSystem
private val activeQueuesFlow = downloadSystem
.queueManager
.activeQueuesFlow(scope)
.stateIn(
scope,
SharingStarted.WhileSubscribed(),
emptyList()
)
val newDownloadAction = simpleAction(
"New Download",
MyIcons.add,
@ -60,8 +70,7 @@ val stopQueueGroupAction = MenuItem.SubMenu(
title = "Stop Queue",
items = emptyList()
).apply {
appComponent.downloadSystem.queueManager
.activeQueuesFlow(scope)
activeQueuesFlow
.onEach {
setItems(it.map {
stopQueueAction(it)
@ -86,15 +95,20 @@ val startQueueGroupAction = MenuItem.SubMenu(
}
val stopAllAction = simpleAction("Stop All", MyIcons.stop) {
val stopAllAction = simpleAction(
"Stop All",
MyIcons.stop,
checkEnable = combineStateFlows(
downloadSystem.downloadMonitor.activeDownloadCount,
activeQueuesFlow
) { downloadCount, activeQueues ->
downloadCount > 0 || activeQueues.isNotEmpty()
}
) {
scope.launch {
downloadSystem.stopAnything()
}
}.apply {
downloadSystem.downloadMonitor.activeDownloadCount
.onEach {
setEnabled( it > 0)
}.launchIn(scope)
}
val exitAction = simpleAction(
@ -108,7 +122,7 @@ val browserIntegrations = MenuItem.SubMenu(
title = "Download Browser Integration",
icon = MyIcons.download,
items = buildMenu {
for (browserExtension in SharedConstants.browserIntegrations){
for (browserExtension in SharedConstants.browserIntegrations) {
item(
title = browserExtension.type.getName(),
icon = browserExtension.type.getIcon(),
@ -130,6 +144,7 @@ val showDownloadList = simpleAction(
) {
appComponent.openHome()
}
/*val checkForUpdateAction = simpleAction(
title = "Check For Update",
icon = MyIcons.refresh,
@ -153,17 +168,17 @@ val supportActionGroup = MenuItem.SubMenu(
title = "Support & Community",
icon = MyIcons.group,
items = buildMenu {
item("Website",MyIcons.appIcon){
item("Website", MyIcons.appIcon) {
UrlUtils.openUrl(AppInfo.website)
}
item("Source Code",MyIcons.openSource){
item("Source Code", MyIcons.openSource) {
UrlUtils.openUrl(AppInfo.sourceCode)
}
subMenu("Telegram",MyIcons.telegram){
item("Channel",MyIcons.speaker){
subMenu("Telegram", MyIcons.telegram) {
item("Channel", MyIcons.speaker) {
UrlUtils.openUrl(SharedConstants.telegramChannelUrl)
}
item("Group",MyIcons.group){
item("Group", MyIcons.group) {
UrlUtils.openUrl(SharedConstants.telegramGroupUrl)
}
}

View File

@ -15,16 +15,17 @@ sealed interface MenuItem {
val title: StateFlow<String>
}
interface CanBeModified{
interface CanBeModified {
fun setIcon(icon: IconSource?)
fun setTitle(title:String)
fun setTitle(title: String)
}
interface HasEnable {
//compose aware property
val isEnabled: StateFlow<Boolean>
}
interface CanChangeEnabled{
interface CanChangeEnabled {
fun setEnabled(boolean: Boolean)
}
@ -34,7 +35,7 @@ sealed interface MenuItem {
abstract class SingleItem(
title: String,
icon: IconSource?=null,
icon: IconSource? = null,
) : MenuItem,
ClickableItem,
ReadableItem,
@ -44,14 +45,13 @@ sealed interface MenuItem {
var shouldDismissOnClick: Boolean = true
private val _title: MutableStateFlow<String> = MutableStateFlow(title)
private val _icon: MutableStateFlow<IconSource?> = MutableStateFlow(icon)
private val _isEnabled: MutableStateFlow<Boolean> = MutableStateFlow(true)
override val title: StateFlow<String> = _title.asStateFlow()
override val icon: StateFlow<IconSource?> = MutableStateFlow(icon)
override val isEnabled: StateFlow<Boolean> = MutableStateFlow(true)
override val icon: StateFlow<IconSource?> = _icon.asStateFlow()
override val isEnabled: StateFlow<Boolean> = _isEnabled.asStateFlow()
override fun setEnabled(boolean: Boolean) {
_isEnabled.update { boolean }
@ -88,8 +88,8 @@ sealed interface MenuItem {
override var icon: StateFlow<IconSource?> = _icon.asStateFlow()
override var title: StateFlow<String> = _title.asStateFlow()
val items:StateFlow<List<MenuItem>> = _items.asStateFlow()
fun setItems(newItems:List<MenuItem>){
val items: StateFlow<List<MenuItem>> = _items.asStateFlow()
fun setItems(newItems: List<MenuItem>) {
_items.update { newItems }
}