Skip to content

Commit 6d9467c

Browse files
committed
Add customizable Hachimi repo
1 parent 446b047 commit 6d9467c

5 files changed

Lines changed: 66 additions & 3 deletions

File tree

app/src/main/java/com/leadrdrk/umapatcher/core/DataStore.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ object PrefKey {
1818
val CHECK_FOR_UPDATES = booleanPreferencesKey("check_for_updates")
1919
val LAST_UPDATE_CHECK = longPreferencesKey("last_update_check")
2020
val APP_LIBS_VERSION = stringPreferencesKey("app_libs_version")
21+
val HACHIMI_REPO = stringPreferencesKey("hachimi_repo")
2122
}
2223

2324
val defaultValues = mapOf(
2425
Pair(PrefKey.CHECK_FOR_UPDATES, true),
2526
Pair(PrefKey.LAST_UPDATE_CHECK, 0L),
2627
Pair(PrefKey.APP_LIBS_VERSION, "")
28+
Pair(PrefKey.HACHIMI_REPO, "Hachimi-Hachimi/Hachimi")
2729
)
2830

2931
suspend fun Context.getPrefValue(key: Preferences.Key<*>): Any? {

app/src/main/java/com/leadrdrk/umapatcher/patcher/AppPatcher.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ import java.io.File
3737
import java.io.IOException
3838
import java.net.URL
3939

40-
private const val LIBS_REPO_PATH = "kairusds/Hachimi-Edge"
41-
4240
private const val MOD_ARM64_LIB_NAME = "libmain-arm64-v8a.so"
4341
private const val APK_ARM64_LIB_DIR = "lib/arm64-v8a"
4442
private const val APK_ARM64_LIB_PATH = "$APK_ARM64_LIB_DIR/libmain.so"
@@ -599,10 +597,11 @@ class AppPatcher(
599597
if (!libsDir.exists()) libsDir.mkdir() || return null
600598

601599
val currentVer = context.getPrefValue(PrefKey.APP_LIBS_VERSION) as String?
600+
val modRepo = context.getPrefValue(PrefKey.HACHIMI_REPO) as String?
602601

603602
// Try syncing the libraries
604603
try {
605-
val releases = GitHubReleases(LIBS_REPO_PATH)
604+
val releases = GitHubReleases(modRepo)
606605
val latest = releases.fetchLatest()
607606
val tagName = latest["tag_name"] as String
608607

app/src/main/java/com/leadrdrk/umapatcher/ui/component/Option.kt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,59 @@ import androidx.compose.runtime.remember
2525
import androidx.compose.runtime.setValue
2626
import androidx.compose.ui.Alignment
2727
import androidx.compose.ui.Modifier
28+
import androidx.compose.ui.platform.LocalContext
2829
import androidx.compose.ui.text.input.KeyboardType
2930
import androidx.compose.ui.unit.dp
3031
import androidx.core.text.isDigitsOnly
3132

33+
@Composable
34+
fun rememberDataStoreStringState(
35+
key: androidx.datastore.preferences.core.Preferences.Key<String>,
36+
defaultValue: String
37+
): MutableState<String> {
38+
val context = LocalContext.current
39+
val dataStore = context.dataStore
40+
val coroutineScope = rememberCoroutineScope()
41+
42+
val savedFlow = remember(key) {
43+
dataStore.data.map { preferences ->
44+
preferences[key] ?: defaultValue
45+
}
46+
}
47+
val savedValue = savedFlow.collectAsState(initial = defaultValue).value
48+
49+
val state = remember {
50+
mutableStateOf(savedValue)
51+
}
52+
53+
LaunchedEffect(savedValue) {
54+
if (state.value != savedValue) {
55+
state.value = savedValue
56+
}
57+
}
58+
59+
return remember {
60+
object : MutableState<String> {
61+
override var value: String
62+
get() = state.value
63+
set(newValue) {
64+
if (state.value != newValue) {
65+
state.value = newValue
66+
67+
coroutineScope.launch {
68+
dataStore.edit { preferences ->
69+
preferences[key] = newValue
70+
}
71+
}
72+
}
73+
}
74+
75+
override fun component1() = value
76+
override fun component2(): (String) -> Unit = { value = it }
77+
}
78+
}
79+
}
80+
3281
@Composable
3382
fun OptionBase(
3483
title: String,

app/src/main/java/com/leadrdrk/umapatcher/ui/screen/SettingsScreen.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import com.leadrdrk.umapatcher.core.getPrefValue
2727
import com.leadrdrk.umapatcher.ui.component.BooleanOption
2828
import com.leadrdrk.umapatcher.ui.component.OptionBase
2929
import com.leadrdrk.umapatcher.ui.component.TopBar
30+
import com.leadrdrk.umapatcher.ui.component.rememberDataStoreStringState
3031
import com.leadrdrk.umapatcher.utils.ksFile
3132
import com.leadrdrk.umapatcher.utils.showToast
3233
import com.ramcosta.composedestinations.annotation.Destination
@@ -39,6 +40,11 @@ fun SettingsScreen() {
3940
val appLibsVersion = remember { mutableStateOf("") }
4041
var configRead by remember { mutableStateOf(false) }
4142

43+
val repoState = rememberDataStoreStringState(
44+
key = PrefKey.HACHIMI_REPO,
45+
defaultValue = defaultValues[PrefKey.HACHIMI_REPO] as String
46+
)
47+
4248
val exportKsLauncher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) {
4349
val uri = it.data?.data ?: return@rememberLauncherForActivityResult
4450
val ksFile = context.ksFile
@@ -123,6 +129,12 @@ fun SettingsScreen() {
123129
state = checkForUpdates
124130
)
125131

132+
StringOption(
133+
title = stringResource(R.string.hachimi_repo),
134+
state = repoState,
135+
placeholder = defaultValues[PrefKey.HACHIMI_REPO] as String
136+
)
137+
126138
OptionBase(
127139
title = stringResource(R.string.export_signing_key),
128140
desc = stringResource(R.string.export_signing_key_desc),

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
<!-- Settings -->
2323
<string name="check_for_updates_desc">Occasionally check for updates when the app starts.</string>
24+
<string name="hachimi_repo">Hachimi repo</string>
2425
<string name="export_signing_key">Export signing key</string>
2526
<string name="export_signing_key_desc">Export the BKS keystore used to sign APK files.</string>
2627
<string name="import_signing_key">Import signing key</string>

0 commit comments

Comments
 (0)