Skip to content

Commit

Permalink
Merge pull request #22 from f-lab-edu/feature/explore
Browse files Browse the repository at this point in the history
Feature/explore
  • Loading branch information
Guri999 authored Dec 26, 2024
2 parents c81374e + 5bb46d4 commit 1f48fd7
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 21 deletions.
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission
android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
android:minSdkVersion="30" />

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/java/kr/co/seedocs/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import androidx.activity.enableEdgeToEdge
import kr.co.main.Main
import kr.co.ui.theme.SeeDocsTheme

class MainActivity : ComponentActivity() {
class MainActivity : PermissionActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
Expand All @@ -18,4 +19,11 @@ class MainActivity : ComponentActivity() {
}
}
}

override fun onResume() {
super.onResume()

if (!checkStoragePermission())
requestStoragePermission()
}
}
71 changes: 71 additions & 0 deletions app/src/main/java/kr/co/seedocs/PermissionActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package kr.co.seedocs

import android.Manifest
import android.app.AppOpsManager
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.provider.Settings
import androidx.activity.ComponentActivity
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat

abstract class PermissionActivity : ComponentActivity() {

internal fun ComponentActivity.checkStoragePermission(): Boolean =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
checkStoragePermissionApi30()
} else {
checkStoragePermissionApi19()
}

@RequiresApi(Build.VERSION_CODES.R)
private fun ComponentActivity.checkStoragePermissionApi30(): Boolean {
val appOps = getSystemService(AppOpsManager::class.java)
val mode = appOps.unsafeCheckOpNoThrow(
MANAGE_EXTERNAL_STORAGE_PERMISSION,
applicationInfo.uid,
packageName
)

return mode == AppOpsManager.MODE_ALLOWED
}

private fun ComponentActivity.checkStoragePermissionApi19(): Boolean {
val status =
ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)

return status == PackageManager.PERMISSION_GRANTED
}

internal fun ComponentActivity.requestStoragePermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
requestStoragePermissionApi30()
} else {
requestStoragePermissionApi19()
}
}

@RequiresApi(Build.VERSION_CODES.R)
private fun ComponentActivity.requestStoragePermissionApi30() {
val intent = Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION)

startActivityForResult(intent, MANAGE_EXTERNAL_STORAGE_PERMISSION_REQUEST)
}

private fun ComponentActivity.requestStoragePermissionApi19() {
val permissions = arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE)
ActivityCompat.requestPermissions(
this,
permissions,
READ_EXTERNAL_STORAGE_PERMISSION_REQUEST
)
}

