diff --git a/src/main/kotlin/ca/gosyer/backend/network/interactions/CategoryInteractionHandler.kt b/src/main/kotlin/ca/gosyer/backend/network/interactions/CategoryInteractionHandler.kt index 08cb133c..5093866f 100644 --- a/src/main/kotlin/ca/gosyer/backend/network/interactions/CategoryInteractionHandler.kt +++ b/src/main/kotlin/ca/gosyer/backend/network/interactions/CategoryInteractionHandler.kt @@ -18,7 +18,6 @@ import ca.gosyer.backend.network.requests.getMangaCategoriesQuery import ca.gosyer.backend.network.requests.getMangaInCategoryQuery import ca.gosyer.backend.network.requests.removeMangaFromCategoryRequest import io.ktor.client.HttpClient -import io.ktor.client.request.forms.formData import io.ktor.client.statement.HttpResponse import io.ktor.http.HttpMethod import io.ktor.http.Parameters @@ -33,17 +32,25 @@ class CategoryInteractionHandler(private val client: HttpClient): BaseInteractio ) } + suspend fun getMangaCategories(manga: Manga) = getMangaCategories(manga.id) + suspend fun addMangaToCategory(mangaId: Long, categoryId: Long) = withContext(Dispatchers.IO) { client.getRepeat( serverUrl + addMangaToCategoryQuery(mangaId, categoryId) ) } + suspend fun addMangaToCategory(manga: Manga, category: Category) = addMangaToCategory(manga.id, category.id) + suspend fun addMangaToCategory(manga: Manga, categoryId: Long) = addMangaToCategory(manga.id, categoryId) + suspend fun addMangaToCategory(mangaId: Long, category: Category) = addMangaToCategory(mangaId, category.id) suspend fun removeMangaFromCategory(mangaId: Long, categoryId: Long) = withContext(Dispatchers.IO) { client.deleteRepeat( serverUrl + removeMangaFromCategoryRequest(mangaId, categoryId) ) } + suspend fun removeMangaFromCategory(manga: Manga, category: Category) = removeMangaFromCategory(manga.id, category.id) + suspend fun removeMangaFromCategory(manga: Manga, categoryId: Long) = removeMangaFromCategory(manga.id, categoryId) + suspend fun removeMangaFromCategory(mangaId: Long, category: Category) = removeMangaFromCategory(mangaId, category.id) suspend fun getCategories() = withContext(Dispatchers.IO) { client.getRepeat>( @@ -73,16 +80,9 @@ class CategoryInteractionHandler(private val client: HttpClient): BaseInteractio } ) { method = HttpMethod.Patch - formData { - if (name != null) { - append("name", name) - } - if (isLanding != null) { - append("isLanding", isLanding.toString()) - } - } } } + suspend fun modifyCategory(category: Category, name: String? = null, isLanding: Boolean? = null) = modifyCategory(category.id, name, isLanding) suspend fun reorderCategory(categoryId: Long, to: Int, from: Int) = withContext(Dispatchers.IO) { client.submitFormRepeat( @@ -95,16 +95,19 @@ class CategoryInteractionHandler(private val client: HttpClient): BaseInteractio method = HttpMethod.Patch } } + suspend fun reorderCategory(category: Category, to: Int, from: Int) = reorderCategory(category.id, to, from) suspend fun deleteCategory(categoryId: Long) = withContext(Dispatchers.IO) { client.deleteRepeat( serverUrl + categoryDeleteRequest(categoryId) ) } + suspend fun deleteCategory(category: Category) = deleteCategory(category.id) suspend fun getMangaFromCategory(categoryId: Long) = withContext(Dispatchers.IO) { client.getRepeat>( serverUrl + getMangaInCategoryQuery(categoryId) ) } + suspend fun getMangaFromCategory(category: Category) = getMangaFromCategory(category.id) } \ No newline at end of file diff --git a/src/main/kotlin/ca/gosyer/ui/categories/CategoriesMenuViewModel.kt b/src/main/kotlin/ca/gosyer/ui/categories/CategoriesMenuViewModel.kt index 1ff1ce25..8fff950c 100644 --- a/src/main/kotlin/ca/gosyer/ui/categories/CategoriesMenuViewModel.kt +++ b/src/main/kotlin/ca/gosyer/ui/categories/CategoriesMenuViewModel.kt @@ -63,17 +63,17 @@ class CategoriesMenuViewModel : ViewModel() { originalCategories.forEach { originalCategory -> val category = categories.find { it.id == originalCategory.id } if (category == null) { - CategoryInteractionHandler(httpClient).deleteCategory(originalCategory.id) + CategoryInteractionHandler(httpClient).deleteCategory(originalCategory) } else if (category.name != originalCategory.name) { - CategoryInteractionHandler(httpClient).modifyCategory(originalCategory.id, category.name) + CategoryInteractionHandler(httpClient).modifyCategory(originalCategory, category.name) } } val updatedCategories = CategoryInteractionHandler(httpClient).getCategories() updatedCategories.forEach { updatedCategory -> val category = categories.find { it.id == updatedCategory.id || it.name == updatedCategory.name } ?: return@forEach if (category.order != updatedCategory.order) { - logger.debug { category.order.toString() + " to " + updatedCategory.order.toString() } - CategoryInteractionHandler(httpClient).reorderCategory(updatedCategory.id, category.order, updatedCategory.order) + logger.debug { "${category.order} to ${updatedCategory.order}" } + CategoryInteractionHandler(httpClient).reorderCategory(updatedCategory, category.order, updatedCategory.order) } } @@ -92,7 +92,7 @@ class CategoriesMenuViewModel : ViewModel() { } fun createCategory(name: String) { - _categories.value += MenuCategory(order = categories.value.size, name = name, landing = false) + _categories.value += MenuCategory(order = categories.value.size + 1, name = name, landing = false) } fun moveUp(category: MenuCategory) { diff --git a/src/main/kotlin/ca/gosyer/ui/library/LibraryScreenViewModel.kt b/src/main/kotlin/ca/gosyer/ui/library/LibraryScreenViewModel.kt index c0ad527a..8d74749f 100644 --- a/src/main/kotlin/ca/gosyer/ui/library/LibraryScreenViewModel.kt +++ b/src/main/kotlin/ca/gosyer/ui/library/LibraryScreenViewModel.kt @@ -63,7 +63,7 @@ class LibraryScreenViewModel: ViewModel() { library.categories.value = listOf(defaultCategory) + categories.sortedBy { it.order } categories.map { async { - library.mangaMap.setManga(it.order, CategoryInteractionHandler(httpClient).getMangaFromCategory(it.id)) + library.mangaMap.setManga(it.order, CategoryInteractionHandler(httpClient).getMangaFromCategory(it)) } }.awaitAll() val mangaInCategories = library.mangaMap.flatMap { it.value.value }.map { it.id }.distinct()