Skip to content

Commit

Permalink
Merge pull request #107 from amir1376/feature/custom-categories
Browse files Browse the repository at this point in the history
add custom categories
  • Loading branch information
amir1376 authored Oct 15, 2024
2 parents ebb2ad3 + 4622b91 commit 5613f6c
Show file tree
Hide file tree
Showing 38 changed files with 3,221 additions and 293 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.abdownloadmanager.desktop.pages.addDownload.AddDownloadConfig
import com.abdownloadmanager.desktop.pages.addDownload.multiple.AddMultiDownloadComponent
import com.abdownloadmanager.desktop.pages.addDownload.single.AddSingleDownloadComponent
import com.abdownloadmanager.desktop.pages.batchdownload.BatchDownloadComponent
import com.abdownloadmanager.desktop.pages.category.CategoryComponent
import com.abdownloadmanager.desktop.pages.category.CategoryDialogManager
import com.abdownloadmanager.desktop.pages.home.HomeComponent
import com.abdownloadmanager.desktop.pages.queue.QueuesComponent
import com.abdownloadmanager.desktop.pages.settings.SettingsComponent
Expand Down Expand Up @@ -35,6 +37,8 @@ import ir.amirab.downloader.utils.ExceptionUtils
import ir.amirab.downloader.utils.OnDuplicateStrategy
import com.abdownloadmanager.integration.Integration
import com.abdownloadmanager.integration.IntegrationResult
import com.abdownloadmanager.utils.category.CategoryManager
import com.abdownloadmanager.utils.category.CategorySelectionMode
import ir.amirab.downloader.exception.TooManyErrorException
import ir.amirab.util.osfileutil.FileUtils
import kotlinx.coroutines.flow.*
Expand All @@ -61,6 +65,7 @@ class AppComponent(
) : BaseComponent(ctx),
DownloadDialogManager,
AddDownloadDialogManager,
CategoryDialogManager,
NotificationSender,
DownloadItemOpener,
ContainsEffects<AppEffects> by supportEffects(),
Expand Down Expand Up @@ -101,7 +106,8 @@ class AppComponent(
downloadItemOpener = this,
downloadDialogManager = this,
addDownloadDialogManager = this,
notificationSender = this
categoryDialogManager = this,
notificationSender = this,
)
}
).subscribeAsStateFlow()
Expand Down Expand Up @@ -192,12 +198,25 @@ class AppComponent(
onRequestClose = {
closeAddDownloadDialog(config.id)
},
onRequestAddToQueue = { item, queueId, onDuplicate ->
addDownload(item = item, queueId = queueId, onDuplicateStrategy = onDuplicate)
onRequestAddToQueue = { item, queueId, onDuplicate, categoryId ->
addDownload(
item = item,
queueId = queueId,
categoryId = categoryId,
onDuplicateStrategy = onDuplicate,
)
closeAddDownloadDialog(dialogId = config.id)
},
onRequestDownload = { item, onDuplicate ->
startNewDownload(item, onDuplicate, true)
onRequestAddCategory = {
openCategoryDialog(-1)
},
onRequestDownload = { item, onDuplicate, categoryId ->
startNewDownload(
item = item,
onDuplicateStrategy = onDuplicate,
openDownloadDialog = true,
categoryId = categoryId,
)
closeAddDownloadDialog(config.id)
},
openExistingDownload = {
Expand All @@ -213,16 +232,20 @@ class AppComponent(

is AddDownloadConfig.MultipleAddConfig -> {
AddMultiDownloadComponent(
ctx,
config.id,
{ closeAddDownloadDialog(config.id) },
{ items, strategy, queueId ->
ctx = ctx,
id = config.id,
onRequestClose = { closeAddDownloadDialog(config.id) },
onRequestAdd = { items, strategy, queueId, categorySelectionMode ->
addDownload(
items = items,
onDuplicateStrategy = strategy,
queueId = queueId,
categorySelectionMode = categorySelectionMode
)
closeAddDownloadDialog(config.id)
},
onRequestAddCategory = {
openCategoryDialog(-1)
}
).apply { addItems(config.links) }
}
Expand Down Expand Up @@ -263,6 +286,77 @@ class AppComponent(
.map { it.items.mapNotNull { it.instance } }
.stateIn(scope, SharingStarted.Eagerly, emptyList())

private val categoryManager: CategoryManager by inject()

private val categoryPageControl = PagesNavigation<Long>()
private val _openedCategoryDialogs = childPages(
key = "openedCategoryDialogs",
source = categoryPageControl,
serializer = null,
initialPages = { Pages() },
pageStatus = { _, _ ->
ChildNavState.Status.RESUMED
},
childFactory = { cfg, ctx ->
CategoryComponent(
ctx = ctx,
close = {
closeCategoryDialog(cfg)
},
submit = { submittedCategory ->
if (submittedCategory.id < 0) {
categoryManager.addCustomCategory(submittedCategory)
} else {
categoryManager.updateCategory(
submittedCategory.id
) {
submittedCategory.copy(
items = it.items
)
}
}
closeCategoryDialog(cfg)
},
id = cfg
)
}
).subscribeAsStateFlow()
override val openedCategoryDialogs: StateFlow<List<CategoryComponent>> = _openedCategoryDialogs
.map {
it.items.mapNotNull { it.instance }
}.stateIn(scope, SharingStarted.Eagerly, emptyList())

override fun openCategoryDialog(categoryId: Long) {
scope.launch {
val component = openedCategoryDialogs.value.find {
it.id == categoryId
}
if (component != null) {
// component.bringToFront()
} else {
categoryPageControl.navigate {
val newItems = (it.items.toSet() + categoryId).toList()
val copy = it.copy(
items = newItems,
selectedIndex = newItems.lastIndex
)
copy
}
}
}
}

override fun closeCategoryDialog(categoryId: Long) {
scope.launch {
categoryPageControl.navigate {
val newItems = it.items.filter { config ->
config != categoryId
}
it.copy(items = newItems, selectedIndex = newItems.lastIndex)
}
}
}

init {
downloadSystem.downloadEvents
.filterIsInstance<DownloadManagerEvents.OnJobRemoved>()
Expand Down Expand Up @@ -518,27 +612,31 @@ class AppComponent(
fun addDownload(
items: List<DownloadItem>,
onDuplicateStrategy: (DownloadItem) -> OnDuplicateStrategy,
categorySelectionMode: CategorySelectionMode?,
queueId: Long?,
) {
scope.launch {
downloadSystem.addDownload(
newItemsToAdd = items,
onDuplicateStrategy = onDuplicateStrategy,
queueId = queueId,
categorySelectionMode = categorySelectionMode,
)
}
}

fun addDownload(
item: DownloadItem,
queueId: Long?,
categoryId: Long?,
onDuplicateStrategy: OnDuplicateStrategy,
) {
scope.launch {
downloadSystem.addDownload(
downloadItem = item,
onDuplicateStrategy = onDuplicateStrategy,
queueId = queueId,
categoryId = categoryId,
)
}
}
Expand All @@ -547,12 +645,14 @@ class AppComponent(
item: DownloadItem,
onDuplicateStrategy: OnDuplicateStrategy,
openDownloadDialog: Boolean,
categoryId: Long?,
) {
scope.launch {
val id = downloadSystem.addDownload(
item,
onDuplicateStrategy,
DefaultQueueInfo.ID,
downloadItem = item,
onDuplicateStrategy = onDuplicateStrategy,
queueId = DefaultQueueInfo.ID,
categoryId = categoryId,
)
launch {
downloadSystem.manualResume(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import ir.amirab.util.compose.action.buildMenu
import ir.amirab.util.compose.action.simpleAction
import com.abdownloadmanager.desktop.utils.getIcon
import com.abdownloadmanager.desktop.utils.getName
import com.abdownloadmanager.utils.category.Category
import ir.amirab.downloader.downloaditem.DownloadCredentials
import ir.amirab.downloader.queue.DownloadQueue
import ir.amirab.downloader.queue.activeQueuesFlow
Expand Down Expand Up @@ -207,6 +208,21 @@ fun moveToQueueAction(
}
}
}
fun createMoveToCategoryAction(
category: Category,
itemIds: List<Long>,
): AnAction {
return simpleAction(category.name) {
scope.launch {
downloadSystem
.categoryManager
.addItemsToCategory(
categoryId = category.id,
itemIds = itemIds,
)
}
}
}

fun stopQueueAction(
queue: DownloadQueue,
Expand Down
49 changes: 46 additions & 3 deletions desktop/app/src/main/kotlin/com/abdownloadmanager/desktop/di/Di.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.abdownloadmanager.desktop.pages.settings.ThemeManager
import ir.amirab.downloader.queue.QueueManager
import com.abdownloadmanager.desktop.repository.AppRepository
import com.abdownloadmanager.desktop.storage.*
import com.abdownloadmanager.desktop.ui.icon.MyIcons
import com.abdownloadmanager.desktop.utils.*
import com.abdownloadmanager.desktop.utils.native_messaging.NativeMessaging
import com.abdownloadmanager.desktop.utils.native_messaging.NativeMessagingManifestApplier
Expand All @@ -34,6 +35,10 @@ import org.koin.dsl.bind
import org.koin.dsl.module
import com.abdownloadmanager.updatechecker.DummyUpdateChecker
import com.abdownloadmanager.updatechecker.UpdateChecker
import com.abdownloadmanager.utils.FileIconProvider
import com.abdownloadmanager.utils.FileIconProviderUsingCategoryIcons
import com.abdownloadmanager.utils.category.*
import com.abdownloadmanager.utils.compose.IMyIcons
import ir.amirab.downloader.monitor.IDownloadMonitor
import ir.amirab.downloader.utils.EmptyFileCreator

Expand Down Expand Up @@ -92,7 +97,7 @@ val downloaderModule = module {

}
single {
val downloadSettings:DownloadSettings = get()
val downloadSettings: DownloadSettings = get()
EmptyFileCreator(
diskStat = get(),
useSparseFile = { downloadSettings.useSparseFileAllocation }
Expand All @@ -104,8 +109,42 @@ val downloaderModule = module {
single<IDownloadMonitor> {
DownloadMonitor(get())
}
}
val downloadSystemModule = module {
single {
CategoryFileStorage(
file = get<DownloadFoldersRegistry>().registerAndGet(
AppInfo.downloadDbDir.resolve("categories")
).resolve("categories.json"),
fileSaver = get()
)
}.bind<CategoryStorage>()
single {
FileIconProviderUsingCategoryIcons(
get(),
get(),
MyIcons,
)
}.bind<FileIconProvider>()
single {
DefaultCategories(
icons = get(),
getDefaultDownloadFolder = {
get<AppSettingsStorage>().defaultDownloadFolder.value
}
)
}
single {
DownloadSystem(get(), get(), get(), get(), get(), get())
CategoryManager(
categoryStorage = get(),
scope = get(),
defaultCategoriesFactory = get(),
downloadManager = get(),
)
}

single {
DownloadSystem(get(), get(), get(), get(), get(), get(), get())
}
}
val coroutineModule = module {
Expand Down Expand Up @@ -154,6 +193,7 @@ val nativeMessagingModule = module {

val appModule = module {
includes(downloaderModule)
includes(downloadSystemModule)
includes(coroutineModule)
includes(jsonModule)
includes(integrationModule)
Expand All @@ -167,8 +207,11 @@ val appModule = module {
AppRepository()
}
single {
ThemeManager(get(),get())
ThemeManager(get(), get())
}
single {
MyIcons
}.bind<IMyIcons>()
single {
AppSettingsStorage(
createMyConfigPreferences(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fun ShowAddDownloadDialogs(component: AddDownloadDialogManager) {
}
when (addDownloadComponent) {
is AddSingleDownloadComponent -> {
val h = 220
val h = 265
val w = 500
val size = remember {
DpSize(
Expand Down Expand Up @@ -61,7 +61,7 @@ fun ShowAddDownloadDialogs(component: AddDownloadDialogManager) {

is AddMultiDownloadComponent -> {
val h = 400
val w = 600
val w = 700
val state = rememberWindowState(
height = h.dp,
width = w.dp,
Expand Down
Loading

0 comments on commit 5613f6c

Please sign in to comment.