feat: refresh views on notebook update

This commit is contained in:
Matthieu Bessat 2024-02-28 23:21:41 +01:00
parent 2d665ab631
commit 89decbe819
6 changed files with 78 additions and 69 deletions

View file

@ -1,10 +1,12 @@
package net.mbess.popequer
import android.content.Context
import java.util.concurrent.CopyOnWriteArrayList
class AppContext(
val androidContext: Context,
) {
val onNotebookUpdateCallbacks: MutableList<() -> Unit> = CopyOnWriteArrayList()
val gitActions = GitActions(androidContext)
val popequer = Popequer(PopequerAdapter(gitActions.cloneFolder.absolutePath))
val backgroundActions = BackgroundActions(this)

View file

@ -1,18 +1,34 @@
package net.mbess.popequer
import android.content.Intent
import android.util.Log
import android.widget.Toast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import net.mbess.popequer.ui.MainActivity
class BackgroundActions(val context: AppContext) {
fun refresh() {
runCatching {
context.gitActions.sync()
context.popequer.index()
context.onNotebookUpdateCallbacks.forEach { it() }
}.onFailure {
Log.e("BackgroundActions", "Failed to refresh the Notebook");
Log.e("BackgroundActions", "Failed to refresh the Notebook", it);
context.androidContext.mainExecutor.execute {
Toast.makeText(
context.androidContext,
"Failed to refresh the notebook",
Toast.LENGTH_LONG
)
.show()
}
}.onSuccess {
Log.i("BackgroundActions", "Notebook refresh successfully");
context.androidContext.mainExecutor.execute {
Toast.makeText(context.androidContext, "Refresh success", Toast.LENGTH_SHORT)
.show()
}
}
}
}

View file

@ -2,7 +2,7 @@ package net.mbess.popequer
import java.time.Instant
class EventItem(
data class EventItem(
val name: String,
val startTime: Instant,
val endTime: Instant?

View file

@ -1,13 +1,16 @@
package net.mbess.popequer
import android.util.Log
class PopequerException(message:String): Exception(message)
class Popequer(
val adapter: PopequerAdapter
) {
fun index(): String {
return adapter.index()
fun index() {
val res = adapter.index()
if (res.contains("ERR")) {
throw PopequerException(res)
}
}
fun upcomingEvents(): List<EventItem> {
val events_str = adapter.upcomingEvents()

View file

@ -32,7 +32,6 @@ fun AppContainer(
val scope = rememberCoroutineScope()
val androidContext = LocalContext.current
val navController = rememberNavController()
val navigationActions = remember(navController) {
AppNavigationActions(navController)
@ -124,57 +123,9 @@ fun AppContainer(
) {
AppNavGraph(
appContext = context,
navController = navController,
navController = navController
)
}
}
}
}
// val old_home = Surface(
// modifier = Modifier.padding(padding)
// ) {
// FlowColumn {
// FlowRow(
// modifier = Modifier.fillMaxWidth(),
// horizontalArrangement = Arrangement.SpaceAround
// ) {
// Button(
// onClick = {
// Toast.makeText(
// context.androidContext,
// "Will pull repo changes",
// Toast.LENGTH_SHORT
// ).show()
// scope.launch(Dispatchers.IO) {
// runCatching {
// context.gitActions.sync()
// Toast.makeText(
// context.androidContext,
// "Repo changes are sync",
// Toast.LENGTH_LONG
// ).show()
// }.onFailure {
// Log.e("MainActivity", "Failed to trigger git actions", it)
// }
// }
// }
// ) {
// Text("Pull")
// }
// Button(
// onClick = {
//
// }
// ) {
// Text("New quick note")
// }
// }
// LazyColumn {
// // Add 5 items
// items(30) { index ->
// Text(text = "Item: $index")
// }
// }
// }
// }

View file

@ -1,20 +1,30 @@
package net.mbess.popequer.ui
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import net.mbess.popequer.AppContext
import net.mbess.popequer.EventItem
import java.time.LocalDateTime
import java.time.ZoneOffset.UTC
import java.time.format.DateTimeFormatter
@ -23,21 +33,48 @@ import java.time.format.DateTimeFormatter
fun UpcomingEventsRoute(
appContext: AppContext
) {
val eventsList = remember {
mutableStateListOf<EventItem>()
}
val refresh: () -> Unit = remember {
{
eventsList.clear()
eventsList.addAll(appContext.popequer.upcomingEvents())
}
}
val formatter = remember {
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
}
Surface(modifier = Modifier.padding(10.dp)) {
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LazyColumn {
itemsIndexed(appContext.popequer.upcomingEvents()) { index, item ->
Card(
elevation = CardDefaults.cardElevation(
defaultElevation = 6.dp
),
shape = MaterialTheme.shapes.medium,
modifier = Modifier.fillMaxWidth().padding(bottom = 10.dp)
) {
Column(modifier = Modifier.padding(15.dp)) {
Text(item.name, style = MaterialTheme.typography.headlineSmall)
Text("Start: ${LocalDateTime.ofInstant(item.startTime, UTC).format(formatter)}")
LaunchedEffect(Unit) {
refresh()
appContext.onNotebookUpdateCallbacks.add(refresh)
}
DisposableEffect(Unit) {
onDispose {
appContext.onNotebookUpdateCallbacks.remove(refresh)
}
}
Box() {
LazyColumn {
itemsIndexed(eventsList) { index, item ->
Card(
elevation = CardDefaults.cardElevation(
defaultElevation = 6.dp
),
shape = MaterialTheme.shapes.medium,
modifier = Modifier
.fillMaxWidth()
.padding(bottom = 10.dp)
) {
Column(modifier = Modifier.padding(15.dp)) {
Text(item.name, style = MaterialTheme.typography.headlineSmall)
Text(
"Start: ${
LocalDateTime.ofInstant(item.startTime, UTC).format(formatter)
}"
)
}
}
}
}