Skip to content

Commit

Permalink
Cleaned up some code
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbyESP committed Mar 11, 2023
1 parent 77f8de0 commit 8531e08
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 37 deletions.
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions app/src/main/java/com/bobbyesp/spowlo/Downloader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ fun GeneralSettingsPage(
PreferencesUtil.updateValue(GEO_BYPASS, useGeobypass)
}
},
isChecked = useCache
isChecked = useGeobypass
)
}
item {
Expand Down
78 changes: 78 additions & 0 deletions app/src/main/java/com/bobbyesp/spowlo/utils/ListUtil.kt
Original file line number Diff line number Diff line change
@@ -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 <T> List<T>.subListNonStrict(length: Int, start: Int = 0) =
subList(start, min(start + length, size))

fun <T> MutableList<T>.swap(to: Collection<T>) {
with(this) {
clear()
addAll(to)
}
}

fun <T> List<T>.randomSubList(length: Int) = List(length) { get(Random.nextInt(size)) }

fun <T> List<T>.strictEquals(to: List<T>): Boolean {
if (size != to.size) return false
for (i in indices) {
if (get(i) != to[i]) return false
}
return true
}

fun <T> List<T>.indexOfOrNull(value: T) = indexOfOrNull { it == value }
fun <T> List<T>.indexOfOrNull(predicate: (T) -> Boolean): Int? {
for (i in indices) {
if (predicate(get(i))) return i
}
return null
}

fun <T> List<T>.distinctList() = distinct().toList()

fun <T> List<T>.mutate(fn: MutableList<T>.() -> Unit): List<T> {
val out = toMutableList()
fn.invoke(out)
return out.toList()
}

class ConcurrentList<T> : MutableList<T> {
private val list = mutableListOf<T>()
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<T>) = 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<T>) = lock.write { list.addAll(elements) }
override fun addAll(index: Int, elements: Collection<T>) =
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<T>) = lock.write { list.removeAll(elements) }
override fun removeAt(index: Int) = lock.write { list.removeAt(index) }
override fun retainAll(elements: Collection<T>) = 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) }
}
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 8531e08

Please sign in to comment.