MVVM for SourceRepository

This commit is contained in:
Syer10
2022-11-04 20:39:29 -04:00
parent 0bb8ab4fcd
commit 4e692d6007
13 changed files with 349 additions and 49 deletions

View File

@@ -0,0 +1,33 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package ca.gosyer.jui.domain.source.interactor
import ca.gosyer.jui.domain.source.model.Source
import ca.gosyer.jui.domain.source.service.SourceRepository
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.singleOrNull
import me.tatarka.inject.annotations.Inject
import org.lighthousegames.logging.logging
class GetFilterList @Inject constructor(private val sourceRepository: SourceRepository) {
suspend fun await(source: Source, reset: Boolean) = asFlow(source.id, reset)
.catch { log.warn(it) { "Failed to get filter list for ${source.displayName} with reset = $reset" } }
.singleOrNull()
suspend fun await(sourceId: Long, reset: Boolean) = asFlow(sourceId, reset)
.catch { log.warn(it) { "Failed to get filter list for $sourceId with reset = $reset" } }
.singleOrNull()
fun asFlow(source: Source, reset: Boolean) = sourceRepository.getFilterList(source.id, reset)
fun asFlow(sourceId: Long, reset: Boolean) = sourceRepository.getFilterList(sourceId, reset)
companion object {
private val log = logging()
}
}

View File

@@ -0,0 +1,33 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package ca.gosyer.jui.domain.source.interactor
import ca.gosyer.jui.domain.source.model.Source
import ca.gosyer.jui.domain.source.service.SourceRepository
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.singleOrNull
import me.tatarka.inject.annotations.Inject
import org.lighthousegames.logging.logging
class GetLatestManga @Inject constructor(private val sourceRepository: SourceRepository) {
suspend fun await(source: Source, page: Int) = asFlow(source.id, page)
.catch { log.warn(it) { "Failed to get latest manga from ${source.displayName} on page $page" } }
.singleOrNull()
suspend fun await(sourceId: Long, page: Int) = asFlow(sourceId, page)
.catch { log.warn(it) { "Failed to get latest manga from $sourceId on page $page" } }
.singleOrNull()
fun asFlow(source: Source, page: Int) = sourceRepository.getLatestManga(source.id, page)
fun asFlow(sourceId: Long, page: Int) = sourceRepository.getLatestManga(sourceId, page)
companion object {
private val log = logging()
}
}

View File

@@ -0,0 +1,33 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package ca.gosyer.jui.domain.source.interactor
import ca.gosyer.jui.domain.source.model.Source
import ca.gosyer.jui.domain.source.service.SourceRepository
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.singleOrNull
import me.tatarka.inject.annotations.Inject
import org.lighthousegames.logging.logging
class GetPopularManga @Inject constructor(private val sourceRepository: SourceRepository) {
suspend fun await(source: Source, page: Int) = asFlow(source.id, page)
.catch { log.warn(it) { "Failed to get popular manga from ${source.displayName} on page $page" } }
.singleOrNull()
suspend fun await(sourceId: Long, page: Int) = asFlow(sourceId, page)
.catch { log.warn(it) { "Failed to get popular manga from $sourceId on page $page" } }
.singleOrNull()
fun asFlow(source: Source, page: Int) = sourceRepository.getPopularManga(source.id, page)
fun asFlow(sourceId: Long, page: Int) = sourceRepository.getPopularManga(sourceId, page)
companion object {
private val log = logging()
}
}

View File

@@ -0,0 +1,41 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package ca.gosyer.jui.domain.source.interactor
import ca.gosyer.jui.domain.source.model.Source
import ca.gosyer.jui.domain.source.service.SourceRepository
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.singleOrNull
import me.tatarka.inject.annotations.Inject
import org.lighthousegames.logging.logging
class GetSearchManga @Inject constructor(private val sourceRepository: SourceRepository) {
suspend fun await(source: Source, searchTerm: String?, page: Int) = asFlow(source.id, searchTerm, page)
.catch { log.warn(it) { "Failed to get search results from ${source.displayName} on page $page with query '$searchTerm'" } }
.singleOrNull()
suspend fun await(sourceId: Long, searchTerm: String?, page: Int) = asFlow(sourceId, searchTerm, page)
.catch { log.warn(it) { "Failed to get search results from $sourceId on page $page with query '$searchTerm'" } }
.singleOrNull()
fun asFlow(source: Source, searchTerm: String?, page: Int) = sourceRepository.getSearchResults(
source.id,
searchTerm?.ifBlank { null },
page
)
fun asFlow(sourceId: Long, searchTerm: String?, page: Int) = sourceRepository.getSearchResults(
sourceId,
searchTerm?.ifBlank { null },
page
)
companion object {
private val log = logging()
}
}

View File

@@ -0,0 +1,26 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package ca.gosyer.jui.domain.source.interactor
import ca.gosyer.jui.domain.source.service.SourceRepository
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.singleOrNull
import me.tatarka.inject.annotations.Inject
import org.lighthousegames.logging.logging
class GetSourceList @Inject constructor(private val sourceRepository: SourceRepository) {
suspend fun await() = asFlow()
.catch { log.warn(it) { "Failed to get source list" } }
.singleOrNull()
fun asFlow() = sourceRepository.getSourceList()
companion object {
private val log = logging()
}
}

