Skip to content

Commit

Permalink
Added wrapper for resources (refactoring)
Browse files Browse the repository at this point in the history
  • Loading branch information
sirekanian committed Jan 6, 2024
1 parent 5de0db7 commit 2402068
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 9 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/org/sirekanyan/outline/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class App : Application() {
val keyRepository by lazy { KeyRepository(api, KeyDao(database), serverDao) }
val prefsDao by lazy { KeyValueDao(database) }
val debugDao: DebugDao by lazy { DebugDaoImpl(database) }
val res: Res by lazy { ResImpl(this) }

override fun attachBaseContext(base: Context?) {
super.attachBaseContext(base)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/sirekanyan/outline/MainContent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ fun MainContent(state: MainState) {
if (hasKeys) {
val context = LocalContext.current
LaunchedEffect(Unit) {
context.showToast(R.string.outln_network_error)
context.showToast(R.string.outln_error_check_network)
}
} else {
KeysErrorContent(
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/java/org/sirekanyan/outline/Res.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.sirekanyan.outline

import androidx.annotation.StringRes
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext

@Composable
fun rememberResources(): Res {
val context = LocalContext.current
return remember { context.app().res }
}

interface Res {
fun getString(@StringRes resId: Int): String
}

class ResImpl(private val app: App) : Res {
override fun getString(resId: Int): String =
app.getString(resId)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fun KeysErrorContent(insets: PaddingValues, onRetry: () -> Unit) {
horizontalAlignment = Alignment.CenterHorizontally,
) {
Text(
text = stringResource(R.string.outln_network_error),
text = stringResource(R.string.outln_error_check_network),
modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.Center,
)
Expand Down
11 changes: 8 additions & 3 deletions app/src/main/java/org/sirekanyan/outline/ui/AddServerContent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ import androidx.compose.ui.unit.dp
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.sirekanyan.outline.NotSupportedContent
import org.sirekanyan.outline.R
import org.sirekanyan.outline.Res
import org.sirekanyan.outline.Router
import org.sirekanyan.outline.SelectedPage
import org.sirekanyan.outline.api.model.createServerEntity
import org.sirekanyan.outline.app
import org.sirekanyan.outline.ext.rememberStateScope
import org.sirekanyan.outline.rememberResources
import org.sirekanyan.outline.repository.ServerRepository
import javax.net.ssl.SSLException

Expand All @@ -45,7 +48,8 @@ private fun rememberAddServerState(router: Router): AddServerState {
val servers = remember { context.app().serverRepository }
val draft = rememberSaveable { mutableStateOf("") }
val insecure = rememberSaveable { mutableStateOf(false) }
return remember { AddServerState(scope, router, servers, draft, insecure) }
val resources = rememberResources()
return remember { AddServerState(scope, router, servers, draft, insecure, resources) }
}

private class AddServerState(
Expand All @@ -54,6 +58,7 @@ private class AddServerState(
private val servers: ServerRepository,
draftState: MutableState<String>,
insecureState: MutableState<Boolean>,
private val resources: Res,
) {

var draft by draftState
Expand Down Expand Up @@ -82,10 +87,10 @@ private class AddServerState(
router.closeDrawer(animated = false)
} catch (exception: SSLException) {
exception.printStackTrace()
error = "Cannot establish a secure connection"
error = resources.getString(R.string.outln_error_secure_connection)
} catch (exception: Exception) {
exception.printStackTrace()
error = "Check URL or try again"
error = resources.getString(R.string.outln_error_check_url)
} finally {
isLoading = false
}
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/java/org/sirekanyan/outline/ui/RenameContent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.sirekanyan.outline.R
import org.sirekanyan.outline.Res
import org.sirekanyan.outline.Router
import org.sirekanyan.outline.ext.rememberStateScope
import org.sirekanyan.outline.rememberResources

interface RenameDelegate {
suspend fun onRename(newName: String)
Expand All @@ -30,13 +33,15 @@ interface RenameDelegate {
@Composable
fun rememberRenameState(router: Router, delegate: RenameDelegate): RenameState {
val scope = rememberStateScope()
return remember { RenameState(scope, router, delegate) }
val resources = rememberResources()
return remember { RenameState(scope, router, delegate, resources) }
}

class RenameState(
scope: CoroutineScope,
private val router: Router,
private val renameDelegate: RenameDelegate,
private val resources: Res,
) : CoroutineScope by scope {

var error by mutableStateOf("")
Expand All @@ -50,7 +55,7 @@ class RenameState(
router.dialog = null
} catch (exception: Exception) {
exception.printStackTrace()
error = "Check name or try again"
error = resources.getString(R.string.outln_error_check_name)
} finally {
isLoading = false
}
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
<string name="outln_sorting_by_id">Id</string>
<string name="outln_sorting_by_name">Name</string>
<string name="outln_sorting_by_traffic">Traffic</string>
<string name="outln_network_error">Check your network connection</string>
</resources>
<string name="outln_error_check_network">Check your network connection</string>
<string name="outln_error_check_url">Check URL or try again</string>
<string name="outln_error_secure_connection">Cannot establish a secure connection</string>
<string name="outln_error_check_name">Check name or try again</string>
</resources>

0 comments on commit 2402068

Please sign in to comment.