Skip to content

Commit

Permalink
Write and read storage backend selection to/from sharedPrefs
Browse files Browse the repository at this point in the history
  • Loading branch information
nain-F49FF806 committed Sep 30, 2024
1 parent 28fafa9 commit a3e73d5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
43 changes: 38 additions & 5 deletions app/src/main/java/alt/nainapps/aer/config/StorageConfigActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package alt.nainapps.aer.config
import alt.nainapps.aer.config.ui.theme.AnemoaerTheme
import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.os.Environment
import android.os.StatFs
import android.preference.PreferenceManager.getDefaultSharedPreferences
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
Expand Down Expand Up @@ -35,6 +37,7 @@ import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.tooling.preview.Preview
Expand All @@ -44,11 +47,13 @@ import sh.calvin.reorderable.rememberReorderableLazyListState
import java.io.File

class StorageConfigActivity : ComponentActivity() {

@OptIn(ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()

// At the top level of your kotlin file:
val sharedPrefs = getDefaultSharedPreferences(this)
setContent {
AnemoaerTheme {
Scaffold(modifier = Modifier.fillMaxSize(),
Expand All @@ -60,13 +65,26 @@ class StorageConfigActivity : ComponentActivity() {
)
}) { innerPadding ->
val storageInfos by rememberSaveable { mutableStateOf(fetchExternalStorageDirectories(this.applicationContext)) }
var selectedStorageDir by rememberSaveable { mutableStateOf(getPreferredStorageDir(sharedPrefs)) }
Column (Modifier.padding(paddingValues = innerPadding)) {
selectedStorageDir?.let {
Card(modifier = Modifier.padding(8.dp)) {
Text(
text = "Selected: $it",
modifier = Modifier.padding(8.dp)
)
}
}

Text(
text = "Drag to reorder:",
fontStyle = FontStyle.Italic,
modifier = Modifier.padding(8.dp)
)
StorageInfoListReorderable(storageInfos)

StorageInfoListReorderable(storageInfos, sharedPrefs) {
selected -> selectedStorageDir = selected
}
}
}
}
Expand Down Expand Up @@ -94,14 +112,18 @@ fun StorageInfoList(storageInfos: List<StorageInfo>) {

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun StorageInfoListReorderable(storageInfos: List<StorageInfo>) {
fun StorageInfoListReorderable(storageInfos: List<StorageInfo>, sharedPrefs: SharedPreferences,
onFreshStorageSelect: (String?) -> Unit) {
val mutableStorageInfosList = remember { mutableStateListOf(*storageInfos.toTypedArray()) }
val lazyListState = rememberLazyListState()
val reorderableLazyListState =
rememberReorderableLazyListState(lazyListState) { from, to ->
mutableStorageInfosList.apply { add(to.index, removeAt(from.index)) }
if (to.index == 0 ) {
// TODO: Save it to settings
if (to.index == 0 || from.index == 0 ) {
// Save it to settings
savePreferredStorageDir(sharedPrefs, mutableStorageInfosList.first().dir)
// callback to let parent compose update
onFreshStorageSelect(getPreferredStorageDir(sharedPrefs))
}
}
LazyColumn(
Expand Down Expand Up @@ -199,6 +221,17 @@ fun bytesToHumanReadableSize(bytes: Long) =
else -> "$bytes bytes"
}

fun savePreferredStorageDir(sharedPrefs: SharedPreferences, dir: String) {
with (sharedPrefs.edit()) {
putString("selected_storage_dir", dir)
apply()
}
}

fun getPreferredStorageDir(sharedPrefs: SharedPreferences): String? {
return sharedPrefs.getString("selected_storage_dir", null)
}

@Composable
fun Greeting(
name: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package alt.nainapps.aer.documents.home

import android.content.Context
import android.os.Environment
import android.preference.PreferenceManager.getDefaultSharedPreferences
import java.io.File
import java.io.IOException
import java.nio.file.Files
Expand All @@ -16,8 +17,9 @@ class HomeEnvironment private constructor(
context: Context,
) {
val baseDir: Path =
getSelectExternalFilesDir(context)?.toPath()
?: context.filesDir.toPath() // internal
getPreferredStorageFilesDir(context)?.toPath() // manual config
?: getSelectExternalFilesDir(context)?.toPath()
?: context.filesDir.toPath() // internal

init {
if (!Files.exists(baseDir)) {
Expand All @@ -29,6 +31,13 @@ class HomeEnvironment private constructor(

fun isRoot(path: Path): Boolean = baseDir == path

private fun getPreferredStorageFilesDir(context: Context): File? {
val sharedPrefs = getDefaultSharedPreferences(context)
return sharedPrefs.getString("selected_storage_dir",null)?.let {
File(it)
}
}

private fun getSelectExternalFilesDir(context: Context): File? {
val externalFilesDirs = context.getExternalFilesDirs(null)
// The first few (in forward order) may be on primary storage,
Expand Down

0 comments on commit a3e73d5

Please sign in to comment.