This commit is contained in:
Koitharu
2020-10-11 17:11:34 +03:00
parent e9bce8ef15
commit 4dc9df0515
8 changed files with 39 additions and 70 deletions

View File

@@ -14,7 +14,7 @@ abstract class BaseService : Service(), CoroutineScope {
private val job = SupervisorJob() private val job = SupervisorJob()
final override val coroutineContext: CoroutineContext final override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job get() = Dispatchers.Main.immediate + job
@CallSuper @CallSuper
override fun onDestroy() { override fun onDestroy() {

View File

@@ -74,12 +74,12 @@ class DownloadService : BaseService() {
} }
private fun downloadManga(manga: Manga, chaptersIds: Set<Long>?, startId: Int): Job { private fun downloadManga(manga: Manga, chaptersIds: Set<Long>?, startId: Int): Job {
return launch(Dispatchers.IO) { return launch(Dispatchers.Default) {
mutex.lock() mutex.lock()
wakeLock.acquire(TimeUnit.HOURS.toMillis(1)) wakeLock.acquire(TimeUnit.HOURS.toMillis(1))
notification.fillFrom(manga) notification.fillFrom(manga)
notification.setCancelId(startId) notification.setCancelId(startId)
withContext(Dispatchers.Main) { withContext(Dispatchers.Main.immediate) {
startForeground(DownloadNotification.NOTIFICATION_ID, notification()) startForeground(DownloadNotification.NOTIFICATION_ID, notification())
} }
val destination = settings.getStorageDir(this@DownloadService) val destination = settings.getStorageDir(this@DownloadService)
@@ -168,7 +168,7 @@ class DownloadService : BaseService() {
jobs.remove(startId) jobs.remove(startId)
output?.cleanup() output?.cleanup()
destination.sub("page.tmp").delete() destination.sub("page.tmp").delete()
withContext(Dispatchers.Main) { withContext(Dispatchers.Main.immediate) {
stopForeground(true) stopForeground(true)
notification.dismiss() notification.dismiss()
stopSelf(startId) stopSelf(startId)

View File

@@ -9,6 +9,7 @@ import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.ActionBarDrawerToggle import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
@@ -41,7 +42,7 @@ import org.koitharu.kotatsu.utils.ext.resolveDp
import java.io.Closeable import java.io.Closeable
class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedListener, class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedListener,
SharedPreferences.OnSharedPreferenceChangeListener, MainView { SharedPreferences.OnSharedPreferenceChangeListener, MainView, View.OnClickListener {
private val presenter by moxyPresenter(factory = ::MainPresenter) 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) ActionBarDrawerToggle(this, drawer, toolbar, R.string.open_menu, R.string.close_menu)
drawer.addDrawerListener(drawerToggle) drawer.addDrawerListener(drawerToggle)
supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setHomeButtonEnabled(true)
navigationView.setNavigationItemSelectedListener(this) navigationView.setNavigationItemSelectedListener(this)
settings.subscribe(this) settings.subscribe(this)
fab.imageTintList = ColorStateList.valueOf(Color.WHITE) with(fab) {
fab.isVisible = true imageTintList = ColorStateList.valueOf(Color.WHITE)
fab.setOnClickListener { isVisible = true
presenter.openLastReader() setOnClickListener(this@MainActivity)
} }
supportFragmentManager.findFragmentById(R.id.container)?.let { 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 { override fun onNavigationItemSelected(item: MenuItem): Boolean {
if (item.groupId == R.id.group_remote_sources) { if (item.groupId == R.id.group_remote_sources) {
val source = MangaSource.values().getOrNull(item.itemId) ?: return false val source = MangaSource.values().getOrNull(item.itemId) ?: return false

View File

@@ -1,11 +1,6 @@
package org.koitharu.kotatsu.ui.list 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.InjectViewState
import moxy.presenterScope
import org.koitharu.kotatsu.core.exceptions.EmptyHistoryException import org.koitharu.kotatsu.core.exceptions.EmptyHistoryException
import org.koitharu.kotatsu.domain.MangaProviderFactory import org.koitharu.kotatsu.domain.MangaProviderFactory
import org.koitharu.kotatsu.domain.history.HistoryRepository import org.koitharu.kotatsu.domain.history.HistoryRepository
@@ -16,26 +11,16 @@ import org.koitharu.kotatsu.ui.reader.ReaderState
class MainPresenter : BasePresenter<MainView>() { class MainPresenter : BasePresenter<MainView>() {
fun openLastReader() { fun openLastReader() {
presenterScope.launch { launchLoadingJob {
viewState.onLoadingStateChanged(isLoading = true) val historyRepository = HistoryRepository()
try { val manga = historyRepository.getList(0, 1).firstOrNull()
val state = withContext(Dispatchers.IO) { ?: throw EmptyHistoryException()
val repo = HistoryRepository() val history = historyRepository.getOne(manga) ?: throw EmptyHistoryException()
val manga = repo.getList(0, 1).firstOrNull() val state = ReaderState(
?: throw EmptyHistoryException() MangaProviderFactory.create(manga.source).getDetails(manga),
val history = repo.getOne(manga) ?: throw EmptyHistoryException() history.chapterId, history.page, history.scroll
ReaderState( )
MangaProviderFactory.create(manga.source).getDetails(manga), viewState.onOpenReader(state)
history.chapterId, history.page, history.scroll
)
}
viewState.onOpenReader(state)
} catch (_: CancellationException) {
} catch (e: Throwable) {
viewState.onError(e)
} finally {
viewState.onLoadingStateChanged(isLoading = false)
}
} }
} }
} }

View File

@@ -55,43 +55,21 @@ class HistoryListPresenter : BasePresenter<MangaListView<MangaHistory>>() {
} }
fun clearHistory() { fun clearHistory() {
presenterScope.launch { launchLoadingJob {
viewState.onLoadingStateChanged(true) repository.clear()
try { viewState.onListChanged(emptyList())
withContext(Dispatchers.IO) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
repository.clear() MangaShortcut.clearAppShortcuts(get())
}
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)
} }
} }
} }
fun removeFromHistory(manga: Manga) { fun removeFromHistory(manga: Manga) {
presenterScope.launch { launchJob {
try { repository.delete(manga)
withContext(Dispatchers.IO) { viewState.onItemRemoved(manga)
repository.delete(manga) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
} MangaShortcut(manga).removeAppShortcut(get())
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()
}
} }
} }
} }

View File

@@ -27,7 +27,7 @@ class PageLoader : KoinComponent, CoroutineScope, DisposableHandle {
private val convertLock = Mutex() private val convertLock = Mutex()
override val coroutineContext: CoroutineContext override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job get() = Dispatchers.Main.immediate + job
@Suppress("BlockingMethodInNonBlockingContext") @Suppress("BlockingMethodInNonBlockingContext")
suspend fun loadFile(url: String, force: Boolean): File { suspend fun loadFile(url: String, force: Boolean): File {

View File

@@ -44,7 +44,7 @@ class PageThumbnailHolder(parent: ViewGroup, private val scope: CoroutineScope)
.size(thumbSize) .size(thumbSize)
.build() .build()
).drawable ).drawable
withContext(Dispatchers.Main) { withContext(Dispatchers.Main.immediate) {
imageView_thumb.setImageDrawable(drawable) imageView_thumb.setImageDrawable(drawable)
} }
} catch (e: CancellationException) { } catch (e: CancellationException) {

View File

@@ -20,7 +20,7 @@ class PagesThumbnailsAdapter(onItemClickListener: OnRecyclerItemClickListener<Ma
private val cache by inject<PagesCache>() private val cache by inject<PagesCache>()
override val coroutineContext: CoroutineContext override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job get() = Dispatchers.Main.immediate + job
override fun dispose() { override fun dispose() {
job.cancel() job.cancel()