diff --git a/app/src/main/java/net/mbess/popequer/AppContext.kt b/app/src/main/java/net/mbess/popequer/AppContext.kt index d8e4139..379ad45 100644 --- a/app/src/main/java/net/mbess/popequer/AppContext.kt +++ b/app/src/main/java/net/mbess/popequer/AppContext.kt @@ -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) diff --git a/app/src/main/java/net/mbess/popequer/BackgroundActions.kt b/app/src/main/java/net/mbess/popequer/BackgroundActions.kt index 69ed442..84db9f2 100644 --- a/app/src/main/java/net/mbess/popequer/BackgroundActions.kt +++ b/app/src/main/java/net/mbess/popequer/BackgroundActions.kt @@ -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() + } } } } \ No newline at end of file diff --git a/app/src/main/java/net/mbess/popequer/EventItem.kt b/app/src/main/java/net/mbess/popequer/EventItem.kt index 7effad0..27a23c9 100644 --- a/app/src/main/java/net/mbess/popequer/EventItem.kt +++ b/app/src/main/java/net/mbess/popequer/EventItem.kt @@ -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? diff --git a/app/src/main/java/net/mbess/popequer/Popequer.kt b/app/src/main/java/net/mbess/popequer/Popequer.kt index 36080a7..58b99b5 100644 --- a/app/src/main/java/net/mbess/popequer/Popequer.kt +++ b/app/src/main/java/net/mbess/popequer/Popequer.kt @@ -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 { val events_str = adapter.upcomingEvents() diff --git a/app/src/main/java/net/mbess/popequer/ui/App.kt b/app/src/main/java/net/mbess/popequer/ui/App.kt index c4ea8b7..e9330bd 100644 --- a/app/src/main/java/net/mbess/popequer/ui/App.kt +++ b/app/src/main/java/net/mbess/popequer/ui/App.kt @@ -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") -// } -// } -// } -// } diff --git a/app/src/main/java/net/mbess/popequer/ui/UpcomingEventsRoute.kt b/app/src/main/java/net/mbess/popequer/ui/UpcomingEventsRoute.kt index 394d76c..0efdfee 100644 --- a/app/src/main/java/net/mbess/popequer/ui/UpcomingEventsRoute.kt +++ b/app/src/main/java/net/mbess/popequer/ui/UpcomingEventsRoute.kt @@ -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() + } + 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) + }" + ) + } } } }