Skip to content

Commit

Permalink
Added update menu button
Browse files Browse the repository at this point in the history
  • Loading branch information
sirekanian committed Feb 11, 2024
1 parent d0630ed commit 1bbf11a
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
15 changes: 15 additions & 0 deletions app/src/main/java/org/sirekanyan/outline/MainContent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Icon
Expand Down Expand Up @@ -86,6 +87,15 @@ fun MainContent(state: MainState) {
}
}
}
Box(Modifier.fillMaxSize().padding(insets).alpha(0.95f)) {
AnimatedVisibility(
visible = page.keys is KeysLoadingState,
enter = fadeIn() + expandVertically(),
exit = fadeOut() + shrinkVertically(),
) {
LinearProgressIndicator(Modifier.fillMaxWidth())
}
}
if (search.isOpened) {
MainTopAppBar(
title = { SearchField(search.query) { search.query = it } },
Expand Down Expand Up @@ -116,6 +126,11 @@ fun MainContent(state: MainState) {
},
onMenuClick = state::openDrawer,
visibleItems = menuItems,
overflowItems = listOf(
MenuItem(R.string.outln_menu_update, Icons.Default.Refresh) {
state.onUpdateButtonClicked()
}
),
)
}
}
Expand Down
36 changes: 35 additions & 1 deletion app/src/main/java/org/sirekanyan/outline/MainState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize
import org.sirekanyan.outline.api.model.Key
import org.sirekanyan.outline.api.model.Server
import org.sirekanyan.outline.db.KeyValueDao
import org.sirekanyan.outline.ext.asyncAll
import org.sirekanyan.outline.ext.rememberStateScope
import org.sirekanyan.outline.feature.keys.KeysErrorState
import org.sirekanyan.outline.feature.keys.KeysIdleState
Expand All @@ -28,6 +30,7 @@ import org.sirekanyan.outline.repository.KeyRepository
import org.sirekanyan.outline.repository.ServerRepository
import org.sirekanyan.outline.ui.SearchState
import org.sirekanyan.outline.ui.rememberSearchState
import kotlin.time.Duration.Companion.seconds

@Composable
fun rememberMainState(router: Router): MainState {
Expand Down Expand Up @@ -90,12 +93,40 @@ class MainState(
}
}

private suspend fun refreshAllKeys() {
val page = page as? HelloPage ?: return
if (page.keys is KeysLoadingState) return
withContext(Dispatchers.IO) {
page.keys = KeysLoadingState
page.keys = try {
servers.getServers()
.asyncAll { server ->
runCatching {
withTimeout(5.seconds) {
keys.updateKeys(server)
}
}
}
KeysIdleState
} catch (exception: Exception) {
exception.printStackTrace()
KeysErrorState
}
}
}

fun onRetryButtonClicked() {
launch {
refreshCurrentKeys(showLoading = true)
}
}

fun onUpdateButtonClicked() {
launch {
refreshAllKeys()
}
}

fun onAddKeyClicked() {
selectedPage?.let { page ->
launch {
Expand Down Expand Up @@ -135,7 +166,10 @@ class MainState(
@Parcelize
sealed class Page : Parcelable

data object HelloPage : Page()
data object HelloPage : Page() {
@IgnoredOnParcel
var keys by mutableStateOf<KeysState>(KeysIdleState)
}

data class SelectedPage(val server: Server) : Page() {
@IgnoredOnParcel
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/org/sirekanyan/outline/ext/Iterable.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.sirekanyan.outline.ext

import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope

suspend fun <T, R> Iterable<T>.asyncAll(block: suspend (T) -> R): List<R> =
coroutineScope { map { async { block(it) } }.awaitAll() }
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ import kotlinx.coroutines.withContext
import org.sirekanyan.outline.api.OutlineApi
import org.sirekanyan.outline.api.model.Server
import org.sirekanyan.outline.api.model.fromEntities
import org.sirekanyan.outline.api.model.fromEntity
import org.sirekanyan.outline.api.model.toEntities
import org.sirekanyan.outline.api.model.toEntity
import org.sirekanyan.outline.db.ServerDao
import org.sirekanyan.outline.ext.logDebug

class ServerRepository(private val api: OutlineApi, private val serverDao: ServerDao) {

fun getServers(): List<Server> =
serverDao.selectAll().map { it.fromEntity() }

fun observeServers(): Flow<List<Server>> =
serverDao.observeAll().mapToList(IO).map { it.fromEntities() }

Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<string name="outln_menu_edit">Изменить</string>
<string name="outln_menu_delete">Удалить</string>
<string name="outln_menu_search">Поиск</string>
<string name="outln_menu_update">Обновить</string>

<!-- Sorting -->
<string name="outln_sorting_by">Сортировать по…</string>
Expand All @@ -30,6 +31,13 @@
<string name="outln_hint_search">Поиск…</string>
<string name="outln_text_add_server">Добавить сервер</string>

<!-- Update -->
<plurals name="outln_update_error">
<item quantity="one">Не удалось обновить %d сервер</item>
<item quantity="few">Не удалось обновить %d сервера</item>
<item quantity="many">Не удалось обновить %d серверов</item>
</plurals>

<!-- Selected Page -->
<string name="outln_btn_add_key">Добавить ключ</string>
<string name="outln_error_check_network">Проверьте подключение к сети</string>
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<string name="outln_menu_edit">Edit</string>
<string name="outln_menu_delete">Delete</string>
<string name="outln_menu_search">Search</string>
<string name="outln_menu_update">Update</string>

<!-- Sorting -->
<string name="outln_sorting_by">Sort by…</string>
Expand All @@ -30,6 +31,12 @@
<string name="outln_hint_search">Search…</string>
<string name="outln_text_add_server">Add server</string>

<!-- Update -->
<plurals name="outln_update_error">
<item quantity="one">Cannot update %d server</item>
<item quantity="other">Cannot update %d servers</item>
</plurals>

<!-- Selected Page -->
<string name="outln_btn_add_key">Add key</string>
<string name="outln_error_check_network">Check your network connection</string>
Expand Down

0 comments on commit 1bbf11a

Please sign in to comment.