From da7fc7ad6f371f296db7afb8cfc381e26d4223aa Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 22 Sep 2024 11:20:20 +0100 Subject: [PATCH] =?UTF-8?q?iOS:=20=E9=80=82=E9=85=8D=E5=B9=B3=E5=8F=B0?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=B7=AF=E5=BE=84=20#496?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/iosMain/kotlin/ios/AniIos.kt | 14 ++++----- .../data/persistent/SettingsStore.ios.kt | 19 +++++++++--- .../ui/foundation/PreviewFoundation.ios.kt | 9 ++++-- utils/io/src/nativeMain/kotlin/Path.native.kt | 30 ++++++++++++++++--- 4 files changed, 54 insertions(+), 18 deletions(-) diff --git a/app/shared/application/src/iosMain/kotlin/ios/AniIos.kt b/app/shared/application/src/iosMain/kotlin/ios/AniIos.kt index fce58dd6a8..d725edbed1 100644 --- a/app/shared/application/src/iosMain/kotlin/ios/AniIos.kt +++ b/app/shared/application/src/iosMain/kotlin/ios/AniIos.kt @@ -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. @@ -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 @@ -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 @@ -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() // start sharing, connect to DHT now diff --git a/app/shared/src/iosMain/kotlin/data/persistent/SettingsStore.ios.kt b/app/shared/src/iosMain/kotlin/data/persistent/SettingsStore.ios.kt index a57887c8a1..e05e3288a8 100644 --- a/app/shared/src/iosMain/kotlin/data/persistent/SettingsStore.ios.kt +++ b/app/shared/src/iosMain/kotlin/data/persistent/SettingsStore.ios.kt @@ -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 /** @@ -17,7 +26,6 @@ actual val Context.dataStoresImpl: PlatformDataStoreManager get() = IosPlatformDataStoreManager object IosPlatformDataStoreManager : PlatformDataStoreManager() { - // TODO: IOS IosPlatformDataStoreManager override val tokenStore: DataStore by lazy { MemoryDataStore(mutablePreferencesOf()) } @@ -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) } diff --git a/app/shared/ui-foundation/src/iosMain/kotlin/ui/foundation/PreviewFoundation.ios.kt b/app/shared/ui-foundation/src/iosMain/kotlin/ui/foundation/PreviewFoundation.ios.kt index cf9c2b4fb3..f349e431e6 100644 --- a/app/shared/ui-foundation/src/iosMain/kotlin/ui/foundation/PreviewFoundation.ios.kt +++ b/app/shared/ui-foundation/src/iosMain/kotlin/ui/foundation/PreviewFoundation.ios.kt @@ -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 @@ -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, + ), ) }, ) { diff --git a/utils/io/src/nativeMain/kotlin/Path.native.kt b/utils/io/src/nativeMain/kotlin/Path.native.kt index 67defbd780..4a22f4ec5f 100644 --- a/utils/io/src/nativeMain/kotlin/Path.native.kt +++ b/utils/io/src/nativeMain/kotlin/Path.native.kt @@ -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 @@ -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(