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

Commit

Permalink
Add sort order option (closes #112)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Apr 26, 2023
1 parent c054220 commit 3ec0aa6
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

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

2 changes: 1 addition & 1 deletion .idea/vcs.xml

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

8 changes: 8 additions & 0 deletions app/src/main/java/com/bnyro/recorder/enums/SortOrder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.bnyro.recorder.enums

enum class SortOrder {
ALPHABETIC,
ALPHABETIC_REV,
SIZE,
SIZE_REV
}
20 changes: 19 additions & 1 deletion app/src/main/java/com/bnyro/recorder/ui/components/PlayerView.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bnyro.recorder.ui.components

import android.provider.OpenableColumns
import android.text.format.DateUtils
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.Box
Expand Down Expand Up @@ -38,15 +39,18 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.bnyro.recorder.R
import com.bnyro.recorder.enums.SortOrder
import com.bnyro.recorder.enums.VideoFormat
import com.bnyro.recorder.ui.dialogs.ConfirmationDialog
import com.bnyro.recorder.ui.models.PlayerModel
import kotlin.math.absoluteValue
import kotlinx.coroutines.delay

@Composable
fun PlayerView(
showVideoModeInitially: Boolean,
showDeleteAllDialog: Boolean,
sortOrder: SortOrder,
onDeleteAllDialogDismissed: () -> Unit
) {
val playerModel: PlayerModel = viewModel()
Expand All @@ -61,11 +65,25 @@ fun PlayerView(
)
}

val files = playerModel.files.filter { file ->
val filesToShow = playerModel.files.filter { file ->
val videoCriteria = VideoFormat.codecs.any { file.name.orEmpty().endsWith(it.extension) }
if (selectedTab == 0) !videoCriteria else videoCriteria
}

val files = when (sortOrder) {
SortOrder.ALPHABETIC, SortOrder.ALPHABETIC_REV -> filesToShow.sortedBy { it.name }
else -> filesToShow.sortedBy {
context.contentResolver.query(it.uri, null, null, null, null)?.use { cursor ->
cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE).absoluteValue)
}
}
}.let {
when (sortOrder) {
SortOrder.ALPHABETIC_REV, SortOrder.SIZE -> it.reversed()
else -> it
}
}

Column(
modifier = Modifier.fillMaxSize()
) {
Expand Down
48 changes: 47 additions & 1 deletion app/src/main/java/com/bnyro/recorder/ui/screens/PlayerScreen.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.bnyro.recorder.ui.screens

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Sort
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -13,6 +18,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.bnyro.recorder.R
import com.bnyro.recorder.enums.SortOrder
import com.bnyro.recorder.ui.common.ClickableIcon
import com.bnyro.recorder.ui.common.FullscreenDialog
import com.bnyro.recorder.ui.components.PlayerView
Expand All @@ -25,12 +31,52 @@ fun PlayerScreen(
var showDeleteAllDialog by remember {
mutableStateOf(false)
}
var selectedSortOrder by remember {
mutableStateOf(SortOrder.ALPHABETIC)
}

FullscreenDialog(
title = stringResource(R.string.recordings),
onDismissRequest = onDismissRequest,
useLargeAppBar = true,
actions = {
Box {
var showDropDown by remember {
mutableStateOf(false)
}
ClickableIcon(
imageVector = Icons.Default.Sort,
contentDescription = stringResource(R.string.sort)
) {
showDropDown = true
}

val sortOptions = listOf(
SortOrder.ALPHABETIC,
SortOrder.ALPHABETIC_REV,
SortOrder.SIZE,
SortOrder.SIZE_REV
)
val sortOptionNames = listOf(
R.string.alphabetic,
R.string.alphabetic_rev,
R.string.size,
R.string.size_rev
)
DropdownMenu(showDropDown, { showDropDown = false }) {
sortOptions.forEachIndexed { index, sortOrder ->
DropdownMenuItem(
text = {
Text(stringResource(sortOptionNames[index]))
},
onClick = {
selectedSortOrder = sortOrder
showDropDown = false
}
)
}
}
}
ClickableIcon(
imageVector = Icons.Default.Delete,
contentDescription = stringResource(R.string.delete_all)
Expand All @@ -44,7 +90,7 @@ fun PlayerScreen(
horizontal = 20.dp
)
) {
PlayerView(showVideoModeInitially, showDeleteAllDialog) {
PlayerView(showVideoModeInitially, showDeleteAllDialog, selectedSortOrder) {
showDeleteAllDialog = false
}
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
<string name="record">Record</string>
<string name="play">Play</string>
<string name="back">Back</string>
<!-- Sort order -->
<string name="sort">Sort</string>
<string name="alphabetic">Alphabetic</string>
<string name="alphabetic_rev">Alphabetic (reversed)</string>
<string name="size">Size</string>
<string name="size_rev">Size (reversed)</string>
<!-- Settings -->
<string name="settings">Settings</string>
<string name="options">Options</string>
Expand Down

0 comments on commit 3ec0aa6

Please sign in to comment.