Skip to content

Commit b3b824e

Browse files
author
tanpengcheng
committed
[feat] Update MyDir view.
1 parent b591ec8 commit b3b824e

File tree

3 files changed

+52
-34
lines changed

3 files changed

+52
-34
lines changed

app/src/main/java/com/tans/tfiletransporter/file/FileUtils.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,17 @@ fun List<FileLeaf.CommonFileLeaf>.toExploreFiles(): List<FileExploreFile> {
8585
lastModify = it.lastModified
8686
)
8787
}
88+
}
89+
90+
fun File.hasTargetParent(targetParent: File): Boolean {
91+
val parent = parentFile
92+
return if (parent == null) {
93+
false
94+
} else {
95+
if (parent.canonicalPath == targetParent.canonicalPath) {
96+
true
97+
} else {
98+
parent.hasTargetParent(targetParent)
99+
}
100+
}
88101
}
Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
package com.tans.tfiletransporter.file
22

3+
import android.content.Context
4+
import android.os.Environment
35
import java.io.File
46

57

6-
fun File.toFileLeaf(rootDirString: String): FileLeaf.CommonFileLeaf {
8+
fun File.toFileLeaf(): FileLeaf.CommonFileLeaf {
79
return FileLeaf.CommonFileLeaf(
810
name = name,
9-
path = canonicalPath.removePrefix(rootDirString),
11+
path = canonicalPath,
1012
size = length(),
1113
lastModified = lastModified()
1214
)
1315
}
1416

15-
fun File.toDirLeaf(rootDirString: String): FileLeaf.DirectoryFileLeaf {
17+
fun File.toDirLeaf(): FileLeaf.DirectoryFileLeaf {
1618
return FileLeaf.DirectoryFileLeaf(
1719
name = name,
18-
path = canonicalPath.removePrefix(rootDirString),
20+
path = canonicalPath,
1921
childrenCount = listFiles()?.size?.toLong() ?: 0L,
2022
lastModified = lastModified()
2123
)
2224
}
2325

24-
fun File.childrenLeafs(rootDirString: String): Pair<List<FileLeaf.DirectoryFileLeaf>, List<FileLeaf.CommonFileLeaf>> {
26+
fun File.childrenLeafs(): Pair<List<FileLeaf.DirectoryFileLeaf>, List<FileLeaf.CommonFileLeaf>> {
2527
val children = listFiles() ?: emptyArray<File>()
2628
val resultFiles = mutableListOf<FileLeaf.CommonFileLeaf>()
2729
val resultDirs = mutableListOf<FileLeaf.DirectoryFileLeaf>()
@@ -30,10 +32,10 @@ fun File.childrenLeafs(rootDirString: String): Pair<List<FileLeaf.DirectoryFileL
3032
if (c.canRead()) {
3133
if (c.isFile) {
3234
if (c.length() > 0) {
33-
resultFiles.add(c.toFileLeaf(rootDirString))
35+
resultFiles.add(c.toFileLeaf())
3436
}
3537
} else {
36-
resultDirs.add(c.toDirLeaf(rootDirString))
38+
resultDirs.add(c.toDirLeaf())
3739
}
3840
}
3941
} catch (e: Throwable) {
@@ -43,35 +45,42 @@ fun File.childrenLeafs(rootDirString: String): Pair<List<FileLeaf.DirectoryFileL
4345
return resultDirs to resultFiles
4446
}
4547

46-
fun createLocalRootTree(rootFile: File): FileTree {
48+
fun createLocalRootTree(context: Context): FileTree {
4749
val fileSeparator = File.separator
48-
return if (rootFile.canRead()) {
49-
val (dirLeafs, fileLeafs) = rootFile.childrenLeafs(rootFile.canonicalPath)
50-
FileTree(
51-
dirLeafs = dirLeafs,
52-
fileLeafs = fileLeafs,
53-
path = fileSeparator,
54-
parentTree = null
50+
val defaultStorageFile = Environment.getExternalStorageDirectory()
51+
val othersSdCardFiles =
52+
getSdCardPaths(context, true).map { File(it) }.filter { !defaultStorageFile.hasTargetParent(it) }
53+
val defaultStorageLeaf = if (defaultStorageFile.canRead()) {
54+
listOf(
55+
FileLeaf.DirectoryFileLeaf(
56+
name = "Default Storage",
57+
path = defaultStorageFile.canonicalPath,
58+
childrenCount = defaultStorageFile.listFiles()?.size?.toLong() ?: 0L,
59+
lastModified = defaultStorageFile.lastModified()
60+
)
5561
)
5662
} else {
57-
FileTree(
58-
dirLeafs = emptyList(),
59-
fileLeafs = emptyList(),
60-
path = fileSeparator,
61-
parentTree = null
62-
)
63+
emptyList()
64+
}
65+
val sdCardLeafs = othersSdCardFiles.map {
66+
it.toDirLeaf()
6367
}
68+
return FileTree(
69+
dirLeafs = defaultStorageLeaf + sdCardLeafs,
70+
fileLeafs = emptyList(),
71+
path = fileSeparator,
72+
parentTree = null
73+
)
6474
}
6575

6676
fun FileTree.newLocalSubTree(
67-
dirLeaf: FileLeaf.DirectoryFileLeaf,
68-
rootFile: File): FileTree {
69-
val file = File(rootFile, dirLeaf.path)
70-
val (dirLeafs, fileLeafs) = file.childrenLeafs(rootFile.canonicalPath)
77+
dirLeaf: FileLeaf.DirectoryFileLeaf): FileTree {
78+
val file = File(dirLeaf.path)
79+
val (dirLeafs, fileLeafs) = file.childrenLeafs()
7180
return FileTree(
7281
dirLeafs = dirLeafs,
7382
fileLeafs = fileLeafs,
74-
path = file.canonicalPath.removePrefix(rootFile.canonicalPath),
83+
path = file.canonicalPath,
7584
parentTree = this
7685
)
7786
}

app/src/main/java/com/tans/tfiletransporter/ui/activity/filetransport/MyDirFragment.kt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ import kotlin.math.min
4343

4444
class MyDirFragment : BaseFragment<MyDirFragmentBinding, MyDirFragment.Companion.MyDirFragmentState>(R.layout.my_dir_fragment, MyDirFragmentState()) {
4545

46-
private val rootDir: File by lazy {
47-
(requireActivity() as FileTransportActivity).rootDirFile
48-
}
49-
5046
private val recyclerViewScrollChannel = Channel<Int>(1)
5147

5248
private val folderPositionDeque: Deque<Int> = ArrayDeque()
@@ -73,7 +69,7 @@ class MyDirFragment : BaseFragment<MyDirFragmentBinding, MyDirFragment.Companion
7369
override fun initViews(binding: MyDirFragmentBinding) {
7470
launch(Dispatchers.IO) {
7571
updateState {
76-
it.copy(fileTree = createLocalRootTree(rootDir))
72+
it.copy(fileTree = createLocalRootTree(requireContext()))
7773
}.await()
7874
}
7975

@@ -121,7 +117,7 @@ class MyDirFragment : BaseFragment<MyDirFragmentBinding, MyDirFragment.Companion
121117
folderPositionDeque.push(i)
122118
updateState { oldState ->
123119
oldState.copy(
124-
fileTree = oldState.fileTree.newLocalSubTree(data, rootDir),
120+
fileTree = oldState.fileTree.newLocalSubTree(data),
125121
selectedFiles = emptySet()
126122
)
127123
}.await()
@@ -220,15 +216,15 @@ class MyDirFragment : BaseFragment<MyDirFragmentBinding, MyDirFragment.Companion
220216
val oldTree = oldState.fileTree
221217
if (oldTree.isRootFileTree()) {
222218
oldState.copy(
223-
fileTree = createLocalRootTree(rootDir),
219+
fileTree = createLocalRootTree(requireContext()),
224220
selectedFiles = emptySet()
225221
)
226222
} else {
227223
val parentTree = oldTree.parentTree
228224
val dirLeaf = parentTree?.dirLeafs?.find { it.path == oldTree.path }
229225
if (parentTree != null && dirLeaf != null) {
230226
oldState.copy(
231-
fileTree = parentTree.newLocalSubTree(dirLeaf, rootDir),
227+
fileTree = parentTree.newLocalSubTree(dirLeaf),
232228
selectedFiles = emptySet()
233229
)
234230
} else {

0 commit comments

Comments
 (0)