From 9b447c3a9dbfc3b5342014217795a35d2d4e05de Mon Sep 17 00:00:00 2001 From: shabinder Date: Tue, 22 Jun 2021 00:30:28 +0530 Subject: [PATCH] SuspendableEvent and other Deprecated Code Fixes --- .../com/shabinder/spotiflyer/MainActivity.kt | 94 ++++++++++--------- .../shabinder/common/models/event/Event.kt | 2 +- .../event/coroutines/SuspendableEvent.kt | 6 +- .../shabinder/common/di/saavn/JsonUtils.kt | 4 +- .../common/di/utils/ParallelExecutor.kt | 6 +- .../list/store/SpotiFlyerListStoreProvider.kt | 4 +- 6 files changed, 61 insertions(+), 55 deletions(-) diff --git a/android/src/main/java/com/shabinder/spotiflyer/MainActivity.kt b/android/src/main/java/com/shabinder/spotiflyer/MainActivity.kt index 20382daa..1344566c 100644 --- a/android/src/main/java/com/shabinder/spotiflyer/MainActivity.kt +++ b/android/src/main/java/com/shabinder/spotiflyer/MainActivity.kt @@ -86,6 +86,7 @@ class MainActivity : ComponentActivity() { private lateinit var queryReceiver: BroadcastReceiver private val internetAvailability by lazy { ConnectionLiveData(applicationContext) } private val tracker get() = (application as App).tracker + private val visibleChild get(): SpotiFlyerRoot.Child = root.routerState.value.activeChild.instance override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -149,9 +150,7 @@ class MainActivity : ComponentActivity() { } private fun initialise() { - val isGithubRelease = checkAppSignature(this).also { - Log.i("SpotiFlyer Github Rel.:",it.toString()) - } + val isGithubRelease = checkAppSignature(this) /* * Only Send an `Update Notification` on Github Release Builds * and Track Downloads for all other releases like F-Droid, @@ -170,42 +169,6 @@ class MainActivity : ComponentActivity() { return internetAvailability.observeAsState() } - @Suppress("DEPRECATION") - private fun setUpOnPrefClickListener() { - // Initialize Builder - val chooser = StorageChooser.Builder() - .withActivity(this) - .withFragmentManager(fragmentManager) - .withMemoryBar(true) - .setTheme(StorageChooser.Theme(applicationContext).apply { - scheme = applicationContext.resources.getIntArray(R.array.default_dark) - }) - .setDialogTitle("Set Download Directory") - .allowCustomPath(true) - .setType(StorageChooser.DIRECTORY_CHOOSER) - .build() - - // get path that the user has chosen - chooser.setOnSelectListener { path -> - Log.d("Setting Base Path", path) - val f = File(path) - if (f.canWrite()) { - // hell yeah :) - dir.setDownloadDirectory(path) - showPopUpMessage( - "Download Directory Set to:\n${dir.defaultDir()} " - ) - }else{ - showPopUpMessage( - "NO WRITE ACCESS on \n$path ,\nReverting Back to Previous" - ) - } - } - - // Show dialog whenever you want by - chooser.show() - } - private fun showPopUpMessage(string: String, long: Boolean = false) { android.widget.Toast.makeText( applicationContext, @@ -256,12 +219,7 @@ class MainActivity : ComponentActivity() { override fun setDownloadDirectoryAction() = setUpOnPrefClickListener() - override fun queryActiveTracks() { - val serviceIntent = Intent(this@MainActivity, ForegroundService::class.java).apply { - action = "query" - } - ContextCompat.startForegroundService(this@MainActivity, serviceIntent) - } + override fun queryActiveTracks() = this@MainActivity.queryActiveTracks() override fun giveDonation() { openPlatform("",platformLink = "https://razorpay.com/payment-button/pl_GnKuuDBdBu0ank/view/?utm_source=payment_button&utm_medium=button&utm_campaign=payment_button") @@ -337,6 +295,48 @@ class MainActivity : ComponentActivity() { } ) + private fun queryActiveTracks() { + val serviceIntent = Intent(this@MainActivity, ForegroundService::class.java).apply { + action = "query" + } + ContextCompat.startForegroundService(this@MainActivity, serviceIntent) + } + + @Suppress("DEPRECATION") + private fun setUpOnPrefClickListener() { + // Initialize Builder + val chooser = StorageChooser.Builder() + .withActivity(this) + .withFragmentManager(fragmentManager) + .withMemoryBar(true) + .setTheme(StorageChooser.Theme(applicationContext).apply { + scheme = applicationContext.resources.getIntArray(R.array.default_dark) + }) + .setDialogTitle("Set Download Directory") + .allowCustomPath(true) + .setType(StorageChooser.DIRECTORY_CHOOSER) + .build() + + // get path that the user has chosen + chooser.setOnSelectListener { path -> + Log.d("Setting Base Path", path) + val f = File(path) + if (f.canWrite()) { + // hell yeah :) + dir.setDownloadDirectory(path) + showPopUpMessage( + "Download Directory Set to:\n${dir.defaultDir()} " + ) + }else{ + showPopUpMessage( + "NO WRITE ACCESS on \n$path ,\nReverting Back to Previous" + ) + } + } + + // Show dialog whenever you want by + chooser.show() + } @SuppressLint("ObsoleteSdkInt") override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { @@ -418,6 +418,10 @@ class MainActivity : ComponentActivity() { override fun onResume() { super.onResume() initializeBroadcast() + if(visibleChild is SpotiFlyerRoot.Child.List) { + // Update Track List Statuses when Returning to App + queryActiveTracks() + } } override fun onPause() { diff --git a/common/data-models/src/commonMain/kotlin/com/shabinder/common/models/event/Event.kt b/common/data-models/src/commonMain/kotlin/com/shabinder/common/models/event/Event.kt index 76cfb021..0997ab96 100644 --- a/common/data-models/src/commonMain/kotlin/com/shabinder/common/models/event/Event.kt +++ b/common/data-models/src/commonMain/kotlin/com/shabinder/common/models/event/Event.kt @@ -161,7 +161,7 @@ sealed class Event: ReadOnlyProperty { class Failure(val error: E) : Event() { override fun component2(): E = error - override val value: Nothing = throw error + override val value: Nothing get() = throw error fun getThrowable(): E = error diff --git a/common/data-models/src/commonMain/kotlin/com/shabinder/common/models/event/coroutines/SuspendableEvent.kt b/common/data-models/src/commonMain/kotlin/com/shabinder/common/models/event/coroutines/SuspendableEvent.kt index 0925d06d..bd6485b2 100644 --- a/common/data-models/src/commonMain/kotlin/com/shabinder/common/models/event/coroutines/SuspendableEvent.kt +++ b/common/data-models/src/commonMain/kotlin/com/shabinder/common/models/event/coroutines/SuspendableEvent.kt @@ -134,7 +134,7 @@ sealed class SuspendableEvent: ReadOnlyProperty override fun component1(): V? = null override fun component2(): E? = error - override val value: V = throw error + override val value: V get() = throw error fun getThrowable(): E = error @@ -158,7 +158,9 @@ sealed class SuspendableEvent: ReadOnlyProperty return value?.let { Success(it) } ?: error(fail()) } - suspend inline fun of(crossinline block: suspend () -> V): SuspendableEvent = try { + suspend inline fun of( + crossinline block: suspend () -> V + ): SuspendableEvent = try { Success(block()) } catch (ex: Throwable) { Failure(ex as E) diff --git a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/saavn/JsonUtils.kt b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/saavn/JsonUtils.kt index eb845e0e..367f39a1 100644 --- a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/saavn/JsonUtils.kt +++ b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/saavn/JsonUtils.kt @@ -6,7 +6,7 @@ package com.shabinder.common.di.saavn fun String.escape(): String { val output = StringBuilder() for (element in this) { - val chx = element.toInt() + val chx = element.code if (chx != 0) { when (element) { '\n' -> { @@ -76,7 +76,7 @@ fun String.unescape(): String { /*if (!x.isLetterOrDigit()) { throw RuntimeException("Bad character in unicode escape.") }*/ - hex.append(x.toLowerCase()) + hex.append(x.lowercaseChar()) } i += 4 // consume those four digits. val code = hex.toString().toInt(16) diff --git a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/utils/ParallelExecutor.kt b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/utils/ParallelExecutor.kt index 13aa686a..7fa50889 100644 --- a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/utils/ParallelExecutor.kt +++ b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/utils/ParallelExecutor.kt @@ -22,7 +22,7 @@ package com.shabinder.common.di.utils // Gist: https://gist.github.com/fluidsonic/ba32de21c156bbe8424c8d5fc20dcd8e import com.shabinder.common.di.dispatcherIO -import io.ktor.utils.io.core.Closeable +import io.ktor.utils.io.core.* import kotlinx.atomicfu.atomic import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CompletableDeferred @@ -96,7 +96,7 @@ class ParallelExecutor( return var change = expectedCount - actualCount - while (change > 0 && killQueue.poll() != null) + while (change > 0 && killQueue.tryReceive().getOrNull() != null) change -= 1 if (change > 0) @@ -104,7 +104,7 @@ class ParallelExecutor( repeat(change) { launchProcessor() } } else - repeat(-change) { killQueue.offer(Unit) } + repeat(-change) { killQueue.trySend(Unit).isSuccess } } private class Operation( diff --git a/common/list/src/commonMain/kotlin/com/shabinder/common/list/store/SpotiFlyerListStoreProvider.kt b/common/list/src/commonMain/kotlin/com/shabinder/common/list/store/SpotiFlyerListStoreProvider.kt index 7fddc9c2..c1459231 100644 --- a/common/list/src/commonMain/kotlin/com/shabinder/common/list/store/SpotiFlyerListStoreProvider.kt +++ b/common/list/src/commonMain/kotlin/com/shabinder/common/list/store/SpotiFlyerListStoreProvider.kt @@ -33,7 +33,7 @@ import com.shabinder.common.models.PlatformQueryResult import com.shabinder.common.models.TrackDetails import com.shabinder.common.models.methods import kotlinx.coroutines.flow.MutableSharedFlow -import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.flow.collect internal class SpotiFlyerListStoreProvider( private val dir: Dir, @@ -80,7 +80,7 @@ internal class SpotiFlyerListStoreProvider( ) } - downloadProgressFlow.collectLatest { map -> + downloadProgressFlow.collect { map -> logger.d(map.size.toString(), "ListStore: flow Updated") val updatedTrackList = getState().trackList.updateTracksStatuses(map) if (updatedTrackList.isNotEmpty()) dispatch(Result.UpdateTrackList(updatedTrackList))