refactor lister

This commit is contained in:
Kagura 2024-10-11 21:11:00 +08:00
parent 9d724496c0
commit be15a90b82
7 changed files with 54 additions and 48 deletions

1
.gitignore vendored
View file

@ -13,3 +13,4 @@
.externalNativeBuild
.cxx
local.properties
/.idea/runConfigurations.xml

View file

@ -7,7 +7,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File
class DocumentLister : Lister() {
class DocumentLister : Lister {
companion object {
val instance by lazy { DocumentLister() }
val directories = listOf("Documents", "Download")
@ -31,7 +31,7 @@ class DocumentLister : Lister() {
return
}
fun dateOrderedList(): List<String> {
override fun dateOrderedList(): List<String> {
val wrappedFileList = mutableListOf<WrappedFile>()
documentList.forEach { wrappedFileList.add(WrappedFile(File(it))) }
wrappedFileList.sortBy { it.lastModifiedTime }
@ -40,7 +40,7 @@ class DocumentLister : Lister() {
return result
}
fun sizeOrderedList(): List<String> {
override fun sizeOrderedList(): List<String> {
val wrappedFileList = mutableListOf<WrappedFile>()
documentList.forEach { wrappedFileList.add(WrappedFile(File(it))) }
wrappedFileList.sortBy { it.size }
@ -49,7 +49,7 @@ class DocumentLister : Lister() {
return result
}
fun getFullSize(): ULong {
override fun getFullSize(): ULong {
var size = 0UL
val wrappedFileList = mutableListOf<WrappedFile>()
documentList.forEach { wrappedFileList.add(WrappedFile(File(it))) }

View file

@ -7,14 +7,14 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File
class ImageLister private constructor() : Lister() {
class ImageLister private constructor() : Lister {
companion object {
val instance by lazy { ImageLister() }
val directories = listOf("DCIM", "Pictures", "Download")
val regex = "\\.(jpg|png|jpeg|webp)".toRegex()
val regex = "\\.(jpg|png|jpeg|webp)$".toRegex()
}
val imageList = mutableListOf<String>()
private val imageList = mutableListOf<String>()
fun initialize(onFinished: (() -> Unit)? = null) {
imageList.clear()
@ -31,7 +31,7 @@ class ImageLister private constructor() : Lister() {
return
}
fun dateOrderedList(): List<String> {
override fun dateOrderedList(): List<String> {
val wrappedFileList = mutableListOf<WrappedFile>()
imageList.forEach { wrappedFileList.add(WrappedFile(File(it))) }
wrappedFileList.sortBy { it.lastModifiedTime }
@ -40,7 +40,7 @@ class ImageLister private constructor() : Lister() {
return result
}
fun sizeOrderedList(): List<String> {
override fun sizeOrderedList(): List<String> {
val wrappedFileList = mutableListOf<WrappedFile>()
imageList.forEach { wrappedFileList.add(WrappedFile(File(it))) }
wrappedFileList.sortBy { it.size }
@ -49,7 +49,7 @@ class ImageLister private constructor() : Lister() {
return result
}
fun getFullSize(): ULong {
override fun getFullSize(): ULong {
var size = 0UL
val wrappedFileList = mutableListOf<WrappedFile>()
imageList.forEach { wrappedFileList.add(WrappedFile(File(it))) }

View file

@ -1,4 +1,34 @@
package com.example.myapplication.fileSystem.byTypeFileLister
abstract class Lister {
}
import java.io.File
interface Lister {
fun dateOrderedList(): List<String>
fun sizeOrderedList(): List<String>
fun getFullSize(): ULong
fun walkDir(
directory: File,
list: MutableList<String>,
pattern: Regex,
ignoreDotFile: Boolean = true
) {
if (!directory.exists() || !directory.isDirectory) {
return
}
directory.listFiles()?.forEach {
if (ignoreDotFile && !it.name.startsWith(".")) {
if (it.isDirectory) {
walkDir(it, list, pattern)
} else if (it.isFile) {
if (it.name.contains(pattern)) {
list.add(it.path)
}
}
}
}
}
}

View file

@ -7,15 +7,15 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File
class MusicLister : Lister() {
class MusicLister : Lister {
companion object {
val instance by lazy { MusicLister() }
val directories =
listOf("Recordings", "Download", "Audiobooks", "Music", "Podcasts", "Ringtones")
val regex = "\\.(mp3|ogg|aac|wav)".toRegex()
val regex = "\\.(mp3|ogg|aac|wav)$".toRegex()
}
val musicList = mutableListOf<String>()
private val musicList = mutableListOf<String>()
fun initialize(onFinished: (() -> Unit)? = null) {
musicList.clear()
@ -32,7 +32,7 @@ class MusicLister : Lister() {
return
}
fun dateOrderedList(): List<String> {
override fun dateOrderedList(): List<String> {
val wrappedFileList = mutableListOf<WrappedFile>()
musicList.forEach { wrappedFileList.add(WrappedFile(File(it))) }
wrappedFileList.sortBy { it.lastModifiedTime }
@ -41,7 +41,7 @@ class MusicLister : Lister() {
return result
}
fun sizeOrderedList(): List<String> {
override fun sizeOrderedList(): List<String> {
val wrappedFileList = mutableListOf<WrappedFile>()
musicList.forEach { wrappedFileList.add(WrappedFile(File(it))) }
wrappedFileList.sortBy { it.size }
@ -50,7 +50,7 @@ class MusicLister : Lister() {
return result
}
fun getFullSize(): ULong {
override fun getFullSize(): ULong {
var size = 0UL
val wrappedFileList = mutableListOf<WrappedFile>()
musicList.forEach { wrappedFileList.add(WrappedFile(File(it))) }

View file

@ -7,11 +7,11 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File
class VideoLister : Lister() {
class VideoLister : Lister {
companion object {
val instance by lazy { VideoLister() }
val directories = listOf("DCIM", "Download", "Movies")
val regex = "\\.(mp4|avi|video|webm)".toRegex()
val regex = "\\.(mp4|avi|video|webm)$".toRegex()
}
val videoList = mutableListOf<String>()
@ -31,7 +31,7 @@ class VideoLister : Lister() {
return
}
fun dateOrderedList(): List<String> {
override fun dateOrderedList(): List<String> {
val wrappedFileList = mutableListOf<WrappedFile>()
videoList.forEach { wrappedFileList.add(WrappedFile(File(it))) }
wrappedFileList.sortBy { it.lastModifiedTime }
@ -40,7 +40,7 @@ class VideoLister : Lister() {
return result
}
fun sizeOrderedList(): List<String> {
override fun sizeOrderedList(): List<String> {
val wrappedFileList = mutableListOf<WrappedFile>()
videoList.forEach { wrappedFileList.add(WrappedFile(File(it))) }
wrappedFileList.sortBy { it.size }
@ -49,7 +49,7 @@ class VideoLister : Lister() {
return result
}
fun getFullSize(): ULong {
override fun getFullSize(): ULong {
var size = 0UL
val wrappedFileList = mutableListOf<WrappedFile>()
videoList.forEach { wrappedFileList.add(WrappedFile(File(it))) }

View file

@ -1,25 +0,0 @@
package com.example.myapplication.fileSystem.byTypeFileLister
import java.io.File
fun Lister.walkDir(
directory: File,
list: MutableList<String>,
pattern: Regex,
ignoreDotFile: Boolean = true
) {
if (!directory.exists() || !directory.isDirectory) {
return
}
directory.listFiles()?.forEach {
if (ignoreDotFile && !it.name.startsWith(".")) {
if (it.isDirectory) {
walkDir(it, list, pattern)
} else if (it.isFile) {
if (it.name.contains(pattern)) {
list.add(it.path)
}
}
}
}
}