From 8531e08c2ec9ee63f6c54ffc6ed0b69dbfb60d9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Font=C3=A1n?= Date: Sat, 11 Mar 2023 01:00:17 +0100 Subject: [PATCH] Cleaned up some code --- .idea/misc.xml | 1 - .../java/com/bobbyesp/spowlo/Downloader.kt | 3 - .../ui/pages/history/DownloadsHistoryPage.kt | 5 +- .../spowlo/ui/pages/settings/SettingsPage.kt | 31 +------- .../settings/general/GeneralSettingsPage.kt | 2 +- .../com/bobbyesp/spowlo/utils/ListUtil.kt | 78 +++++++++++++++++++ gradle/libs.versions.toml | 2 +- 7 files changed, 85 insertions(+), 37 deletions(-) create mode 100644 app/src/main/java/com/bobbyesp/spowlo/utils/ListUtil.kt diff --git a/.idea/misc.xml b/.idea/misc.xml index 55ded342..d119ba0a 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/app/src/main/java/com/bobbyesp/spowlo/Downloader.kt b/app/src/main/java/com/bobbyesp/spowlo/Downloader.kt index e13d1428..0f825145 100644 --- a/app/src/main/java/com/bobbyesp/spowlo/Downloader.kt +++ b/app/src/main/java/com/bobbyesp/spowlo/Downloader.kt @@ -30,9 +30,6 @@ object Downloader { object Idle : State() } - fun makeKey(url: String, templateName: String): String = "${templateName}_$url" - - data class ErrorState( val errorReport: String = "", val errorMessageResId: Int = R.string.unknown_error, diff --git a/app/src/main/java/com/bobbyesp/spowlo/ui/pages/history/DownloadsHistoryPage.kt b/app/src/main/java/com/bobbyesp/spowlo/ui/pages/history/DownloadsHistoryPage.kt index ba9115b2..aa03eb72 100644 --- a/app/src/main/java/com/bobbyesp/spowlo/ui/pages/history/DownloadsHistoryPage.kt +++ b/app/src/main/java/com/bobbyesp/spowlo/ui/pages/history/DownloadsHistoryPage.kt @@ -28,7 +28,6 @@ import androidx.compose.material.icons.Icons import androidx.compose.material.icons.outlined.Checklist import androidx.compose.material.icons.outlined.DeleteSweep import androidx.compose.material.icons.outlined.DownloadForOffline -import androidx.compose.material.icons.outlined.LayersClear import androidx.compose.material3.BottomAppBar import androidx.compose.material3.Divider import androidx.compose.material3.ExperimentalMaterial3Api @@ -60,7 +59,6 @@ import androidx.compose.ui.semantics.contentDescription import androidx.compose.ui.semantics.semantics import androidx.compose.ui.state.ToggleableState import androidx.compose.ui.text.style.TextAlign -import androidx.compose.ui.text.toUpperCase import androidx.compose.ui.unit.dp import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle @@ -73,7 +71,6 @@ import com.bobbyesp.spowlo.ui.components.ConfirmButton import com.bobbyesp.spowlo.ui.components.DismissButton import com.bobbyesp.spowlo.ui.components.LargeTopAppBar import com.bobbyesp.spowlo.ui.components.MultiChoiceItem -import com.bobbyesp.spowlo.ui.components.PreferenceSubtitle import com.bobbyesp.spowlo.ui.components.SpowloDialog import com.bobbyesp.spowlo.ui.components.history.HistoryMediaItem import com.bobbyesp.spowlo.utils.DatabaseUtil @@ -277,7 +274,7 @@ fun DownloadsHistoryPage( } ) { innerPaddings -> val cellCount = when (LocalWindowWidthState.current) { - WindowWidthSizeClass.Expanded -> 2 //TODO: Add 2 columns. Actually that crashes the app so we'll keep it at 1 for now. The problem is that MarqueeText is not working properly with LazyColumn + WindowWidthSizeClass.Expanded -> 2 else -> 1 } val span: (LazyGridItemSpanScope) -> GridItemSpan = { GridItemSpan(cellCount) } diff --git a/app/src/main/java/com/bobbyesp/spowlo/ui/pages/settings/SettingsPage.kt b/app/src/main/java/com/bobbyesp/spowlo/ui/pages/settings/SettingsPage.kt index 095a2a65..a01400a7 100644 --- a/app/src/main/java/com/bobbyesp/spowlo/ui/pages/settings/SettingsPage.kt +++ b/app/src/main/java/com/bobbyesp/spowlo/ui/pages/settings/SettingsPage.kt @@ -29,17 +29,15 @@ import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.Scaffold import androidx.compose.material3.TopAppBarDefaults import androidx.compose.runtime.Composable -import androidx.compose.ui.platform.LocalContext -import androidx.navigation.NavController import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.input.nestedscroll.nestedScroll +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource -import androidx.compose.ui.res.vectorResource +import androidx.navigation.NavController import com.bobbyesp.spowlo.R import com.bobbyesp.spowlo.ui.common.LocalDarkTheme import com.bobbyesp.spowlo.ui.common.Route @@ -48,6 +46,7 @@ import com.bobbyesp.spowlo.ui.components.PreferencesHintCard import com.bobbyesp.spowlo.ui.components.SettingItem import com.bobbyesp.spowlo.ui.components.SettingTitle import com.bobbyesp.spowlo.ui.components.SmallTopAppBar +import com.bobbyesp.spowlo.ui.pages.settings.about.LocalAsset @OptIn(ExperimentalMaterial3Api::class) @Composable @@ -132,7 +131,7 @@ fun SettingsPage(navController: NavController) { description = stringResource( id = R.string.spotify_settings_desc ), - icon = ImageVector.vectorResource(id = R.drawable.spotify_logo) + icon = LocalAsset(id = R.drawable.spotify_logo) ) { navController.navigate(Route.SPOTIFY_PREFERENCES) { launchSingleTop = true @@ -163,17 +162,6 @@ fun SettingsPage(navController: NavController) { } } } - /* item { - SettingItem( - title = stringResource(id = R.string.subtitle), description = stringResource( - id = R.string.subtitle_desc - ), icon = Icons.Outlined.Subtitles - ) { - navController.navigate(Route.SUBTITLE_PREFERENCES) { - launchSingleTop = true - } - } - }*/ /*item { SettingItem( title = stringResource(id = R.string.network), @@ -185,17 +173,6 @@ fun SettingsPage(navController: NavController) { } } }*/ - /*item { - SettingItem( - title = stringResource(id = R.string.custom_command), - description = stringResource(id = R.string.custom_command_desc), - icon = Icons.Outlined.Terminal - ) { - navController.navigate(Route.TEMPLATE) { - launchSingleTop = true - } - } - }*/ item { SettingItem( title = stringResource(id = R.string.appearance), description = stringResource( diff --git a/app/src/main/java/com/bobbyesp/spowlo/ui/pages/settings/general/GeneralSettingsPage.kt b/app/src/main/java/com/bobbyesp/spowlo/ui/pages/settings/general/GeneralSettingsPage.kt index 8a67783a..acdb35ef 100644 --- a/app/src/main/java/com/bobbyesp/spowlo/ui/pages/settings/general/GeneralSettingsPage.kt +++ b/app/src/main/java/com/bobbyesp/spowlo/ui/pages/settings/general/GeneralSettingsPage.kt @@ -174,7 +174,7 @@ fun GeneralSettingsPage( PreferencesUtil.updateValue(GEO_BYPASS, useGeobypass) } }, - isChecked = useCache + isChecked = useGeobypass ) } item { diff --git a/app/src/main/java/com/bobbyesp/spowlo/utils/ListUtil.kt b/app/src/main/java/com/bobbyesp/spowlo/utils/ListUtil.kt new file mode 100644 index 00000000..db11f9a1 --- /dev/null +++ b/app/src/main/java/com/bobbyesp/spowlo/utils/ListUtil.kt @@ -0,0 +1,78 @@ +package com.bobbyesp.spowlo.utils + +import java.util.concurrent.locks.ReentrantReadWriteLock +import kotlin.concurrent.read +import kotlin.concurrent.write +import kotlin.math.min +import kotlin.random.Random + +fun List.subListNonStrict(length: Int, start: Int = 0) = + subList(start, min(start + length, size)) + +fun MutableList.swap(to: Collection) { + with(this) { + clear() + addAll(to) + } +} + +fun List.randomSubList(length: Int) = List(length) { get(Random.nextInt(size)) } + +fun List.strictEquals(to: List): Boolean { + if (size != to.size) return false + for (i in indices) { + if (get(i) != to[i]) return false + } + return true +} + +fun List.indexOfOrNull(value: T) = indexOfOrNull { it == value } +fun List.indexOfOrNull(predicate: (T) -> Boolean): Int? { + for (i in indices) { + if (predicate(get(i))) return i + } + return null +} + +fun List.distinctList() = distinct().toList() + +fun List.mutate(fn: MutableList.() -> Unit): List { + val out = toMutableList() + fn.invoke(out) + return out.toList() +} + +class ConcurrentList : MutableList { + private val list = mutableListOf() + private val lock = ReentrantReadWriteLock() + + override val size: Int get() = lock.read { list.size } + + override operator fun set(index: Int, element: T) = lock.write { list.set(index, element) } + override operator fun get(index: Int) = lock.read { list[index] } + + override fun contains(element: T) = lock.read { list.contains(element) } + override fun containsAll(elements: Collection) = lock.read { list.containsAll(elements) } + override fun indexOf(element: T) = lock.read { list.indexOf(element) } + override fun lastIndexOf(element: T) = lock.read { list.lastIndexOf(element) } + override fun isEmpty() = lock.read { list.isEmpty() } + override fun subList(fromIndex: Int, toIndex: Int) = + lock.read { list.subList(fromIndex, toIndex) } + + override fun add(element: T) = lock.write { list.add(element) } + override fun add(index: Int, element: T) = lock.write { list.add(index, element) } + override fun addAll(elements: Collection) = lock.write { list.addAll(elements) } + override fun addAll(index: Int, elements: Collection) = + lock.write { list.addAll(index, elements) } + + override fun clear() = lock.write { list.clear() } + override fun remove(element: T) = lock.write { list.remove(element) } + override fun removeAll(elements: Collection) = lock.write { list.removeAll(elements) } + override fun removeAt(index: Int) = lock.write { list.removeAt(index) } + override fun retainAll(elements: Collection) = lock.write { list.retainAll(elements) } + + // NOTE: `write` lock since it returns `MutableIterator`s + override fun iterator() = lock.write { list.iterator() } + override fun listIterator() = lock.write { list.listIterator() } + override fun listIterator(index: Int) = lock.write { list.listIterator(index) } +} \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 5dac6440..14796f37 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -32,7 +32,7 @@ junit4 = "4.13.2" kotlin = "1.8.0" kotlinxCoroutines = "1.6.4" kotlinxDatetime = "0.4.0" -kotlinxSerializationJson = "1.4.1" +kotlinxSerializationJson = "1.5.0" okhttp = "5.0.0-alpha.10" ktor = "1.6.3"