Update moxy
This commit is contained in:
@@ -1,22 +1,7 @@
|
||||
package org.koitharu.kotatsu.ui.common
|
||||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import moxy.MvpPresenter
|
||||
import moxy.MvpView
|
||||
import org.koin.core.KoinComponent
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
abstract class BasePresenter<V : MvpView> : MvpPresenter<V>(), KoinComponent, CoroutineScope {
|
||||
|
||||
private val job = SupervisorJob()
|
||||
|
||||
override val coroutineContext: CoroutineContext
|
||||
get() = Dispatchers.Main + job
|
||||
|
||||
override fun onDestroy() {
|
||||
job.cancel()
|
||||
super.onDestroy()
|
||||
}
|
||||
}
|
||||
abstract class BasePresenter<V : MvpView> : MvpPresenter<V>(), KoinComponent
|
||||
@@ -22,7 +22,7 @@ class ChaptersFragment : BaseFragment(R.layout.fragment_chapters), MangaDetailsV
|
||||
OnRecyclerItemClickListener<MangaChapter> {
|
||||
|
||||
@Suppress("unused")
|
||||
private val presenter by moxyPresenter { (activity as MangaDetailsActivity).presenter }
|
||||
private val presenter by moxyPresenter(factory = MangaDetailsPresenter.Companion::getInstance)
|
||||
|
||||
private var manga: Manga? = null
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ import org.koitharu.kotatsu.utils.ext.getDisplayMessage
|
||||
|
||||
class MangaDetailsActivity : BaseActivity(), MangaDetailsView {
|
||||
|
||||
val presenter by moxyPresenter(factory = ::MangaDetailsPresenter)
|
||||
private val presenter by moxyPresenter(factory = MangaDetailsPresenter.Companion::getInstance)
|
||||
|
||||
private var manga: Manga? = null
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ import kotlin.math.roundToInt
|
||||
class MangaDetailsFragment : BaseFragment(R.layout.fragment_details), MangaDetailsView {
|
||||
|
||||
@Suppress("unused")
|
||||
private val presenter by moxyPresenter { (activity as MangaDetailsActivity).presenter }
|
||||
private val presenter by moxyPresenter(factory = MangaDetailsPresenter.Companion::getInstance)
|
||||
|
||||
private var manga: Manga? = null
|
||||
private var history: MangaHistory? = null
|
||||
|
||||
@@ -4,6 +4,7 @@ import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import moxy.InjectViewState
|
||||
import moxy.presenterScope
|
||||
import org.koitharu.kotatsu.BuildConfig
|
||||
import org.koitharu.kotatsu.core.model.Manga
|
||||
import org.koitharu.kotatsu.domain.MangaProviderFactory
|
||||
@@ -14,7 +15,7 @@ import org.koitharu.kotatsu.domain.history.OnHistoryChangeListener
|
||||
import org.koitharu.kotatsu.ui.common.BasePresenter
|
||||
|
||||
@InjectViewState
|
||||
class MangaDetailsPresenter : BasePresenter<MangaDetailsView>(), OnHistoryChangeListener,
|
||||
class MangaDetailsPresenter private constructor(): BasePresenter<MangaDetailsView>(), OnHistoryChangeListener,
|
||||
OnFavouritesChangeListener {
|
||||
|
||||
private lateinit var historyRepository: HistoryRepository
|
||||
@@ -37,7 +38,7 @@ class MangaDetailsPresenter : BasePresenter<MangaDetailsView>(), OnHistoryChange
|
||||
loadHistory(manga)
|
||||
viewState.onMangaUpdated(manga)
|
||||
loadFavourite(manga)
|
||||
launch {
|
||||
presenterScope.launch {
|
||||
try {
|
||||
viewState.onLoadingStateChanged(true)
|
||||
val data = withContext(Dispatchers.IO) {
|
||||
@@ -57,7 +58,7 @@ class MangaDetailsPresenter : BasePresenter<MangaDetailsView>(), OnHistoryChange
|
||||
}
|
||||
|
||||
private fun loadHistory(manga: Manga) {
|
||||
launch {
|
||||
presenterScope.launch {
|
||||
try {
|
||||
val history = withContext(Dispatchers.IO) {
|
||||
historyRepository.getOne(manga)
|
||||
@@ -72,7 +73,7 @@ class MangaDetailsPresenter : BasePresenter<MangaDetailsView>(), OnHistoryChange
|
||||
}
|
||||
|
||||
private fun loadFavourite(manga: Manga) {
|
||||
launch {
|
||||
presenterScope.launch {
|
||||
try {
|
||||
val categories = withContext(Dispatchers.IO) {
|
||||
favouritesRepository.getCategories(manga.id)
|
||||
@@ -99,6 +100,18 @@ class MangaDetailsPresenter : BasePresenter<MangaDetailsView>(), OnHistoryChange
|
||||
override fun onDestroy() {
|
||||
HistoryRepository.unsubscribe(this)
|
||||
FavouritesRepository.unsubscribe(this)
|
||||
instance = null
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
private var instance: MangaDetailsPresenter? = null
|
||||
|
||||
fun getInstance(): MangaDetailsPresenter = instance ?: synchronized(this) {
|
||||
MangaDetailsPresenter().also {
|
||||
instance = it
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import moxy.InjectViewState
|
||||
import moxy.presenterScope
|
||||
import org.koitharu.kotatsu.BuildConfig
|
||||
import org.koitharu.kotatsu.domain.favourites.FavouritesRepository
|
||||
import org.koitharu.kotatsu.ui.common.BasePresenter
|
||||
@@ -20,7 +21,7 @@ class FavouritesListPresenter : BasePresenter<MangaListView<Unit>>() {
|
||||
}
|
||||
|
||||
fun loadList(offset: Int) {
|
||||
launch {
|
||||
presenterScope.launch {
|
||||
viewState.onLoadingChanged(true)
|
||||
try {
|
||||
val list = withContext(Dispatchers.IO) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import moxy.InjectViewState
|
||||
import moxy.presenterScope
|
||||
import org.koitharu.kotatsu.BuildConfig
|
||||
import org.koitharu.kotatsu.core.model.Manga
|
||||
import org.koitharu.kotatsu.domain.favourites.FavouritesRepository
|
||||
@@ -21,7 +22,7 @@ class FavouriteCategoriesPresenter : BasePresenter<FavouriteCategoriesView>() {
|
||||
}
|
||||
|
||||
fun loadAllCategories() {
|
||||
launch {
|
||||
presenterScope.launch {
|
||||
try {
|
||||
val categories = withContext(Dispatchers.IO) {
|
||||
repository.getAllCategories()
|
||||
@@ -37,7 +38,7 @@ class FavouriteCategoriesPresenter : BasePresenter<FavouriteCategoriesView>() {
|
||||
}
|
||||
|
||||
fun loadMangaCategories(manga: Manga) {
|
||||
launch {
|
||||
presenterScope.launch {
|
||||
try {
|
||||
val categories = withContext(Dispatchers.IO) {
|
||||
repository.getCategories(manga.id)
|
||||
@@ -53,7 +54,7 @@ class FavouriteCategoriesPresenter : BasePresenter<FavouriteCategoriesView>() {
|
||||
}
|
||||
|
||||
fun createCategory(name: String) {
|
||||
launch {
|
||||
presenterScope.launch {
|
||||
try {
|
||||
val categories = withContext(Dispatchers.IO) {
|
||||
repository.addCategory(name)
|
||||
@@ -70,7 +71,7 @@ class FavouriteCategoriesPresenter : BasePresenter<FavouriteCategoriesView>() {
|
||||
}
|
||||
|
||||
fun addToCategory(manga: Manga, categoryId: Long) {
|
||||
launch {
|
||||
presenterScope.launch {
|
||||
try {
|
||||
val categories = withContext(Dispatchers.IO) {
|
||||
repository.addToCategory(manga,categoryId)
|
||||
@@ -85,7 +86,7 @@ class FavouriteCategoriesPresenter : BasePresenter<FavouriteCategoriesView>() {
|
||||
}
|
||||
|
||||
fun removeFromCategory(manga: Manga, categoryId: Long) {
|
||||
launch {
|
||||
presenterScope.launch {
|
||||
try {
|
||||
val categories = withContext(Dispatchers.IO) {
|
||||
repository.removeFromCategory(manga, categoryId)
|
||||
|
||||
@@ -4,6 +4,7 @@ import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import moxy.InjectViewState
|
||||
import moxy.presenterScope
|
||||
import org.koitharu.kotatsu.BuildConfig
|
||||
import org.koitharu.kotatsu.core.model.Manga
|
||||
import org.koitharu.kotatsu.core.model.MangaHistory
|
||||
@@ -22,7 +23,7 @@ class HistoryListPresenter : BasePresenter<MangaListView<MangaHistory>>() {
|
||||
}
|
||||
|
||||
fun loadList(offset: Int) {
|
||||
launch {
|
||||
presenterScope.launch {
|
||||
viewState.onLoadingChanged(true)
|
||||
try {
|
||||
val list = withContext(Dispatchers.IO) {
|
||||
@@ -45,7 +46,7 @@ class HistoryListPresenter : BasePresenter<MangaListView<MangaHistory>>() {
|
||||
}
|
||||
|
||||
fun clearHistory() {
|
||||
launch {
|
||||
presenterScope.launch {
|
||||
viewState.onLoadingChanged(true)
|
||||
try {
|
||||
withContext(Dispatchers.IO) {
|
||||
@@ -64,7 +65,7 @@ class HistoryListPresenter : BasePresenter<MangaListView<MangaHistory>>() {
|
||||
}
|
||||
|
||||
fun removeFromHistory(manga: Manga) {
|
||||
launch {
|
||||
presenterScope.launch {
|
||||
try {
|
||||
withContext(Dispatchers.IO) {
|
||||
repository.delete(manga)
|
||||
|
||||
@@ -7,6 +7,7 @@ import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import moxy.InjectViewState
|
||||
import moxy.presenterScope
|
||||
import org.koitharu.kotatsu.BuildConfig
|
||||
import org.koitharu.kotatsu.core.exceptions.UnsupportedFileException
|
||||
import org.koitharu.kotatsu.core.model.Manga
|
||||
@@ -33,7 +34,7 @@ class LocalListPresenter : BasePresenter<MangaListView<File>>() {
|
||||
}
|
||||
|
||||
fun loadList() {
|
||||
launch {
|
||||
presenterScope.launch {
|
||||
viewState.onLoadingChanged(true)
|
||||
try {
|
||||
val list = withContext(Dispatchers.IO) {
|
||||
@@ -53,7 +54,7 @@ class LocalListPresenter : BasePresenter<MangaListView<File>>() {
|
||||
}
|
||||
|
||||
fun importFile(context: Context, uri: Uri) {
|
||||
launch(Dispatchers.IO) {
|
||||
presenterScope.launch(Dispatchers.IO) {
|
||||
try {
|
||||
val name = MediaStoreCompat.getName(context, uri)
|
||||
?: throw IOException("Cannot fetch name from uri: $uri")
|
||||
@@ -84,7 +85,7 @@ class LocalListPresenter : BasePresenter<MangaListView<File>>() {
|
||||
}
|
||||
|
||||
fun delete(manga: Manga) {
|
||||
launch {
|
||||
presenterScope.launch {
|
||||
try {
|
||||
withContext(Dispatchers.IO) {
|
||||
repository.delete(manga) || throw IOException("Unable to delete file")
|
||||
|
||||
@@ -4,6 +4,7 @@ import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import moxy.InjectViewState
|
||||
import moxy.presenterScope
|
||||
import org.koitharu.kotatsu.BuildConfig
|
||||
import org.koitharu.kotatsu.core.model.MangaFilter
|
||||
import org.koitharu.kotatsu.core.model.MangaSource
|
||||
@@ -18,7 +19,7 @@ class RemoteListPresenter : BasePresenter<MangaListView<Unit>>() {
|
||||
private var filter: MangaFilter? = null
|
||||
|
||||
fun loadList(source: MangaSource, offset: Int) {
|
||||
launch {
|
||||
presenterScope.launch {
|
||||
viewState.onLoadingChanged(true)
|
||||
try {
|
||||
val list = withContext(Dispatchers.IO) {
|
||||
@@ -55,7 +56,7 @@ class RemoteListPresenter : BasePresenter<MangaListView<Unit>>() {
|
||||
|
||||
private fun loadFilter(source: MangaSource) {
|
||||
isFilterInitialized = true
|
||||
launch {
|
||||
presenterScope.launch {
|
||||
try {
|
||||
val (sorts, tags) = withContext(Dispatchers.IO) {
|
||||
val repo = MangaProviderFactory.create(source)
|
||||
|
||||
@@ -7,6 +7,7 @@ import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import moxy.InjectViewState
|
||||
import moxy.presenterScope
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import org.koitharu.kotatsu.BuildConfig
|
||||
@@ -23,7 +24,7 @@ import org.koitharu.kotatsu.utils.ext.mimeType
|
||||
class ReaderPresenter : BasePresenter<ReaderView>() {
|
||||
|
||||
fun loadChapter(state: ReaderState) {
|
||||
launch {
|
||||
presenterScope.launch {
|
||||
viewState.onLoadingStateChanged(isLoading = true)
|
||||
try {
|
||||
val pages = withContext(Dispatchers.IO) {
|
||||
@@ -46,7 +47,7 @@ class ReaderPresenter : BasePresenter<ReaderView>() {
|
||||
}
|
||||
|
||||
fun saveState(state: ReaderState) {
|
||||
launch(Dispatchers.IO) {
|
||||
presenterScope.launch(Dispatchers.IO) {
|
||||
HistoryRepository().addOrUpdate(
|
||||
manga = state.manga,
|
||||
chapterId = state.chapterId,
|
||||
@@ -56,7 +57,7 @@ class ReaderPresenter : BasePresenter<ReaderView>() {
|
||||
}
|
||||
|
||||
fun savePage(resolver: ContentResolver, page: MangaPage) {
|
||||
launch(Dispatchers.IO) {
|
||||
presenterScope.launch(Dispatchers.IO) {
|
||||
try {
|
||||
val repo = MangaProviderFactory.create(page.source)
|
||||
val url = repo.getPageFullUrl(page)
|
||||
|
||||
@@ -4,6 +4,7 @@ import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import moxy.InjectViewState
|
||||
import moxy.presenterScope
|
||||
import org.koitharu.kotatsu.BuildConfig
|
||||
import org.koitharu.kotatsu.core.model.MangaSource
|
||||
import org.koitharu.kotatsu.domain.MangaProviderFactory
|
||||
@@ -21,7 +22,7 @@ class SearchPresenter : BasePresenter<MangaListView<Unit>>() {
|
||||
}
|
||||
|
||||
fun loadList(query: String, offset: Int) {
|
||||
launch {
|
||||
presenterScope.launch {
|
||||
viewState.onLoadingChanged(true)
|
||||
try {
|
||||
//TODO select source
|
||||
|
||||
Reference in New Issue
Block a user