mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-10-09 22:32:37 +02:00
add options to disable auto playback e show images in the settings screen
This commit is contained in:
@@ -10,6 +10,7 @@ import com.vitorpamplona.amethyst.model.Account
|
|||||||
import com.vitorpamplona.amethyst.model.GLOBAL_FOLLOWS
|
import com.vitorpamplona.amethyst.model.GLOBAL_FOLLOWS
|
||||||
import com.vitorpamplona.amethyst.model.KIND3_FOLLOWS
|
import com.vitorpamplona.amethyst.model.KIND3_FOLLOWS
|
||||||
import com.vitorpamplona.amethyst.model.RelaySetupInfo
|
import com.vitorpamplona.amethyst.model.RelaySetupInfo
|
||||||
|
import com.vitorpamplona.amethyst.model.Settings
|
||||||
import com.vitorpamplona.amethyst.model.hexToByteArray
|
import com.vitorpamplona.amethyst.model.hexToByteArray
|
||||||
import com.vitorpamplona.amethyst.service.HttpClient
|
import com.vitorpamplona.amethyst.service.HttpClient
|
||||||
import com.vitorpamplona.amethyst.service.model.ContactListEvent
|
import com.vitorpamplona.amethyst.service.model.ContactListEvent
|
||||||
@@ -67,6 +68,8 @@ private object PrefKeys {
|
|||||||
const val WARN_ABOUT_REPORTS = "warn_about_reports"
|
const val WARN_ABOUT_REPORTS = "warn_about_reports"
|
||||||
const val FILTER_SPAM_FROM_STRANGERS = "filter_spam_from_strangers"
|
const val FILTER_SPAM_FROM_STRANGERS = "filter_spam_from_strangers"
|
||||||
const val LAST_READ_PER_ROUTE = "last_read_route_per_route"
|
const val LAST_READ_PER_ROUTE = "last_read_route_per_route"
|
||||||
|
const val AUTOMATICALLY_SHOW_IMAGES = "automatically_show_images"
|
||||||
|
const val AUTOMATICALLY_START_PLAYBACK = "automatically_start_playback"
|
||||||
val LAST_READ: (String) -> String = { route -> "last_read_route_$route" }
|
val LAST_READ: (String) -> String = { route -> "last_read_route_$route" }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -236,6 +239,21 @@ object LocalPreferences {
|
|||||||
putBoolean(PrefKeys.SHOW_SENSITIVE_CONTENT, account.showSensitiveContent!!)
|
putBoolean(PrefKeys.SHOW_SENSITIVE_CONTENT, account.showSensitiveContent!!)
|
||||||
}
|
}
|
||||||
}.apply()
|
}.apply()
|
||||||
|
|
||||||
|
val globalPrefs = encryptedPreferences()
|
||||||
|
globalPrefs.edit().apply {
|
||||||
|
if (account.settings.automaticallyShowImages == null) {
|
||||||
|
remove(PrefKeys.AUTOMATICALLY_SHOW_IMAGES)
|
||||||
|
} else {
|
||||||
|
putBoolean(PrefKeys.AUTOMATICALLY_SHOW_IMAGES, account.settings.automaticallyShowImages!!)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (account.settings.automaticallyStartPlayback == null) {
|
||||||
|
remove(PrefKeys.AUTOMATICALLY_START_PLAYBACK)
|
||||||
|
} else {
|
||||||
|
putBoolean(PrefKeys.AUTOMATICALLY_START_PLAYBACK, account.settings.automaticallyStartPlayback!!)
|
||||||
|
}
|
||||||
|
}.apply()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun loadFromEncryptedStorage(): Account? {
|
fun loadFromEncryptedStorage(): Account? {
|
||||||
@@ -340,6 +358,21 @@ object LocalPreferences {
|
|||||||
mapOf()
|
mapOf()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val settings = Settings(null, null)
|
||||||
|
encryptedPreferences().apply {
|
||||||
|
settings.automaticallyShowImages = if (contains(PrefKeys.AUTOMATICALLY_SHOW_IMAGES)) {
|
||||||
|
getBoolean(PrefKeys.AUTOMATICALLY_SHOW_IMAGES, false)
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
|
||||||
|
settings.automaticallyStartPlayback = if (contains(PrefKeys.AUTOMATICALLY_START_PLAYBACK)) {
|
||||||
|
getBoolean(PrefKeys.AUTOMATICALLY_START_PLAYBACK, false)
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val a = Account(
|
val a = Account(
|
||||||
loggedIn = Persona(privKey = privKey?.hexToByteArray(), pubKey = pubKey.hexToByteArray()),
|
loggedIn = Persona(privKey = privKey?.hexToByteArray(), pubKey = pubKey.hexToByteArray()),
|
||||||
followingChannels = followingChannels,
|
followingChannels = followingChannels,
|
||||||
@@ -366,7 +399,8 @@ object LocalPreferences {
|
|||||||
showSensitiveContent = showSensitiveContent,
|
showSensitiveContent = showSensitiveContent,
|
||||||
warnAboutPostsWithReports = warnAboutReports,
|
warnAboutPostsWithReports = warnAboutReports,
|
||||||
filterSpamFromStrangers = filterSpam,
|
filterSpamFromStrangers = filterSpam,
|
||||||
lastReadPerRoute = lastReadPerRoute
|
lastReadPerRoute = lastReadPerRoute,
|
||||||
|
settings = settings
|
||||||
)
|
)
|
||||||
|
|
||||||
return a
|
return a
|
||||||
|
@@ -73,7 +73,8 @@ class Account(
|
|||||||
var showSensitiveContent: Boolean? = null,
|
var showSensitiveContent: Boolean? = null,
|
||||||
var warnAboutPostsWithReports: Boolean = true,
|
var warnAboutPostsWithReports: Boolean = true,
|
||||||
var filterSpamFromStrangers: Boolean = true,
|
var filterSpamFromStrangers: Boolean = true,
|
||||||
var lastReadPerRoute: Map<String, Long> = mapOf<String, Long>()
|
var lastReadPerRoute: Map<String, Long> = mapOf<String, Long>(),
|
||||||
|
var settings: Settings = Settings(null, null)
|
||||||
) {
|
) {
|
||||||
var transientHiddenUsers: Set<String> = setOf()
|
var transientHiddenUsers: Set<String> = setOf()
|
||||||
|
|
||||||
@@ -85,6 +86,13 @@ class Account(
|
|||||||
|
|
||||||
var userProfileCache: User? = null
|
var userProfileCache: User? = null
|
||||||
|
|
||||||
|
fun updateGlobalSettings(automaticallyShowImages: Boolean?, automaticallyStartPlayback: Boolean?) {
|
||||||
|
settings.automaticallyStartPlayback = automaticallyStartPlayback
|
||||||
|
settings.automaticallyShowImages = automaticallyShowImages
|
||||||
|
live.invalidateData()
|
||||||
|
saveable.invalidateData()
|
||||||
|
}
|
||||||
|
|
||||||
fun updateOptOutOptions(warnReports: Boolean, filterSpam: Boolean) {
|
fun updateOptOutOptions(warnReports: Boolean, filterSpam: Boolean) {
|
||||||
warnAboutPostsWithReports = warnReports
|
warnAboutPostsWithReports = warnReports
|
||||||
filterSpamFromStrangers = filterSpam
|
filterSpamFromStrangers = filterSpam
|
||||||
|
@@ -1,4 +1,9 @@
|
|||||||
package com.vitorpamplona.amethyst.model
|
package com.vitorpamplona.amethyst.model
|
||||||
|
|
||||||
class Settings {
|
import androidx.compose.runtime.Stable
|
||||||
}
|
|
||||||
|
@Stable
|
||||||
|
class Settings(
|
||||||
|
var automaticallyShowImages: Boolean?,
|
||||||
|
var automaticallyStartPlayback: Boolean?
|
||||||
|
)
|
||||||
|
@@ -40,6 +40,10 @@ class AccountViewModel(val account: Account) : ViewModel() {
|
|||||||
val userFollows: LiveData<UserState> = account.userProfile().live().follows.map { it }
|
val userFollows: LiveData<UserState> = account.userProfile().live().follows.map { it }
|
||||||
val userRelays: LiveData<UserState> = account.userProfile().live().relays.map { it }
|
val userRelays: LiveData<UserState> = account.userProfile().live().relays.map { it }
|
||||||
|
|
||||||
|
fun updateGlobalSettings(automaticallyShowImages: Boolean?, automaticallyStartPlayback: Boolean?) {
|
||||||
|
account.updateGlobalSettings(automaticallyShowImages, automaticallyStartPlayback)
|
||||||
|
}
|
||||||
|
|
||||||
fun isWriteable(): Boolean {
|
fun isWriteable(): Boolean {
|
||||||
return account.isWriteable()
|
return account.isWriteable()
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,11 @@
|
|||||||
package com.vitorpamplona.amethyst.ui.screen.loggedIn
|
package com.vitorpamplona.amethyst.ui.screen.loggedIn
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.Row
|
||||||
import androidx.compose.foundation.layout.Spacer
|
import androidx.compose.foundation.layout.Spacer
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.material.Button
|
||||||
import androidx.compose.material.DropdownMenuItem
|
import androidx.compose.material.DropdownMenuItem
|
||||||
import androidx.compose.material.ExperimentalMaterialApi
|
import androidx.compose.material.ExperimentalMaterialApi
|
||||||
import androidx.compose.material.ExposedDropdownMenuBox
|
import androidx.compose.material.ExposedDropdownMenuBox
|
||||||
@@ -13,38 +17,95 @@ import androidx.compose.runtime.MutableState
|
|||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.runtime.rememberCoroutineScope
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.text.font.FontWeight
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
import androidx.compose.ui.unit.sp
|
import androidx.compose.ui.unit.sp
|
||||||
|
import com.vitorpamplona.amethyst.LocalPreferences
|
||||||
|
import com.vitorpamplona.amethyst.ServiceManager
|
||||||
import com.vitorpamplona.amethyst.ui.theme.DoubleVertSpacer
|
import com.vitorpamplona.amethyst.ui.theme.DoubleVertSpacer
|
||||||
import com.vitorpamplona.amethyst.ui.theme.StdPadding
|
import com.vitorpamplona.amethyst.ui.theme.StdPadding
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun SettingsScreen(
|
fun SettingsScreen(
|
||||||
accountViewModel: AccountViewModel,
|
accountViewModel: AccountViewModel,
|
||||||
nav: (String) -> Unit
|
nav: (String) -> Unit
|
||||||
) {
|
) {
|
||||||
val listItems = arrayOf("Always", "Wifi-only", "Never")
|
val scope = rememberCoroutineScope()
|
||||||
val selectedItem = remember {
|
val selectedItens = arrayOf("Always", "Wifi-only", "Never")
|
||||||
mutableStateOf(listItems[0])
|
val settings = accountViewModel.account.settings
|
||||||
|
val index = if (settings.automaticallyShowImages == null) { 0 } else {
|
||||||
|
if (settings.automaticallyShowImages == true) 1 else 2
|
||||||
|
}
|
||||||
|
val videoIndex = if (settings.automaticallyStartPlayback == null) { 0 } else {
|
||||||
|
if (settings.automaticallyShowImages == true) 1 else 2
|
||||||
|
}
|
||||||
|
val selectedItem = remember {
|
||||||
|
mutableStateOf(selectedItens[index])
|
||||||
|
}
|
||||||
|
val selectedVideoItem = remember {
|
||||||
|
mutableStateOf(selectedItens[videoIndex])
|
||||||
}
|
}
|
||||||
|
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
Column(
|
Column(
|
||||||
StdPadding
|
StdPadding,
|
||||||
|
horizontalAlignment = Alignment.CenterHorizontally
|
||||||
) {
|
) {
|
||||||
Section("Account preferences")
|
Section("Account preferences")
|
||||||
|
|
||||||
Section("Application preferences")
|
Section("Application preferences")
|
||||||
|
|
||||||
Text(
|
Text(
|
||||||
"Media",
|
"Media",
|
||||||
fontWeight = FontWeight.Bold
|
fontWeight = FontWeight.Bold
|
||||||
)
|
)
|
||||||
|
|
||||||
DropDownSettings(
|
DropDownSettings(
|
||||||
selectedItem = selectedItem,
|
selectedItem = selectedItem,
|
||||||
listItems = listItems
|
listItems = selectedItens,
|
||||||
|
title = "Automatically load images/gifs"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Spacer(modifier = DoubleVertSpacer)
|
||||||
|
|
||||||
|
DropDownSettings(
|
||||||
|
selectedItem = selectedVideoItem,
|
||||||
|
listItems = selectedItens,
|
||||||
|
title = "Automatically play videos"
|
||||||
|
)
|
||||||
|
|
||||||
|
Row(
|
||||||
|
Modifier.fillMaxWidth(),
|
||||||
|
Arrangement.Center
|
||||||
|
) {
|
||||||
|
Button(
|
||||||
|
onClick = {
|
||||||
|
val automaticallyShowImages = when (selectedItens.indexOf(selectedItem.value)) {
|
||||||
|
1 -> true
|
||||||
|
2 -> false
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
val automaticallyStartPlayback = when (selectedItens.indexOf(selectedVideoItem.value)) {
|
||||||
|
1 -> true
|
||||||
|
2 -> false
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
scope.launch(Dispatchers.IO) {
|
||||||
|
accountViewModel.updateGlobalSettings(automaticallyShowImages, automaticallyStartPlayback)
|
||||||
|
LocalPreferences.saveToEncryptedStorage(accountViewModel.account)
|
||||||
|
ServiceManager.pause()
|
||||||
|
ServiceManager.start(context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
Text(text = "Save")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,7 +113,8 @@ fun SettingsScreen(
|
|||||||
@Composable
|
@Composable
|
||||||
fun DropDownSettings(
|
fun DropDownSettings(
|
||||||
selectedItem: MutableState<String>,
|
selectedItem: MutableState<String>,
|
||||||
listItems: Array<String>
|
listItems: Array<String>,
|
||||||
|
title: String
|
||||||
) {
|
) {
|
||||||
var expanded by remember {
|
var expanded by remember {
|
||||||
mutableStateOf(false)
|
mutableStateOf(false)
|
||||||
@@ -64,10 +126,11 @@ fun DropDownSettings(
|
|||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
TextField(
|
TextField(
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
value = selectedItem.value,
|
value = selectedItem.value,
|
||||||
onValueChange = {},
|
onValueChange = {},
|
||||||
readOnly = true,
|
readOnly = true,
|
||||||
label = { Text(text = "Automatically load images/gifs") },
|
label = { Text(text = title) },
|
||||||
trailingIcon = {
|
trailingIcon = {
|
||||||
ExposedDropdownMenuDefaults.TrailingIcon(
|
ExposedDropdownMenuDefaults.TrailingIcon(
|
||||||
expanded = expanded
|
expanded = expanded
|
||||||
@@ -81,10 +144,12 @@ fun DropDownSettings(
|
|||||||
onDismissRequest = { expanded = false }
|
onDismissRequest = { expanded = false }
|
||||||
) {
|
) {
|
||||||
listItems.forEach { selectedOption ->
|
listItems.forEach { selectedOption ->
|
||||||
DropdownMenuItem(onClick = {
|
DropdownMenuItem(
|
||||||
|
onClick = {
|
||||||
selectedItem.value = selectedOption
|
selectedItem.value = selectedOption
|
||||||
expanded = false
|
expanded = false
|
||||||
}) {
|
}
|
||||||
|
) {
|
||||||
Text(text = selectedOption)
|
Text(text = selectedOption)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user