Refactor
This commit is contained in:
@@ -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() {
|
||||
|
||||
@@ -74,12 +74,12 @@ class DownloadService : BaseService() {
|
||||
}
|
||||
|
||||
private fun downloadManga(manga: Manga, chaptersIds: Set<Long>?, 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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<MainView>() {
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -55,43 +55,21 @@ class HistoryListPresenter : BasePresenter<MangaListView<MangaHistory>>() {
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -20,7 +20,7 @@ class PagesThumbnailsAdapter(onItemClickListener: OnRecyclerItemClickListener<Ma
|
||||
private val cache by inject<PagesCache>()
|
||||
|
||||
override val coroutineContext: CoroutineContext
|
||||
get() = Dispatchers.Main + job
|
||||
get() = Dispatchers.Main.immediate + job
|
||||
|
||||
override fun dispose() {
|
||||
job.cancel()
|
||||
|
||||
Reference in New Issue
Block a user