Skip to content

Commit

Permalink
iOS: 适配平台文件路径 #496
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed Sep 22, 2024
1 parent 0b41863 commit da7fc7a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
14 changes: 7 additions & 7 deletions app/shared/application/src/iosMain/kotlin/ios/AniIos.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024 OpenAni and contributors.
* Copyright (C) 2024 OpenAni and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link.
Expand Down Expand Up @@ -28,7 +28,6 @@ import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.io.files.Path
import me.him188.ani.app.data.source.media.resolver.HttpStreamingVideoSourceResolver
import me.him188.ani.app.data.source.media.resolver.LocalFileVideoSourceResolver
import me.him188.ani.app.data.source.media.resolver.TorrentVideoSourceResolver
Expand Down Expand Up @@ -66,9 +65,10 @@ import me.him188.ani.app.ui.main.AniApp
import me.him188.ani.app.ui.main.AniAppContent
import me.him188.ani.app.videoplayer.ui.state.DummyPlayerState
import me.him188.ani.app.videoplayer.ui.state.PlayerStateFactory
import me.him188.ani.utils.io.SystemCacheDir
import me.him188.ani.utils.io.SystemDocumentDir
import me.him188.ani.utils.io.SystemPath
import me.him188.ani.utils.io.inSystem
import me.him188.ani.utils.io.createDirectories
import me.him188.ani.utils.io.resolve
import me.him188.ani.utils.platform.annotations.TestOnly
import org.koin.core.context.startKoin
Expand All @@ -81,14 +81,14 @@ fun MainViewController(): UIViewController {

val context = IosContext(
IosContextFiles(
cacheDir = Path(SystemDocumentDir).resolve("cache").inSystem,
dataDir = Path(SystemDocumentDir).resolve("data").inSystem,
cacheDir = SystemCacheDir.apply { createDirectories() },
dataDir = SystemDocumentDir.apply { createDirectories() },
),
) // TODO IOS
)

val koin = startKoin {
modules(getCommonKoinModule({ context }, scope))
modules(getIosModules(Path(SystemDocumentDir).resolve("torrent").inSystem, scope)) // TODO IOS
modules(getIosModules(SystemDocumentDir.resolve("torrent"), scope))
}.startCommonKoinModule(scope).koin

koin.get<TorrentManager>() // start sharing, connect to DHT now
Expand Down
19 changes: 15 additions & 4 deletions app/shared/src/iosMain/kotlin/data/persistent/SettingsStore.ios.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
/*
* Copyright (C) 2024 OpenAni and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link.
*
* https://github.com/open-ani/ani/blob/main/LICENSE
*/

package me.him188.ani.app.data.persistent

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.mutablePreferencesOf
import kotlinx.io.files.Path
import me.him188.ani.app.platform.Context
import me.him188.ani.app.tools.caching.MemoryDataStore
import me.him188.ani.utils.io.SystemDocumentDir
import me.him188.ani.utils.io.SystemPath
import me.him188.ani.utils.io.inSystem
import me.him188.ani.utils.io.createDirectories
import me.him188.ani.utils.io.resolve

/**
Expand All @@ -17,7 +26,6 @@ actual val Context.dataStoresImpl: PlatformDataStoreManager
get() = IosPlatformDataStoreManager

object IosPlatformDataStoreManager : PlatformDataStoreManager() {
// TODO: IOS IosPlatformDataStoreManager
override val tokenStore: DataStore<Preferences> by lazy {
MemoryDataStore(mutablePreferencesOf())
}
Expand All @@ -28,5 +36,8 @@ object IosPlatformDataStoreManager : PlatformDataStoreManager() {
MemoryDataStore(mutablePreferencesOf())
}

override fun resolveDataStoreFile(name: String): SystemPath = Path(".").inSystem.resolve(name)
override fun resolveDataStoreFile(name: String): SystemPath =
SystemDocumentDir.resolve("datastores")
.apply { createDirectories() }
.resolve(name)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ package me.him188.ani.app.ui.foundation
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.remember
import kotlinx.io.files.Path
import me.him188.ani.app.platform.IosContext
import me.him188.ani.app.platform.IosContextFiles
import me.him188.ani.app.platform.LocalContext
import me.him188.ani.utils.io.inSystem
import me.him188.ani.utils.io.SystemCacheDir
import me.him188.ani.utils.io.SystemDocumentDir
import me.him188.ani.utils.platform.annotations.TestOnly

@Composable
Expand All @@ -26,7 +26,10 @@ internal actual inline fun ProvidePlatformCompositionLocalsForPreview(crossinlin
CompositionLocalProvider(
LocalContext provides remember {
IosContext(
IosContextFiles(Path(".").inSystem, Path(".").inSystem),
IosContextFiles(
cacheDir = SystemCacheDir,
dataDir = SystemDocumentDir,
),
)
},
) {
Expand Down
30 changes: 26 additions & 4 deletions utils/io/src/nativeMain/kotlin/Path.native.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
/*
* Copyright (C) 2024 OpenAni and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link.
*
* https://github.com/open-ani/ani/blob/main/LICENSE
*/

package me.him188.ani.utils.io

import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.convert
import kotlinx.io.files.Path
import kotlinx.io.files.SystemFileSystem
import kotlinx.io.files.SystemTemporaryDirectory
import me.him188.ani.utils.platform.Uuid
import platform.Foundation.NSCachesDirectory
import platform.Foundation.NSDocumentDirectory
import platform.Foundation.NSSearchPathForDirectoriesInDomains
import platform.Foundation.NSUserDomainMask
Expand Down Expand Up @@ -34,13 +45,24 @@ private fun resolveImpl(parent: String, child: String): String {
}

@OptIn(ExperimentalForeignApi::class)
val SystemDocumentDir
get() = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory.convert(), NSUserDomainMask.convert(), true)
.firstOrNull()?.toString() ?: error("Cannot get current working directory")
val SystemDocumentDir by lazy {
Path(
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory.convert(), NSUserDomainMask.convert(), true)
.firstOrNull()?.toString() ?: error("Cannot get SystemDocumentDir"),
).inSystem
}

@OptIn(ExperimentalForeignApi::class)
val SystemCacheDir by lazy {
Path(
NSSearchPathForDirectoriesInDomains(NSCachesDirectory.convert(), NSUserDomainMask.convert(), true)
.firstOrNull()?.toString() ?: error("Cannot get SystemCacheDir"),
).inSystem
}

actual val SystemPath.absolutePath: String
get() {
return resolveImpl(SystemDocumentDir, path.toString())
return resolveImpl(SystemDocumentDir.toString(), path.toString())
}

actual fun SystemPaths.createTempDirectory(
Expand Down

0 comments on commit da7fc7a

Please sign in to comment.