From a93c0577acdf9fdc591087838ecb5a004d3acf81 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 24 Jan 2021 11:04:25 -0500 Subject: [PATCH] Minor cleanup --- .../data/backup/AbstractBackupRestore.kt | 7 +- .../data/library/LibraryUpdateService.kt | 12 ++-- .../eu/kanade/tachiyomi/util/CrashLogUtil.kt | 8 +-- .../tachiyomi/util/lang/RxCoroutineBridge.kt | 64 ------------------- .../util/system/ContextExtensions.kt | 10 +++ 5 files changed, 20 insertions(+), 81 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupRestore.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupRestore.kt index 87ced736a2..0bc284cb77 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupRestore.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupRestore.kt @@ -10,6 +10,7 @@ import eu.kanade.tachiyomi.data.database.models.Track import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.util.chapter.NoChaptersException +import eu.kanade.tachiyomi.util.system.createFileInCacheDir import kotlinx.coroutines.Job import uy.kohesive.injekt.injectLazy import java.io.File @@ -118,15 +119,15 @@ abstract class AbstractBackupRestore(protected val co internal fun writeErrorLog(): File { try { if (errors.isNotEmpty()) { - val destFile = File(context.externalCacheDir, "tachiyomi_restore.txt") + val file = context.createFileInCacheDir("tachiyomi_restore.txt") val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault()) - destFile.bufferedWriter().use { out -> + file.bufferedWriter().use { out -> errors.forEach { (date, message) -> out.write("[${sdf.format(date)}] $message\n") } } - return destFile + return file } } catch (e: Exception) { // Empty diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt index eb6ff78587..0714c9fb4c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt @@ -32,6 +32,7 @@ import eu.kanade.tachiyomi.util.prepUpdateCover import eu.kanade.tachiyomi.util.shouldDownloadNewChapters import eu.kanade.tachiyomi.util.storage.getUriCompat import eu.kanade.tachiyomi.util.system.acquireWakeLock +import eu.kanade.tachiyomi.util.system.createFileInCacheDir import eu.kanade.tachiyomi.util.system.isServiceRunning import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -250,13 +251,9 @@ class LibraryUpdateService( * @return an observable delivering the progress of each update. */ suspend fun updateChapterList(mangaToUpdate: List) { - // Initialize the variables holding the progress of the updates. val progressCount = AtomicInteger(0) - // List containing new updates val newUpdates = mutableListOf>>() - // List containing failed updates val failedUpdates = mutableListOf>() - // Boolean to determine if DownloadManager has downloads var hasDownloads = false mangaToUpdate @@ -420,15 +417,14 @@ class LibraryUpdateService( private fun writeErrorFile(errors: List>): File { try { if (errors.isNotEmpty()) { - val destFile = File(externalCacheDir, "tachiyomi_update_errors.txt") - - destFile.bufferedWriter().use { out -> + val file = createFileInCacheDir("tachiyomi_update_errors.txt") + file.bufferedWriter().use { out -> errors.forEach { (manga, error) -> val source = sourceManager.getOrStub(manga.source) out.write("${manga.title} ($source): $error\n") } } - return destFile + return file } } catch (e: Exception) { // Empty diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/CrashLogUtil.kt b/app/src/main/java/eu/kanade/tachiyomi/util/CrashLogUtil.kt index 751bd8cd01..dc75452f32 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/CrashLogUtil.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/CrashLogUtil.kt @@ -6,10 +6,10 @@ import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.util.storage.getUriCompat +import eu.kanade.tachiyomi.util.system.createFileInCacheDir import eu.kanade.tachiyomi.util.system.notificationBuilder import eu.kanade.tachiyomi.util.system.notificationManager import eu.kanade.tachiyomi.util.system.toast -import java.io.File import java.io.IOException class CrashLogUtil(private val context: Context) { @@ -20,11 +20,7 @@ class CrashLogUtil(private val context: Context) { fun dumpLogs() { try { - val file = File(context.externalCacheDir, "tachiyomi_crash_logs.txt") - if (file.exists()) { - file.delete() - } - file.createNewFile() + val file = context.createFileInCacheDir("tachiyomi_crash_logs.txt") Runtime.getRuntime().exec("logcat *:E -d -f ${file.absolutePath}") showNotification(file.getUriCompat(context)) diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/lang/RxCoroutineBridge.kt b/app/src/main/java/eu/kanade/tachiyomi/util/lang/RxCoroutineBridge.kt index 9a6da89b18..ce760e03d5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/lang/RxCoroutineBridge.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/lang/RxCoroutineBridge.kt @@ -5,17 +5,10 @@ import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CoroutineStart import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.channels.awaitClose -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.launch import kotlinx.coroutines.suspendCancellableCoroutine import rx.Emitter import rx.Observable -import rx.Observer -import rx.Scheduler -import rx.Single -import rx.SingleSubscriber import rx.Subscriber import rx.Subscription import kotlin.coroutines.resume @@ -25,45 +18,6 @@ import kotlin.coroutines.resumeWithException * Util functions for bridging RxJava and coroutines. Taken from TachiyomiEH/SY. */ -suspend fun Single.await(subscribeOn: Scheduler? = null): T { - return suspendCancellableCoroutine { continuation -> - val self = if (subscribeOn != null) subscribeOn(subscribeOn) else this - lateinit var sub: Subscription - sub = self.subscribe( - { - continuation.resume(it) { - sub.unsubscribe() - } - }, - { - if (!continuation.isCancelled) { - continuation.resumeWithException(it) - } - } - ) - - continuation.invokeOnCancellation { - sub.unsubscribe() - } - } -} - -suspend fun Single.await(): T = suspendCancellableCoroutine { cont -> - cont.unsubscribeOnCancellation( - subscribe( - object : SingleSubscriber() { - override fun onSuccess(t: T) { - cont.resume(t) - } - - override fun onError(error: Throwable) { - cont.resumeWithException(error) - } - } - ) - ) -} - suspend fun Observable.awaitSingle(): T = single().awaitOne() private suspend fun Observable.awaitOne(): T = suspendCancellableCoroutine { cont -> @@ -105,24 +59,6 @@ private suspend fun Observable.awaitOne(): T = suspendCancellableCoroutin internal fun CancellableContinuation.unsubscribeOnCancellation(sub: Subscription) = invokeOnCancellation { sub.unsubscribe() } -fun Observable.asFlow(): Flow = callbackFlow { - val observer = object : Observer { - override fun onNext(t: T) { - offer(t) - } - - override fun onError(e: Throwable) { - close(e) - } - - override fun onCompleted() { - close() - } - } - val subscription = subscribe(observer) - awaitClose { subscription.unsubscribe() } -} - fun runAsObservable( block: suspend () -> T, backpressureMode: Emitter.BackpressureMode = Emitter.BackpressureMode.NONE diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/system/ContextExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/system/ContextExtensions.kt index ce6e0465ac..5d5a0dbfc6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/system/ContextExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/system/ContextExtensions.kt @@ -33,6 +33,7 @@ import androidx.core.net.toUri import androidx.localbroadcastmanager.content.LocalBroadcastManager import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.util.lang.truncateCenter +import java.io.File import kotlin.math.roundToInt /** @@ -244,3 +245,12 @@ fun Context.openInBrowser(uri: Uri, @ColorInt toolbarColor: Int? = null) { toast(e.message) } } + +fun Context.createFileInCacheDir(name: String): File { + val file = File(externalCacheDir, name) + if (file.exists()) { + file.delete() + } + file.createNewFile() + return file +}