Skip to content

Commit

Permalink
Add option to remove nomedia
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamza417 committed Oct 3, 2024
1 parent 2d00a07 commit 5a62f19
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 16 deletions.
65 changes: 50 additions & 15 deletions app/src/main/java/app/simple/peri/compose/screens/Folders.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.lazy.staggeredgrid.LazyVerticalStaggeredGrid
Expand All @@ -24,6 +25,8 @@ import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Add
import androidx.compose.material.icons.rounded.Close
import androidx.compose.material.icons.rounded.Visibility
import androidx.compose.material.icons.rounded.VisibilityOff
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
Expand Down Expand Up @@ -67,6 +70,7 @@ import app.simple.peri.compose.constants.DIALOG_TITLE_FONT_SIZE
import app.simple.peri.compose.dialogs.common.ShowWarningDialog
import app.simple.peri.compose.nav.Routes
import app.simple.peri.models.Folder
import app.simple.peri.utils.ConditionUtils.invert
import app.simple.peri.viewmodels.WallpaperViewModel
import com.bumptech.glide.integration.compose.ExperimentalGlideComposeApi
import com.bumptech.glide.integration.compose.GlideImage
Expand Down Expand Up @@ -161,6 +165,7 @@ fun FolderItem(folder: Folder, navController: NavController? = null, wallpaperVi
val context = LocalContext.current
var showFolderMenu by remember { mutableStateOf(false) }
var showNomediaSuccess by remember { mutableStateOf(false) }
var showNomediaRemoveSuccess by remember { mutableStateOf(false) }
displayDimension.width = LocalView.current.width
displayDimension.height = LocalView.current.height

Expand All @@ -175,12 +180,18 @@ fun FolderItem(folder: Folder, navController: NavController? = null, wallpaperVi
}

context.getString(R.string.add_nomedia) -> {
Log.d("FolderItem", "Add .nomedia file to ${folder.name}")
wallpaperViewModel.addNoMediaFile(folder) {
Log.d("FolderItem", "Add .nomedia file to ${folder.name} success")
showNomediaSuccess = true
}
}

context.getString(R.string.remove_nomedia) -> {
Log.d("FolderItem", "Remove nomedia")
wallpaperViewModel.removeNoMediaFile(folder) {
Log.d("FolderItem", "Remove nomedia success")
showNomediaRemoveSuccess = true
}
}
}
}
)
Expand All @@ -194,6 +205,14 @@ fun FolderItem(folder: Folder, navController: NavController? = null, wallpaperVi
)
}

if (showNomediaRemoveSuccess) {
ShowWarningDialog(
title = stringResource(R.string.nomedia),
warning = stringResource(R.string.nomedia_remove_success),
onDismiss = { showNomediaRemoveSuccess = false }
)
}

