@@ -50,6 +50,8 @@ import androidx.compose.ui.unit.sp
5050import androidx.navigation.NavController
5151import androidx.navigation.NavHostController
5252import androidx.navigation.compose.rememberNavController
53+ import kotlinx.coroutines.CoroutineScope
54+ import kotlinx.coroutines.Dispatchers
5355import kotlinx.coroutines.launch
5456import org.vonderheidt.hips.data.HiPSDataStore
5557import org.vonderheidt.hips.data.Settings
@@ -189,14 +191,19 @@ fun SettingsScreen(navController: NavController, modifier: Modifier) {
189191 Button (
190192 onClick = {
191193 // Check if the LLM is in memory already, load/unload it and update the state variable
192- // Needs coroutineScope.launch{...} around every function call to update the state only after loading/unloading
194+ // Use coroutine with Dispatchers.IO since loading the LLM is I/O-bound, doesn't block UI in main thread anymore this way
193195 if (! isInMemory) {
194- coroutineScope.launch { LlamaCpp .startInstance() }
196+ CoroutineScope (Dispatchers .IO ).launch {
197+ LlamaCpp .startInstance()
198+ isInMemory = LlamaCpp .isInMemory()
199+ }
195200 }
196201 else {
197- coroutineScope.launch { LlamaCpp .stopInstance() }
202+ CoroutineScope (Dispatchers .IO ).launch {
203+ LlamaCpp .stopInstance()
204+ isInMemory = LlamaCpp .isInMemory()
205+ }
198206 }
199- coroutineScope.launch { isInMemory = LlamaCpp .isInMemory() }
200207 },
201208 shape = RoundedCornerShape (4 .dp)
202209 ) {
0 commit comments