Skip to content

Commit

Permalink
chore: README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
jing332 committed Feb 2, 2024
1 parent b76de41 commit 0c6d66e
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 174 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ComposeFilePicker
[![](https://jitpack.io/v/jing332/ComposeFilePicker.svg)](https://jitpack.io/#jing332/ComposeFilePicker)
[![Android CI (Test)](https://github.com/jing332/ComposeFilePicker/actions/workflows/build.yml/badge.svg)](https://github.com/jing332/ComposeFilePicker/actions/workflows/build.yml)
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
package com.github.jing332.filepicker

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ChevronRight
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.minimumInteractiveComponentSize
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.unit.dp

data class NavBarItem(val name: String, val path: String)
Expand All @@ -38,16 +33,15 @@ fun FileNavBar(
}

LazyRow(modifier, state = state) {
itemsIndexed(list) { index, item -> Row(
itemsIndexed(list) { index, item ->
Row(
modifier = Modifier.animateItemPlacement(),
verticalAlignment = Alignment.CenterVertically
) {
Box(
Modifier
.clip(MaterialTheme.shapes.small)
.clickable { onClick(item) }
.minimumInteractiveComponentSize())
{
TextButton(
onClick = { onClick(item) },
contentPadding = PaddingValues(horizontal = 2.dp)
) {
Text(text = item.name)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ fun FilePicker(
) {
Column(modifier) {
var sortConfig by remember { mutableStateOf(config.sortConfig) }
var viewType by remember { mutableStateOf(config.viewType) }
fun getState() = vm.fileListStates[vm.currentPath]
val selectedCount = getState()?.items?.count { it.isChecked.value } ?: 0
FilePickerToolbar(
Expand All @@ -124,6 +125,12 @@ fun FilePicker(
config.sortConfig = it
update()
},
viewType = viewType,
onSwitchViewType = {
viewType = it
config.viewType = it
update()
},
selectedCount = selectedCount,
onCancelSelect = {
getState()?.uncheckAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import com.github.jing332.filepicker.filetype.FileDetector
import com.github.jing332.filepicker.model.IFileModel


object ViewType {
const val LIST = 0
const val GRID = 1
}

fun interface FileFilter {
fun accept(file: IFileModel): Boolean
}
Expand All @@ -30,5 +35,5 @@ data class FilePickerConfig(
},

var sortConfig: SortConfig = SortConfig(),
) {
}
var viewType: Int = ViewType.GRID,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package com.github.jing332.filepicker

import androidx.compose.animation.Crossfade
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.Sort
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material.icons.filled.RadioButtonChecked
import androidx.compose.material.icons.filled.RadioButtonUnchecked
import androidx.compose.material3.Divider
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
Expand All @@ -26,6 +30,11 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.role
import androidx.compose.ui.semantics.selectableGroup
import androidx.compose.ui.semantics.selected
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp


Expand Down Expand Up @@ -55,11 +64,14 @@ fun FilePickerToolbar(
sortConfig: SortConfig,
onSortConfigChange: (SortConfig) -> Unit,

viewType: Int,
onSwitchViewType: (Int) -> Unit,

selectedCount: Int,
onCancelSelect: () -> Unit,
onConfirmSelect: () -> Unit,
) {
Crossfade(targetState = selectedCount > 0) {
Crossfade(targetState = selectedCount > 0, label = "") {
if (it)
BasicToolbar(title = { Text(text = "$selectedCount") }, modifier = modifier,
navigationIcon = {
Expand All @@ -77,9 +89,6 @@ fun FilePickerToolbar(
})
else
BasicToolbar(modifier = modifier, title = { Text(title) }, actions = {
// IconButton(onClick = { /*TODO*/ }) {
// Icon(Icons.Default.Search, stringResource(R.string.search))
// }
var showSortConfigDialog by remember { mutableStateOf(false) }
if (showSortConfigDialog)
SortSettingsDialog(
Expand All @@ -90,19 +99,71 @@ fun FilePickerToolbar(
var showOptions by rememberSaveable { mutableStateOf(false) }
IconButton(onClick = { showOptions = true }) {
Icon(Icons.Default.MoreVert, stringResource(R.string.more_options))
if (showOptions)
DropdownMenu(
expanded = showOptions,
onDismissRequest = { showOptions = false }) {
DropdownMenuItem(
text = { Text(stringResource(id = R.string.sort_by)) },
onClick = {
showOptions = false
showSortConfigDialog = true
DropdownMenu(
expanded = showOptions,
onDismissRequest = { showOptions = false }) {
RadioDropdownMenuItem(
text = {
Text(stringResource(id = R.string.list))
},
checked = viewType == ViewType.LIST,
onClick = {
showOptions = false
onSwitchViewType(if (viewType == ViewType.LIST) ViewType.GRID else ViewType.LIST)
}
)
RadioDropdownMenuItem(
text = {
Text(stringResource(id = R.string.grid))
},
checked = viewType == ViewType.GRID,
onClick = {
showOptions = false
onSwitchViewType(if (viewType == ViewType.LIST) ViewType.GRID else ViewType.LIST)
}
)

Divider(Modifier.fillMaxWidth())
DropdownMenuItem(
text = {
Row {
Icon(Icons.AutoMirrored.Filled.Sort, null)
Text(stringResource(id = R.string.sort_by))
}
)
}
},
onClick = {
showOptions = false
showSortConfigDialog = true
}
)
}
}
})
}
}

@Composable
internal fun RadioDropdownMenuItem(
modifier: Modifier = Modifier,
text: @Composable () -> Unit,
checked: Boolean,
onClick: () -> Unit
) {
DropdownMenuItem(
modifier = modifier
.semantics {
role = Role.RadioButton
selected = checked

selectableGroup()
},
text = text,
onClick = onClick,
leadingIcon = {
if (checked)
Icon(Icons.Default.RadioButtonChecked, null)
else
Icon(Icons.Default.RadioButtonUnchecked, null)
}
)
}
Loading

0 comments on commit 0c6d66e

Please sign in to comment.