private companion object {
private const val MANAGE_EXTERNAL_STORAGE_PERMISSION = "android:manage_external_storage"
private const val MANAGE_EXTERNAL_STORAGE_PERMISSION_REQUEST = 100
private const val READ_EXTERNAL_STORAGE_PERMISSION_REQUEST = 101
}
}
10 changes: 2 additions & 8 deletions core/ui/src/main/java/kr/co/ui/widget/FileBox.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ fun FileBox(
vertical = 12.dp,
horizontal = 8.dp
)
.semantics {
contentDescription = name
.semantics {
contentDescription = "$name"
},
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(24.dp)
Expand All @@ -51,20 +51,14 @@ fun FileBox(
)

Column(
modifier = Modifier
.clearAndSetSemantics { },
verticalArrangement = Arrangement.spacedBy(4.dp)
) {
Text(
modifier = Modifier
.clearAndSetSemantics { },
text = name,
style = Theme.typography.body2sb,
color = Theme.colors.text
)
Text(
modifier = Modifier
.clearAndSetSemantics { },
text = "2023.01.01",
style = Theme.typography.caption1r,
color = Theme.colors.grayText
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ private fun BookmarkScreen(
)
}

//TODO 로컬에 저장된 북마크 데이터를 불러와 파일목록을 보여줄 예정
items(listOf("Effective Kotlin", "Android Developer")) { file ->
FileBox(
name = file
Expand Down
48 changes: 40 additions & 8 deletions feature/explore/src/main/java/kr/co/explore/ExploreScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,39 @@ import kr.co.ui.theme.SeeDocsTheme
import kr.co.ui.theme.Theme
import kr.co.ui.widget.FileBox
import kr.co.widget.FolderBox
import java.io.File

@Composable
internal fun ExploreRoute(
path: String,
padding: PaddingValues,
navigateToPdf: () -> Unit = {}
navigateToFolder: (String) -> Unit = {},
navigateToPdf: () -> Unit = {},
) {
val files = readPDFOrDirectory(path)

ExploreScreen(
path = path.replace("/storage/emulated/0", "내장 저장 공간"),
files = files,
padding = padding,
onFolderClick = { folderPath -> navigateToFolder(folderPath) },
onFileClick = navigateToPdf
)
}

@Composable
private fun ExploreScreen(
path: String,
files: List<Item> = emptyList(),
padding: PaddingValues,
onFolderClick: (String) -> Unit = {},
onFileClick: () -> Unit = {}
) {
Box(
modifier = Modifier
.fillMaxSize()
.padding(padding)
.background(color = Theme.colors.bg)
.background(color = Theme.colors.bg),
) {
LazyVerticalGrid(
contentPadding = PaddingValues(
Expand All @@ -69,12 +79,13 @@ private fun ExploreScreen(
)
Text(
text = buildAnnotatedString {
append("규상의 S24 >")
append(">${path.split("/").dropLast(1).joinToString(separator = "/")}")
append("/")
withStyle(
Theme.typography.caption1r.copy(color = Theme.colors.highlight)
.toSpanStyle()
) {
append("Download")
append(path.split("/").last())
}
},
style = Theme.typography.caption1r,
Expand All @@ -83,30 +94,51 @@ private fun ExploreScreen(
}
}

items(listOf("Download", "Documents", "DCIM")) { folder ->
items(files.filter { it.isDirectory }) { folder ->
FolderBox(
name = folder
name = folder.name,
onClick = { onFolderClick(folder.path) }
)
}

items(
items = listOf("Effective Kotlin", "Android Developer"),
items = files.filter { !it.isDirectory },
span = { GridItemSpan(maxLineSpan) }
) { file ->
FileBox(
name = file,
name = file.name,
onFileClick = onFileClick
)
}
}
}
}

private fun readPDFOrDirectory(
path: String,
): List<Item> =
File(path).listFiles()?.filter { !it.isHidden && (it.isDirectory || it.extension == "pdf") }?.map {
Item(
name = it.name,
path = it.path,
type = it.extension,
isDirectory = it.isDirectory
)
}?: emptyList()

private data class Item(
val name: String,
val path: String,
val type: String,
val isDirectory: Boolean,
)

@Preview
@Composable
private fun Preview() {
SeeDocsTheme {
ExploreScreen(
path = "/storage/emulated/0/Download",
padding = PaddingValues(),
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package kr.co.navigation

import android.os.Environment
import androidx.compose.foundation.layout.PaddingValues
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import androidx.navigation.toRoute
import kr.co.explore.ExploreRoute

fun NavGraphBuilder.exploreNavGraph(
padding: PaddingValues,
navigateToFolder: (String) -> Unit = {},
navigateToPdf: () -> Unit = {}
) {
composable<MainNavigation.Explore> {
ExploreRoute(
path = it.toRoute<MainNavigation.Explore>().path?: Environment.getExternalStorageDirectory().absolutePath,
padding = padding,
navigateToFolder = navigateToFolder,
navigateToPdf = navigateToPdf
)
}
Expand Down
5 changes: 5 additions & 0 deletions feature/explore/src/main/java/kr/co/widget/FolderBox.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kr.co.widget

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
Expand All @@ -13,6 +14,7 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
Expand All @@ -25,9 +27,12 @@ import kr.co.ui.theme.Theme
internal fun FolderBox(
name: String,
modifier: Modifier = Modifier,
onClick: () -> Unit = {}
) {
Box(
modifier = modifier
.clip(RoundedCornerShape(8.dp))
.clickable(onClick = onClick)
.border(
width = 1.dp,
color = Theme.colors.grayText,
Expand Down
4 changes: 2 additions & 2 deletions feature/main/src/main/java/kr/co/main/MainBottomBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ internal fun MainBottomBar(
),
) {
tabs.forEach { tab ->
Item(
BottomItem(
tab = tab,
selected = tab == currentTab,
onClick = { onTabSelected(tab) }
Expand All @@ -67,7 +67,7 @@ internal fun MainBottomBar(
}

@Composable
private fun RowScope.Item(
private fun RowScope.BottomItem(
tab: MainTab,
selected: Boolean,
onClick: () -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kr.co.main.navigation
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.runtime.Composable
import androidx.navigation.compose.NavHost
import kr.co.navigation.MainNavigation
import kr.co.navigation.Route
import kr.co.navigation.bookmarkNavGraph
import kr.co.navigation.exploreNavGraph
Expand All @@ -20,6 +21,7 @@ internal fun MainNavHost(
) {
exploreNavGraph(
padding = padding,
navigateToFolder = { navigator.navigate(MainNavigation.Explore(it)) },
navigateToPdf = { navigator.navigate(Route.Pdf) }
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal class MainNavigator(
@Composable get() = navController
.currentBackStackEntryAsState().value?.destination

val startDestination: MainNavigation = MainNavigation.Explore
val startDestination: MainNavigation = MainNavigation.Explore()

@Composable
fun currentTab(): MainTab? =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal enum class MainTab(
Explore(
icon = SeeDocsIcon.Explore,
contentDescription = "Explore",
route = MainNavigation.Explore,
route = MainNavigation.Explore(),
),
Recent(
icon = SeeDocsIcon.RecentFill,
Expand Down

0 comments on commit 1f48fd7

Please sign in to comment.