Where to put the code that creates local files in android?

huangapple go评论61阅读模式
英文:

Where to put the code that creates local files in android?

问题

我正在尝试创建一个应用程序(使用Kotlin和Jetpack Compose),该应用程序可以将文件保存到内部存储,但我不知道将代码放在哪个位置最合适,例如这段代码:

fun deleteFile(context: Context, filePath: String): Boolean {
    
    val path = context.filesDir
    val file = File(path, filePath)

    if (!file.exists()) return false
    
    return file.deleteRecursively()
}

由于我需要上下文来处理文件,所以这段代码必须放在UI层,对吗?但这不是业务代码吗?如果是业务代码,我不能将其放在数据层,因为我需要上下文,将上下文传递给viewModel也不是一个好方法,那么应该放在哪里呢?

英文:

I'm trying to create an app (using Kotlin with Jetpack Compose) that can save files to internal storage, but I don't know what is the best place to put for example this code:

fun deleteFile(context: Context, filePath: String): Boolean {
    
    val path = context.filesDir
    val file = File(path, filePath)

    if (!file.exists()) return false
    
    return file.deleteRecursively()
}

Since I need the context to handling files the code must be in the UI layer right? But isn't it a business code? If is business code I can't put it in the data layer because I need context, passing the context to the viewModel is also not good, so where?

答案1

得分: 0

通常,在遵循推荐实践的 Android 应用程序架构中,您会拥有独立的层,例如 UI 层(由活动/片段和 Jetpack Compose 组成),ViewModel 层和数据层(包括存储库和数据源)。为了处理文件操作,您可以引入存储库模式。

FileRepository 类充当 ViewModel 和文件系统之间的中介,利用应用程序上下文来从内部存储中删除文件。

class FileRepository(private val application: Application) {

    fun deleteFile(filePath: String): Boolean {
        val path = application.filesDir
        val file = File(path, filePath)

        if (!file.exists()) return false

        return file.deleteRecursively()
    }
}

MyViewModel 类扩展了 AndroidViewModel 并持有 FileRepository 的实例。它充当了 UI 层和文件处理逻辑之间的桥梁。

class MyViewModel(application: Application) : AndroidViewModel(application) {

    private val fileRepository by lazy { FileRepository(application) }
    
    fun deleteFile(filePath: String): Boolean {
        return fileRepository.deleteFile(filePath)
    }
}

示例用法

@Composable
fun Example(myViewModel: MyViewModel = viewModel()) {
    // UI
}

该函数将 MyViewModel 的实例作为参数,使用 viewModel() 函数获取默认值。在函数内部,您可以定义特定于此组件的 UI 元素和布局。

英文:

Typically, in an Android app architecture that follows the recommended practices, you would have separate layers such as UI layer (composed of activities/fragments and Jetpack Compose), ViewModel layer, and Data layer (including repositories and data sources). To handle file operations, you can introduce a repository pattern.

The FileRepository class acts as an intermediary between the ViewModel and the file system, utilizing the application context to delete files from internal storage.

class FileRepository(private val application: Application) {

    fun deleteFile(filePath: String): Boolean {
        val path = application.filesDir
        val file = File(path, filePath)

        if (!file.exists()) return false

        return file.deleteRecursively()
    }
}

The MyViewModel class extends AndroidViewModel and holds an instance of the FileRepository. It serves as a bridge between the UI layer and the file handling logic,

class MyViewModel(application: Application) : AndroidViewModel(application) {

    private val fileRepository by lazy { FileRepository(application) }
    
    fun deleteFile(filePath: String): Boolean {
        return fileRepository.deleteFile(filePath)
    }
}

Example usage

@Composable
fun Example(myViewModel: MyViewModel = viewModel()) {
    // UI
}

The function takes an instance of MyViewModel as a parameter, with a default value obtained using the viewModel() function. Inside the function, you would define the UI elements and layout specific to this component.

huangapple
  • 本文由 发表于 2023年7月7日 01:20:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定