Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix stop all action not disabled when nothing active #103

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand All @@ -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(),
Expand All @@ -130,6 +144,7 @@ val showDownloadList = simpleAction(
) {
appComponent.openHome()
}

/*val checkForUpdateAction = simpleAction(
title = "Check For Update",
icon = MyIcons.refresh,
Expand All @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -34,7 +35,7 @@ sealed interface MenuItem {

abstract class SingleItem(
title: String,
icon: IconSource?=null,
icon: IconSource? = null,
) : MenuItem,
ClickableItem,
ReadableItem,
Expand All @@ -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 }
Expand Down Expand Up @@ -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 }
}

Expand Down