From 4dc9df0515c796dc1e211cb15494c1e7989498e3 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Sun, 11 Oct 2020 17:11:34 +0300 Subject: [PATCH] Refactor --- .../koitharu/kotatsu/ui/common/BaseService.kt | 2 +- .../kotatsu/ui/download/DownloadService.kt | 6 +-- .../koitharu/kotatsu/ui/list/MainActivity.kt | 18 +++++--- .../koitharu/kotatsu/ui/list/MainPresenter.kt | 35 +++++----------- .../ui/list/history/HistoryListPresenter.kt | 42 +++++-------------- .../koitharu/kotatsu/ui/reader/PageLoader.kt | 2 +- .../reader/thumbnails/PageThumbnailHolder.kt | 2 +- .../thumbnails/PagesThumbnailsAdapter.kt | 2 +- 8 files changed, 39 insertions(+), 70 deletions(-) diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/common/BaseService.kt b/app/src/main/java/org/koitharu/kotatsu/ui/common/BaseService.kt index 6d9c39a74..6ab2cd307 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/common/BaseService.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/common/BaseService.kt @@ -14,7 +14,7 @@ abstract class BaseService : Service(), CoroutineScope { private val job = SupervisorJob() final override val coroutineContext: CoroutineContext - get() = Dispatchers.Main + job + get() = Dispatchers.Main.immediate + job @CallSuper override fun onDestroy() { diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/download/DownloadService.kt b/app/src/main/java/org/koitharu/kotatsu/ui/download/DownloadService.kt index d6d03f4fb..b819f385d 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/download/DownloadService.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/download/DownloadService.kt @@ -74,12 +74,12 @@ class DownloadService : BaseService() { } private fun downloadManga(manga: Manga, chaptersIds: Set?, startId: Int): Job { - return launch(Dispatchers.IO) { + return launch(Dispatchers.Default) { mutex.lock() wakeLock.acquire(TimeUnit.HOURS.toMillis(1)) notification.fillFrom(manga) notification.setCancelId(startId) - withContext(Dispatchers.Main) { + withContext(Dispatchers.Main.immediate) { startForeground(DownloadNotification.NOTIFICATION_ID, notification()) } val destination = settings.getStorageDir(this@DownloadService) @@ -168,7 +168,7 @@ class DownloadService : BaseService() { jobs.remove(startId) output?.cleanup() destination.sub("page.tmp").delete() - withContext(Dispatchers.Main) { + withContext(Dispatchers.Main.immediate) { stopForeground(true) notification.dismiss() stopSelf(startId) diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/list/MainActivity.kt b/app/src/main/java/org/koitharu/kotatsu/ui/list/MainActivity.kt index 12673ccb1..697cceec7 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/list/MainActivity.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/list/MainActivity.kt @@ -9,6 +9,7 @@ import android.os.Build import android.os.Bundle import android.view.Menu import android.view.MenuItem +import android.view.View import androidx.appcompat.app.ActionBarDrawerToggle import androidx.core.view.isVisible import androidx.fragment.app.Fragment @@ -41,7 +42,7 @@ import org.koitharu.kotatsu.utils.ext.resolveDp import java.io.Closeable class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedListener, - SharedPreferences.OnSharedPreferenceChangeListener, MainView { + SharedPreferences.OnSharedPreferenceChangeListener, MainView, View.OnClickListener { private val presenter by moxyPresenter(factory = ::MainPresenter) @@ -56,15 +57,14 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList ActionBarDrawerToggle(this, drawer, toolbar, R.string.open_menu, R.string.close_menu) drawer.addDrawerListener(drawerToggle) supportActionBar?.setDisplayHomeAsUpEnabled(true) - supportActionBar?.setHomeButtonEnabled(true) navigationView.setNavigationItemSelectedListener(this) settings.subscribe(this) - fab.imageTintList = ColorStateList.valueOf(Color.WHITE) - fab.isVisible = true - fab.setOnClickListener { - presenter.openLastReader() + with(fab) { + imageTintList = ColorStateList.valueOf(Color.WHITE) + isVisible = true + setOnClickListener(this@MainActivity) } supportFragmentManager.findFragmentById(R.id.container)?.let { @@ -119,6 +119,12 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList } } + override fun onClick(v: View) { + when (v.id) { + R.id.fab -> presenter.openLastReader() + } + } + override fun onNavigationItemSelected(item: MenuItem): Boolean { if (item.groupId == R.id.group_remote_sources) { val source = MangaSource.values().getOrNull(item.itemId) ?: return false diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/list/MainPresenter.kt b/app/src/main/java/org/koitharu/kotatsu/ui/list/MainPresenter.kt index 74428726d..6cab9febb 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/list/MainPresenter.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/list/MainPresenter.kt @@ -1,11 +1,6 @@ package org.koitharu.kotatsu.ui.list -import kotlinx.coroutines.CancellationException -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext import moxy.InjectViewState -import moxy.presenterScope import org.koitharu.kotatsu.core.exceptions.EmptyHistoryException import org.koitharu.kotatsu.domain.MangaProviderFactory import org.koitharu.kotatsu.domain.history.HistoryRepository @@ -16,26 +11,16 @@ import org.koitharu.kotatsu.ui.reader.ReaderState class MainPresenter : BasePresenter() { fun openLastReader() { - presenterScope.launch { - viewState.onLoadingStateChanged(isLoading = true) - try { - val state = withContext(Dispatchers.IO) { - val repo = HistoryRepository() - val manga = repo.getList(0, 1).firstOrNull() - ?: throw EmptyHistoryException() - val history = repo.getOne(manga) ?: throw EmptyHistoryException() - ReaderState( - MangaProviderFactory.create(manga.source).getDetails(manga), - history.chapterId, history.page, history.scroll - ) - } - viewState.onOpenReader(state) - } catch (_: CancellationException) { - } catch (e: Throwable) { - viewState.onError(e) - } finally { - viewState.onLoadingStateChanged(isLoading = false) - } + launchLoadingJob { + val historyRepository = HistoryRepository() + val manga = historyRepository.getList(0, 1).firstOrNull() + ?: throw EmptyHistoryException() + val history = historyRepository.getOne(manga) ?: throw EmptyHistoryException() + val state = ReaderState( + MangaProviderFactory.create(manga.source).getDetails(manga), + history.chapterId, history.page, history.scroll + ) + viewState.onOpenReader(state) } } } \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/list/history/HistoryListPresenter.kt b/app/src/main/java/org/koitharu/kotatsu/ui/list/history/HistoryListPresenter.kt index f3d5cca1d..f8532630a 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/list/history/HistoryListPresenter.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/list/history/HistoryListPresenter.kt @@ -55,43 +55,21 @@ class HistoryListPresenter : BasePresenter>() { } fun clearHistory() { - presenterScope.launch { - viewState.onLoadingStateChanged(true) - try { - withContext(Dispatchers.IO) { - repository.clear() - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { - MangaShortcut.clearAppShortcuts(get()) - } - viewState.onListChanged(emptyList()) - } catch (_: CancellationException) { - } catch (e: Throwable) { - if (BuildConfig.DEBUG) { - e.printStackTrace() - } - viewState.onError(e) - } finally { - viewState.onLoadingStateChanged(false) + launchLoadingJob { + repository.clear() + viewState.onListChanged(emptyList()) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { + MangaShortcut.clearAppShortcuts(get()) } } } fun removeFromHistory(manga: Manga) { - presenterScope.launch { - try { - withContext(Dispatchers.IO) { - repository.delete(manga) - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { - MangaShortcut(manga).removeAppShortcut(get()) - } - viewState.onItemRemoved(manga) - } catch (_: CancellationException) { - } catch (e: Throwable) { - if (BuildConfig.DEBUG) { - e.printStackTrace() - } + launchJob { + repository.delete(manga) + viewState.onItemRemoved(manga) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { + MangaShortcut(manga).removeAppShortcut(get()) } } } diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/reader/PageLoader.kt b/app/src/main/java/org/koitharu/kotatsu/ui/reader/PageLoader.kt index 6d9724dd3..37f9c2640 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/reader/PageLoader.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/reader/PageLoader.kt @@ -27,7 +27,7 @@ class PageLoader : KoinComponent, CoroutineScope, DisposableHandle { private val convertLock = Mutex() override val coroutineContext: CoroutineContext - get() = Dispatchers.Main + job + get() = Dispatchers.Main.immediate + job @Suppress("BlockingMethodInNonBlockingContext") suspend fun loadFile(url: String, force: Boolean): File { diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/reader/thumbnails/PageThumbnailHolder.kt b/app/src/main/java/org/koitharu/kotatsu/ui/reader/thumbnails/PageThumbnailHolder.kt index 6cbcfe144..c881a11b7 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/reader/thumbnails/PageThumbnailHolder.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/reader/thumbnails/PageThumbnailHolder.kt @@ -44,7 +44,7 @@ class PageThumbnailHolder(parent: ViewGroup, private val scope: CoroutineScope) .size(thumbSize) .build() ).drawable - withContext(Dispatchers.Main) { + withContext(Dispatchers.Main.immediate) { imageView_thumb.setImageDrawable(drawable) } } catch (e: CancellationException) { diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/reader/thumbnails/PagesThumbnailsAdapter.kt b/app/src/main/java/org/koitharu/kotatsu/ui/reader/thumbnails/PagesThumbnailsAdapter.kt index 8daf0f9ab..19b248c93 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/reader/thumbnails/PagesThumbnailsAdapter.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/reader/thumbnails/PagesThumbnailsAdapter.kt @@ -20,7 +20,7 @@ class PagesThumbnailsAdapter(onItemClickListener: OnRecyclerItemClickListener() override val coroutineContext: CoroutineContext - get() = Dispatchers.Main + job + get() = Dispatchers.Main.immediate + job override fun dispose() { job.cancel()