All Downloads are FREE. Search and download functionalities are using the official Maven repository.

iosMain.com.apadmi.mockzilla.lib.internal.utils.FileIo.kt Maven / Gradle / Ivy

Go to download

Solution for running and configuring a local HTTP server on mobile.

There is a newer version: 2.1.0
Show newest version
package com.apadmi.mockzilla.lib.internal.utils

import platform.Foundation.*

internal actual class FileIo {
    private val directoryPath by lazy {
        val dir = NSSearchPathForDirectoriesInDomains(
            NSCachesDirectory,
            NSUserDomainMask,
            true
        ).firstOrNull() as? String ?: throw IllegalStateException("Failed to get cache directory :(")
        "${dir.removeSuffix("/")}/com.apadmi.mockzilla.lib"
    }
    actual suspend fun readFromCache(filename: String): String? {
        val filePath = filePath(filename)
        return NSString.stringWithContentsOfFile(filePath, NSUTF8StringEncoding, null)
    }

    actual suspend fun saveToCache(filename: String, contents: String) {
        val filePath = filePath(filename)
        NSFileManager.defaultManager.createDirectoryAtPath(directoryPath, true, null, null)

        (contents as NSString).writeToFile(filePath, true, NSUTF8StringEncoding, null)
    }

    actual suspend fun deleteCacheFile(filename: String) {
        NSFileManager.defaultManager.removeItemAtPath(filePath(filename), null)
    }

    actual suspend fun deleteAllCaches() {
        NSFileManager.defaultManager.removeItemAtPath(directoryPath, null)
    }

    private fun filePath(filename: String) = "$directoryPath/$filename"
}

internal actual fun createFileIoforTesting() = FileIo()




© 2015 - 2025 Weber Informatics LLC | Privacy Policy