ElevatedCard(
elevation = CardDefaults.cardElevation(
defaultElevation = 16.dp,
Expand Down Expand Up @@ -238,18 +257,31 @@ fun FolderItem(folder: Folder, navController: NavController? = null, wallpaperVi
style = HazeDefaults.style(backgroundColor = Color(0x50000000), blurRadius = 5.dp))
.align(Alignment.BottomCenter)
) {
Text(
text = folder.name ?: "",
modifier = Modifier
.padding(start = 16.dp, top = 16.dp, end = 16.dp),
textAlign = TextAlign.Start,
fontSize = 20.sp, // Set the font size
fontWeight = FontWeight.Bold, // Make the text bold
color = Color.White, // Set the text color
maxLines = 1,
overflow = TextOverflow.Ellipsis,
softWrap = false,
)
Row(
verticalAlignment = Alignment.CenterVertically,
) {
Text(
text = folder.name ?: "",
modifier = Modifier
.padding(start = 16.dp, top = 16.dp, end = 16.dp)
.weight(1f),
textAlign = TextAlign.Start,
fontSize = 20.sp, // Set the font size
fontWeight = FontWeight.Bold, // Make the text bold
color = Color.White, // Set the text color
maxLines = 1,
overflow = TextOverflow.Ellipsis,
softWrap = false,
)
Icon(
imageVector = if (folder.isNomedia) Icons.Rounded.VisibilityOff else Icons.Rounded.Visibility,
contentDescription = "No media",
modifier = Modifier
.padding(start = 8.dp, top = 16.dp, end = 16.dp)
.size(14.dp),
tint = Color.White
)
}

Text(
text = stringResource(id = R.string.tag_count, folder.count),
Expand All @@ -268,7 +300,10 @@ fun FolderItem(folder: Folder, navController: NavController? = null, wallpaperVi
fun FolderMenu(folder: Folder? = null, onDismiss: () -> Unit, onOptionSelected: (String) -> Unit) {
val options = listOf(
stringResource(R.string.delete),
stringResource(R.string.add_nomedia),
when (folder?.isNomedia?.invert()) {
true -> stringResource(R.string.add_nomedia)
else -> stringResource(R.string.remove_nomedia)
},
)

AlertDialog(
Expand Down
14 changes: 13 additions & 1 deletion app/src/main/java/app/simple/peri/models/Folder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ public class Folder implements Parcelable {
private String name;
private String uri;
private int count;
private boolean isNomedia;

public Folder(int hashcode, String name, String uri, int count) {
public Folder(int hashcode, String name, String uri, int count, boolean isNomedia) {
this.hashcode = hashcode;
this.name = name;
this.uri = uri;
this.count = count;
this.isNomedia = isNomedia;
}

public Folder() {
Expand All @@ -24,6 +26,7 @@ protected Folder(Parcel in) {
name = in.readString();
uri = in.readString();
count = in.readInt();
isNomedia = in.readByte() != 0;
}

@Override
Expand All @@ -32,6 +35,7 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(uri);
dest.writeInt(count);
dest.writeByte((byte) (isNomedia ? 1 : 0));
}

@Override
Expand Down Expand Up @@ -82,4 +86,12 @@ public int getCount() {
public void setCount(int count) {
this.count = count;
}

public boolean isNomedia() {
return isNomedia;
}

public void setNomedia(boolean nomedia) {
isNomedia = nomedia;
}
}
21 changes: 21 additions & 0 deletions app/src/main/java/app/simple/peri/viewmodels/WallpaperViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class WallpaperViewModel(application: Application) : AndroidViewModel(applicatio
this.uri = uri.uri.toString()
count = wallpaperDao?.getWallpapersByUriHashcode(uri.uri?.hashCode() ?: 0)?.size ?: 0
hashcode = uri.uri.hashCode()
isNomedia = pickedDirectory.findFile(".nomedia")?.exists() == true
}

folders.add(folder)
Expand Down Expand Up @@ -508,6 +509,26 @@ class WallpaperViewModel(application: Application) : AndroidViewModel(applicatio
isNomediaDirectory.postValue(false)
withContext(Dispatchers.Main) {
onSuccess()
loadFolders()
}
}
}
}
}

fun removeNoMediaFile(folder: Folder, onSuccess: () -> Unit) {
viewModelScope.launch(Dispatchers.IO) {
val pickedDirectory = DocumentFile.fromTreeUri(getApplication(), Uri.parse(folder.uri))!!

if (pickedDirectory.exists()) {
pickedDirectory.findFile(".nomedia")?.delete()

if (pickedDirectory.findFile(".nomedia") == null
|| pickedDirectory.findFile(".nomedia")?.exists() == false) {
isNomediaDirectory.postValue(true)
withContext(Dispatchers.Main) {
onSuccess()
loadFolders()
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,6 @@
<string name="live_wallpapers">Live Wallpapers</string>
<string name="reset">Reset</string>
<string name="add_nomedia">Add .nomedia</string>
<string name="remove_nomedia">Remove .nomedia</string>
<string name="nomedia_remove_success">.nomedia has been removed. The folder will be visible again.</string>
</resources>

0 comments on commit 5a62f19

Please sign in to comment.