Skip to content

Commit 9419f20

Browse files
committed
fix structure
1 parent cd77dcd commit 9419f20

3 files changed

Lines changed: 72 additions & 55 deletions

File tree

composeApp/src/commonMain/kotlin/neth/iecal/trease/ui/components/TreeGrowthPlayer.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import kotlinx.coroutines.delay
2626
import neth.iecal.trease.PlatformVideoPlayer
2727
import neth.iecal.trease.models.TimerStatus
2828
import neth.iecal.trease.utils.CacheManager
29+
import neth.iecal.trease.utils.getCachedVideoPath
2930
import neth.iecal.trease.viewmodels.HomeScreenViewModel
3031

3132

@@ -71,11 +72,10 @@ fun TreeGrowthPlayer(
7172
)
7273

7374
LaunchedEffect(selectedTreeId, selectedMinutes) {
74-
val cacheManager = CacheManager()
7575
val remoteUrl = "https://trease-focus.github.io/cache-trees/video/$selectedTreeId.webm"
7676

7777
try {
78-
val localPath = cacheManager.getCachedVideoPath(remoteUrl, selectedTreeId)
78+
val localPath = getCachedVideoPath(remoteUrl, selectedTreeId)
7979
statePlayer.openUri(localPath)
8080
isReady = true
8181

composeApp/src/commonMain/kotlin/neth/iecal/trease/utils/CacheManager.kt

Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,69 +16,25 @@ import okio.use
1616

1717
class CacheManager {
1818

19-
private val client = getPlatformHttpClient()
2019
private val fileSystem = getFileSystem()
2120

2221

23-
suspend fun getCachedVideoPath(
24-
url: String,
25-
videoId: String
26-
): String {
27-
val cachePath = getCacheDir().toPath()
28-
29-
if (!fileSystem.exists(cachePath)) {
30-
fileSystem.createDirectories(cachePath)
31-
}
32-
33-
val filePath = cachePath.resolve("$videoId.webm")
34-
35-
if (fileSystem.exists(filePath)) {
36-
return filePath.toString()
37-
}
38-
39-
try {
40-
val response = client.get(url)
41-
val channel = response.bodyAsChannel()
42-
43-
val sink = fileSystem.sink(filePath).buffer()
44-
45-
try {
46-
while (!channel.isClosedForRead) {
47-
val packet = channel.readRemaining(8_192)
48-
while (!packet.isEmpty) {
49-
sink.write(packet.readBytes())
50-
}
51-
}
52-
} finally {
53-
sink.flush()
54-
sink.close()
55-
}
56-
57-
} catch (e: Exception) {
58-
if (fileSystem.exists(filePath)) {
59-
fileSystem.delete(filePath)
60-
}
61-
throw e
62-
}
63-
64-
return filePath.toString()
65-
}
66-
67-
6822
suspend fun saveFile(
6923
fileName: String,
7024
content: String
7125
) {
72-
val cachePath = getCacheDir().toPath()
26+
return withContext(Dispatchers.Main) {
27+
val cachePath = getCacheDir().toPath()
7328

74-
if (!fileSystem.exists(cachePath)) {
75-
fileSystem.createDirectories(cachePath)
76-
}
29+
if (!fileSystem.exists(cachePath)) {
30+
fileSystem.createDirectories(cachePath)
31+
}
7732

78-
val filePath = cachePath.resolve(fileName)
33+
val filePath = cachePath.resolve(fileName)
7934

80-
fileSystem.sink(filePath).buffer().use { sink ->
81-
sink.writeUtf8(content)
35+
fileSystem.sink(filePath).buffer().use { sink ->
36+
sink.writeUtf8(content)
37+
}
8238
}
8339
}
8440

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package neth.iecal.trease.utils
2+
3+
import io.ktor.client.request.get
4+
import io.ktor.client.statement.bodyAsChannel
5+
import io.ktor.utils.io.core.isEmpty
6+
import io.ktor.utils.io.core.readBytes
7+
import io.ktor.utils.io.readRemaining
8+
import neth.iecal.trease.getCacheDir
9+
import neth.iecal.trease.getFileSystem
10+
import neth.iecal.trease.getPlatformHttpClient
11+
import okio.Path.Companion.toPath
12+
import okio.buffer
13+
14+
15+
suspend fun getCachedVideoPath(
16+
url: String,
17+
videoId: String
18+
): String {
19+
20+
val client = getPlatformHttpClient()
21+
val fileSystem = getFileSystem()
22+
23+
val cachePath = getCacheDir().toPath()
24+
25+
if (!fileSystem.exists(cachePath)) {
26+
fileSystem.createDirectories(cachePath)
27+
}
28+
29+
val filePath = cachePath.resolve("$videoId.webm")
30+
31+
if (fileSystem.exists(filePath)) {
32+
return filePath.toString()
33+
}
34+
35+
try {
36+
val response = client.get(url)
37+
val channel = response.bodyAsChannel()
38+
39+
val sink = fileSystem.sink(filePath).buffer()
40+
41+
try {
42+
while (!channel.isClosedForRead) {
43+
val packet = channel.readRemaining(8_192)
44+
while (!packet.isEmpty) {
45+
sink.write(packet.readBytes())
46+
}
47+
}
48+
} finally {
49+
sink.flush()
50+
sink.close()
51+
}
52+
53+
} catch (e: Exception) {
54+
if (fileSystem.exists(filePath)) {
55+
fileSystem.delete(filePath)
56+
}
57+
throw e
58+
}
59+
60+
return filePath.toString()
61+
}

0 commit comments

Comments
 (0)