-
-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: smarter and faster assets syncing
Finally I remember to utilize the checksums pre-calculated on build, comparing the sha256 among the files to determine how to handle them. Ref: https://github.com/fcitx5-android/fcitx5-android/blob/59558c5b624359455911082b10750f4dcbd10fe8/app/src/main/java/org/fcitx/fcitx5/android/core/data
- Loading branch information
1 parent
ff6b838
commit 571e7c4
Showing
6 changed files
with
182 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
app/src/main/java/com/osfans/trime/data/base/DataChecksums.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// SPDX-FileCopyrightText: 2015 - 2024 Rime community | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
package com.osfans.trime.data.base | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class DataChecksums( | ||
val sha256: String, | ||
val files: Map<String, String>, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// SPDX-FileCopyrightText: 2015 - 2024 Rime community | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
package com.osfans.trime.data.base | ||
|
||
sealed interface DataDiff { | ||
val path: String | ||
|
||
val ordinal: Int | ||
|
||
data class CreateFile(override val path: String) : DataDiff { | ||
override val ordinal: Int | ||
get() = 3 | ||
} | ||
|
||
data class UpdateFile(override val path: String) : DataDiff { | ||
override val ordinal: Int | ||
get() = 2 | ||
} | ||
|
||
data class DeleteDir(override val path: String) : DataDiff { | ||
override val ordinal: Int | ||
get() = 1 | ||
} | ||
|
||
data class DeleteFile(override val path: String) : DataDiff { | ||
override val ordinal: Int | ||
get() = 0 | ||
} | ||
|
||
companion object { | ||
fun diff( | ||
old: DataChecksums, | ||
new: DataChecksums, | ||
): List<DataDiff> { | ||
if (old.sha256 == new.sha256) return emptyList() | ||
return new.files.mapNotNull { (path, sha256) -> | ||
when { | ||
path !in old.files && sha256.isNotBlank() -> CreateFile(path) | ||
old.files[path] != sha256 -> | ||
if (sha256.isNotBlank()) UpdateFile(path) else null | ||
else -> null | ||
} | ||
}.toMutableList().apply { | ||
addAll( | ||
old.files.filterKeys { it !in new.files } | ||
.map { (path, sha256) -> | ||
if (sha256.isNotBlank()) DeleteFile(path) else DeleteDir(path) | ||
}, | ||
) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// SPDX-FileCopyrightText: 2015 - 2024 Rime community | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
package com.osfans.trime.util | ||
|
||
import java.io.File | ||
import java.io.IOException | ||
|
||
object FileUtils { | ||
fun delete(file: File) = | ||
runCatching { | ||
if (!file.exists()) return Result.success(Unit) | ||
val res = | ||
if (file.isDirectory) { | ||
file.walkBottomUp() | ||
.fold(true) { acc, file -> | ||
if (file.exists()) file.delete() else acc | ||
} | ||
} else { | ||
file.delete() | ||
} | ||
if (!res) { | ||
throw IOException("Cannot delete ${file.path}") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// SPDX-FileCopyrightText: 2015 - 2024 Rime community | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
package com.osfans.trime.util | ||
|
||
import java.io.File | ||
|
||
object ResourceUtils { | ||
fun copyFile( | ||
filename: String, | ||
dest: File, | ||
) = runCatching { | ||
appContext.assets.open(filename).use { i -> | ||
File(dest, filename) | ||
.also { it.parentFile?.mkdirs() } | ||
.outputStream() | ||
.use { o -> i.copyTo(o) } | ||
} | ||
} | ||
} |