Skip to content

Commit

Permalink
Add memory card support #106
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamza417 committed Oct 29, 2024
1 parent 47b2e4c commit 10b0043
Show file tree
Hide file tree
Showing 5 changed files with 497 additions and 34 deletions.
4 changes: 2 additions & 2 deletions .idea/deploymentTargetSelector.xml

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

123 changes: 92 additions & 31 deletions app/src/main/java/app/simple/peri/activities/PathChooserActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app.simple.peri.activities
import android.content.Intent
import android.os.Bundle
import android.os.Environment
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.OnBackPressedCallback
import androidx.activity.compose.setContent
Expand All @@ -16,7 +17,11 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Memory
import androidx.compose.material.icons.rounded.SdCard
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
Expand All @@ -37,9 +42,11 @@ import androidx.compose.ui.unit.sp
import androidx.core.view.WindowInsetsCompat
import app.simple.peri.R
import app.simple.peri.compose.commons.COMMON_PADDING
import app.simple.peri.compose.dialogs.common.ShowWarningDialog
import app.simple.peri.compose.theme.PeristyleTheme
import app.simple.peri.preferences.SharedPreferences
import app.simple.peri.utils.FileUtils.toSize
import app.simple.peri.utils.SDCard
import java.io.File

class PathChooserActivity : ComponentActivity() {
Expand All @@ -50,9 +57,12 @@ class PathChooserActivity : ComponentActivity() {
SharedPreferences.init(applicationContext)

setContent {
var selectedPath by remember { mutableStateOf(Environment.getExternalStorageDirectory().absolutePath) }
var sdcardMode by remember { mutableStateOf(false) }
var mainPath by remember { mutableStateOf(Environment.getExternalStorageDirectory().absolutePath) }
var selectedPath by remember { mutableStateOf(mainPath) }
var statusBarHeight by remember { mutableIntStateOf(0) }
var navigationBarHeight by remember { mutableIntStateOf(0) }
var showNoSdCardWarning by remember { mutableStateOf(false) }

statusBarHeight = WindowInsetsCompat.toWindowInsetsCompat(
LocalView.current.rootWindowInsets
Expand All @@ -72,8 +82,9 @@ class PathChooserActivity : ComponentActivity() {
if (backPressedCallback == null) {
backPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
Log.i("PathChooser", "Selected path: $selectedPath for main path: $mainPath")
if (selectedPath.split("/").size > 1) {
if (selectedPath.equals(Environment.getExternalStorageDirectory().absolutePath).not()) {
if (selectedPath.equals(mainPath).not()) {
selectedPath = selectedPath.substringBeforeLast("/")
} else {
finish()
Expand All @@ -93,41 +104,96 @@ class PathChooserActivity : ComponentActivity() {
.fillMaxSize()
.padding(top = topPadding, bottom = bottomPadding)
) {
if (showNoSdCardWarning) {
ShowWarningDialog(
title = stringResource(id = R.string.error),
warning = stringResource(id = R.string.no_sd_card_found),
onDismiss = {
showNoSdCardWarning = false
}
)
}

Column(
modifier = Modifier
.fillMaxSize()
.padding(start = 16.dp, end = 16.dp)
) {
Text(
text = stringResource(id = R.string.select_folder),
modifier = Modifier
.wrapContentHeight()
.padding(start = 8.dp, end = 8.dp),
fontWeight = FontWeight.Bold,
fontSize = 32.sp
)
Row(
verticalAlignment = Alignment.CenterVertically,
) {
Column(
modifier = Modifier
.weight(1f)
) {
Text(
text = stringResource(id = R.string.select_folder),
modifier = Modifier
.wrapContentHeight()
.padding(start = 8.dp, end = 8.dp),
fontWeight = FontWeight.Bold,
fontSize = 32.sp
)

Text(
text = selectedPath,
modifier = Modifier
.wrapContentHeight()
.padding(start = 8.dp, end = 8.dp),
fontWeight = FontWeight.Normal,
fontSize = 16.sp
)
Text(
text = selectedPath,
modifier = Modifier
.wrapContentHeight()
.padding(start = 8.dp, end = 8.dp),
fontWeight = FontWeight.Normal,
fontSize = 16.sp
)
}

Button(
modifier = Modifier
.padding(COMMON_PADDING)
.wrapContentWidth(),
onClick = {
sdcardMode = sdcardMode.not()
try {
mainPath = if (sdcardMode) {
SDCard.findSdCardPath(applicationContext).absolutePath
} else {
Environment.getExternalStorageDirectory().absolutePath
}

selectedPath = mainPath
} catch (e: NullPointerException) {
sdcardMode = false
mainPath = Environment.getExternalStorageDirectory().absolutePath
selectedPath = mainPath
showNoSdCardWarning = true
}
}) {
if (sdcardMode) {
Icon(
imageVector = Icons.Rounded.SdCard,
contentDescription = null,
tint = MaterialTheme.colorScheme.surface,
modifier = Modifier.padding(8.dp)
)
} else {
Icon(
imageVector = Icons.Rounded.Memory,
contentDescription = null,
tint = MaterialTheme.colorScheme.surface,
modifier = Modifier.padding(8.dp)
)
}
}
}

DirectoryList(
modifier = Modifier
.weight(1f)
.padding(top = COMMON_PADDING, bottom = COMMON_PADDING),
directories = (selectedPath.takeIf { it.isNotEmpty() }
?.let { File(it).listFiles()?.toList() }
?: getInternalStorageDirectories())
?.let { File(it).listFiles()?.toList() } ?: emptyList())
.sortedWith(compareBy<File> { !it.isDirectory }.thenBy { it.name }),
selectedPath = selectedPath,
onDirectorySelected = { path ->
selectedPath = path
}
},
modifier = Modifier
.weight(1f)
.padding(top = COMMON_PADDING, bottom = COMMON_PADDING)
)
Row(
modifier = Modifier.fillMaxWidth(),
Expand Down Expand Up @@ -162,11 +228,6 @@ class PathChooserActivity : ComponentActivity() {
}
}

private fun getInternalStorageDirectories(): List<File> {
val internalStorage = Environment.getExternalStorageDirectory()
return internalStorage.listFiles()?.filter { it.isDirectory } ?: emptyList()
}

private fun onPathChosen(path: String) {
val resultIntent = Intent()
resultIntent.putExtra("chosen_path", path)
Expand All @@ -175,7 +236,7 @@ class PathChooserActivity : ComponentActivity() {
}

@Composable
fun DirectoryList(directories: List<File>, onDirectorySelected: (String) -> Unit, modifier: Modifier, selectedPath: String) {
fun DirectoryList(directories: List<File>, onDirectorySelected: (String) -> Unit, modifier: Modifier) {
LazyColumn(
modifier = modifier
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ fun WallpaperList(navController: NavController? = null) {
// Update the wallpapers list
wallpapers.clear()
wallpapers = updatedWallpapers.toMutableStateList()
Log.d("WallpaperList", "Updated wallpapers: ${updatedWallpapers.size}")

if (wallpapers.isEmpty()) { // TODO = list doesn't update with this
Log.e("WallpaperList", "Wallpapers list is empty")
Expand Down
Loading

0 comments on commit 10b0043

Please sign in to comment.