View File

@@ -0,0 +1,33 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package ca.gosyer.jui.domain.source.interactor
import ca.gosyer.jui.domain.source.model.Source
import ca.gosyer.jui.domain.source.service.SourceRepository
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.singleOrNull
import me.tatarka.inject.annotations.Inject
import org.lighthousegames.logging.logging
class GetSourceSettings @Inject constructor(private val sourceRepository: SourceRepository) {
suspend fun await(source: Source) = asFlow(source.id)
.catch { log.warn(it) { "Failed to get source settings for ${source.displayName}" } }
.singleOrNull()
suspend fun await(sourceId: Long) = asFlow(sourceId)
.catch { log.warn(it) { "Failed to get source settings for $sourceId" } }
.singleOrNull()
fun asFlow(source: Source) = sourceRepository.getSourceSettings(source.id)
fun asFlow(sourceId: Long) = sourceRepository.getSourceSettings(sourceId)
companion object {
private val log = logging()
}
}

View File

@@ -0,0 +1,60 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package ca.gosyer.jui.domain.source.interactor
import ca.gosyer.jui.domain.source.model.Source
import ca.gosyer.jui.domain.source.model.sourcefilters.SourceFilterChange
import ca.gosyer.jui.domain.source.service.SourceRepository
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import me.tatarka.inject.annotations.Inject
import org.lighthousegames.logging.logging
class SetSourceFilter @Inject constructor(private val sourceRepository: SourceRepository) {
suspend fun await(source: Source, filterIndex: Int, filter: Any) = asFlow(source, filterIndex, filter)
.catch { log.warn(it) { "Failed to set filter for ${source.displayName} with index = $filterIndex and value = $filter" } }
.collect()
suspend fun await(sourceId: Long, filterIndex: Int, filter: Any) = asFlow(sourceId, filterIndex, filter)
.catch { log.warn(it) { "Failed to set filter for $sourceId with index = $filterIndex and value = $filter" } }
.collect()
suspend fun await(source: Source, filterIndex: Int, childFilterIndex: Int, filter: Any) = asFlow(source, filterIndex, childFilterIndex, filter)
.catch { log.warn(it) { "Failed to set filter for ${source.displayName} with index = $filterIndex and childIndex = $childFilterIndex and value = $filter" } }
.collect()
suspend fun await(sourceId: Long, filterIndex: Int, childFilterIndex: Int, filter: Any) = asFlow(sourceId, filterIndex, childFilterIndex, filter)
.catch { log.warn(it) { "Failed to set filter for $sourceId with index = $filterIndex and childIndex = $childFilterIndex and value = $filter" } }
.collect()
fun asFlow(source: Source, filterIndex: Int, filter: Any) = sourceRepository.setFilter(
source.id,
SourceFilterChange(filterIndex, filter)
)
fun asFlow(sourceId: Long, filterIndex: Int, filter: Any) = sourceRepository.setFilter(
sourceId,
SourceFilterChange(filterIndex, filter)
)
fun asFlow(source: Source, filterIndex: Int, childFilterIndex: Int, filter: Any) = sourceRepository.setFilter(
source.id,
SourceFilterChange(filterIndex, Json.encodeToString(SourceFilterChange(childFilterIndex, filter)))
)
fun asFlow(sourceId: Long, filterIndex: Int, childFilterIndex: Int,filter: Any) = sourceRepository.setFilter(
sourceId,
SourceFilterChange(filterIndex, Json.encodeToString(SourceFilterChange(childFilterIndex, filter)))
)
companion object {
private val log = logging()
}
}

View File

@@ -0,0 +1,40 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package ca.gosyer.jui.domain.source.interactor
import ca.gosyer.jui.domain.source.model.Source
import ca.gosyer.jui.domain.source.model.sourcepreference.SourcePreferenceChange
import ca.gosyer.jui.domain.source.service.SourceRepository
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import me.tatarka.inject.annotations.Inject
import org.lighthousegames.logging.logging
class SetSourceSetting @Inject constructor(private val sourceRepository: SourceRepository) {
suspend fun await(source: Source, settingIndex: Int, setting: Any) = asFlow(source, settingIndex, setting)
.catch { log.warn(it) { "Failed to set setting for ${source.displayName} with index = $settingIndex and value = $setting" } }
.collect()
suspend fun await(sourceId: Long, settingIndex: Int, setting: Any) = asFlow(sourceId, settingIndex, setting)
.catch { log.warn(it) { "Failed to set setting for $sourceId with index = $settingIndex and value = $setting" } }
.collect()
fun asFlow(source: Source, settingIndex: Int, setting: Any) = sourceRepository.setSourceSetting(
source.id,
SourcePreferenceChange(settingIndex, setting)
)
fun asFlow(sourceId: Long, settingIndex: Int, setting: Any) = sourceRepository.setSourceSetting(
sourceId,
SourcePreferenceChange(settingIndex, setting)
)
companion object {
private val log = logging()
}
}