Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
feat: custom instance option
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Nov 30, 2023
1 parent 53b89c8 commit 67371b6
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package app.suhasdissa.vibeyou.ui.screens.settings

import android.widget.Toast
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
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.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import app.suhasdissa.vibeyou.R
import app.suhasdissa.vibeyou.backend.models.PipedInstance
import app.suhasdissa.vibeyou.utils.Pref
import app.suhasdissa.vibeyou.utils.rememberPreference
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull

fun String.isValidBaseUrl(): Boolean {
val url = toHttpUrlOrNull() ?: return false
return url.encodedPath in listOf("", "/")
}

@Composable
fun CustomInstanceOption(
onInstanceChange: (PipedInstance) -> Unit
) {
val context = LocalContext.current

var isUsingValidCustomInstance = remember {
Pref.sharedPreferences.getBoolean(Pref.customPipedInstanceKey, false)
}
var showCustomInstanceDialog by remember {
mutableStateOf(false)
}
var useCustomInstanceChecked by rememberPreference(
key = Pref.customPipedInstanceKey,
defaultValue = false
)

SwitchPref(
prefKey = Pref.customPipedInstanceKey,
title = stringResource(R.string.custom_instance)
) {
showCustomInstanceDialog = it

// reset the instance to the default one
if (!it) {
val instance = Pref.pipedInstances.first()
Pref.setInstance(instance)
onInstanceChange(instance)
}
}

if (showCustomInstanceDialog) {
var instanceApiUrl by remember {
mutableStateOf(Pref.currentInstance.apiUrl)
}
var instanceImageProxyUrl by remember {
mutableStateOf(Pref.currentInstance.imageProxyUrl)
}

AlertDialog(
onDismissRequest = {
showCustomInstanceDialog = false
useCustomInstanceChecked = isUsingValidCustomInstance
},
confirmButton = {
TextButton(
onClick = {
val apiUrl = instanceApiUrl.toHttpUrlOrNull()
if (apiUrl == null || !instanceApiUrl.isValidBaseUrl()) {
Toast.makeText(context, R.string.invalid_url, Toast.LENGTH_SHORT).show()

isUsingValidCustomInstance = false
useCustomInstanceChecked = false
} else {
val instance = PipedInstance(
name = apiUrl.host,
apiUrl = apiUrl.toString(),
imageProxyUrl = instanceImageProxyUrl
.takeIf { it.isValidBaseUrl() }
.orEmpty()
)

Pref.setInstance(instance)
onInstanceChange(instance)

isUsingValidCustomInstance = true
}

showCustomInstanceDialog = false
}
) {
Text(text = stringResource(R.string.ok))
}
},
dismissButton = {
TextButton(
onClick = {
showCustomInstanceDialog = false
useCustomInstanceChecked = isUsingValidCustomInstance
}
) {
Text(text = stringResource(R.string.cancel))
}
},
title = {
Text(text = stringResource(R.string.custom_instance))
},
text = {
Column {
OutlinedTextField(
value = instanceApiUrl,
onValueChange = {
instanceApiUrl = it
},
isError = !instanceApiUrl.isValidBaseUrl(),
placeholder = {
Text(stringResource(R.string.api_url))
}
)
Spacer(modifier = Modifier.height(8.dp))
OutlinedTextField(
value = instanceImageProxyUrl,
onValueChange = {
instanceImageProxyUrl = it
},
isError = !instanceImageProxyUrl.isValidBaseUrl(),
placeholder = {
Text(stringResource(R.string.image_proxy_url))
}
)
}
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ fun NetworkSettingsScreen() {
showDialog = true
}
}

item {
CustomInstanceOption {
currentServer = it
}
}
}
}
if (showDialog) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fun SwitchPref(
indication = null
) {
checked = !checked
onCheckedChange.invoke(checked)
},
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/app/suhasdissa/vibeyou/utils/Pref.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ object Pref {
const val thumbnailColorFallbackKey = "ThumbnailColorFallbackef"
const val latestSongsSortOrderKey = "LatestSongsSortOrderKey"
const val latestReverseSongsPrefKey = "LatestReverseSongsPrefKey"
const val customPipedInstanceKey = "CustomPipedInstanceKey"

lateinit var sharedPreferences: SharedPreferences

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,8 @@
<string name="added_all_the_songs_to_the_library">Added all the songs to the library</string>
<string name="add_all_songs_to_the_library">Add all songs to the library</string>
<string name="clear_queue">Clear Queue</string>
<string name="custom_instance">Custom instance</string>
<string name="api_url">Api Url</string>
<string name="image_proxy_url">Image proxy url</string>
<string name="invalid_url">Invalid url</string>
</resources>

0 comments on commit 67371b6

Please sign in to comment.