diff --git a/desktop/app/src/main/kotlin/com/abdownloadmanager/desktop/actions/main.kt b/desktop/app/src/main/kotlin/com/abdownloadmanager/desktop/actions/main.kt index 7d4234b..7c8cde8 100644 --- a/desktop/app/src/main/kotlin/com/abdownloadmanager/desktop/actions/main.kt +++ b/desktop/app/src/main/kotlin/com/abdownloadmanager/desktop/actions/main.kt @@ -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() private val scope = Di.get() 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) } } diff --git a/shared/compose-utils/src/main/kotlin/ir/amirab/util/compose/action/MenuItem.kt b/shared/compose-utils/src/main/kotlin/ir/amirab/util/compose/action/MenuItem.kt index 067ce7d..a3fd88b 100644 --- a/shared/compose-utils/src/main/kotlin/ir/amirab/util/compose/action/MenuItem.kt +++ b/shared/compose-utils/src/main/kotlin/ir/amirab/util/compose/action/MenuItem.kt @@ -15,16 +15,17 @@ sealed interface MenuItem { val title: StateFlow } - interface CanBeModified{ + interface CanBeModified { fun setIcon(icon: IconSource?) - fun setTitle(title:String) + fun setTitle(title: String) } interface HasEnable { //compose aware property val isEnabled: StateFlow } - 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 = MutableStateFlow(title) private val _icon: MutableStateFlow = MutableStateFlow(icon) private val _isEnabled: MutableStateFlow = MutableStateFlow(true) override val title: StateFlow = _title.asStateFlow() - override val icon: StateFlow = MutableStateFlow(icon) - override val isEnabled: StateFlow = MutableStateFlow(true) + override val icon: StateFlow = _icon.asStateFlow() + override val isEnabled: StateFlow = _isEnabled.asStateFlow() override fun setEnabled(boolean: Boolean) { _isEnabled.update { boolean } @@ -88,8 +88,8 @@ sealed interface MenuItem { override var icon: StateFlow = _icon.asStateFlow() override var title: StateFlow = _title.asStateFlow() - val items:StateFlow> = _items.asStateFlow() - fun setItems(newItems:List){ + val items: StateFlow> = _items.asStateFlow() + fun setItems(newItems: List) { _items.update { newItems } }