Skip to content

Commit

Permalink
Try to cancel coroutine jobs, show notification instead of progress i…
Browse files Browse the repository at this point in the history
…n the ui
  • Loading branch information
greenart7c3 committed Dec 23, 2024
1 parent 3a60f6f commit 11e9655
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 188 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/greenart7c3/citrine/Citrine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class Citrine : Application() {
fun contentResolverFn(): ContentResolver = contentResolver

fun cancelJob() {
job?.cancelChildren(null)
job?.cancel(null)
job?.cancelChildren()
job?.cancel()
}

suspend fun eventsToDelete(database: AppDatabase) {
Expand Down
19 changes: 4 additions & 15 deletions app/src/main/java/com/greenart7c3/citrine/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -307,21 +307,14 @@ class MainActivity : ComponentActivity() {
TextButton(
onClick = {
deleteAllDialog = false
Citrine.getInstance().cancelJob()
Citrine.getInstance().applicationScope.launch(Dispatchers.IO) {
homeViewModel.setLoading(true)
Citrine.getInstance().job?.join()
Citrine.getInstance().isImportingEvents = true
homeViewModel.setProgress("Calculating database size")
val ids = database.eventDao().getAllIds()
val size = ids.size
val batchSize = 500
ids.chunked(batchSize).forEachIndexed { index, chunk ->
homeViewModel.setProgress("Deleting ${index * batchSize}/$size")
database.eventDao().delete(chunk)
}

homeViewModel.setProgress("Deleting all events")
database.clearAllTables()
homeViewModel.setProgress("")
Citrine.getInstance().isImportingEvents = false
homeViewModel.setLoading(false)
}
},
) {
Expand Down Expand Up @@ -432,10 +425,6 @@ class MainActivity : ComponentActivity() {
) {
if (state.value.loading) {
CircularProgressIndicator()
if (state.value.progress.isNotBlank()) {
Spacer(modifier = Modifier.padding(4.dp))
Text(state.value.progress)
}
} else {
val isStarted = homeViewModel.state.value.service?.isStarted() ?: false
val clipboardManager = LocalClipboardManager.current
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package com.greenart7c3.citrine.service

import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.app.TaskStackBuilder
import android.content.Context
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationChannelGroupCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.net.toUri
import androidx.documentfile.provider.DocumentFile
import com.greenart7c3.citrine.Citrine
Expand All @@ -23,6 +24,7 @@ import com.greenart7c3.citrine.server.CustomWebSocketServer
import com.greenart7c3.citrine.server.EventSubscription
import com.greenart7c3.citrine.server.Settings
import com.greenart7c3.citrine.utils.ExportDatabaseUtils
import com.vitorpamplona.ammolite.relays.RelayPool
import java.util.Timer
import java.util.TimerTask
import kotlin.coroutines.cancellation.CancellationException
Expand Down Expand Up @@ -53,6 +55,11 @@ class WebSocketServerService : Service() {
timer?.schedule(
object : TimerTask() {
override fun run() {
if (Citrine.getInstance().job == null || Citrine.getInstance().job?.isCompleted == true) {
RelayPool.disconnect()
RelayPool.unloadRelays()
}

runBlocking {
Citrine.getInstance().eventsToDelete(database)
}
Expand Down Expand Up @@ -134,10 +141,19 @@ class WebSocketServerService : Service() {

private fun createNotification(): Notification {
Log.d(Citrine.TAG, "Creating notification")
val notificationManager = NotificationManagerCompat.from(this)
val channelId = "WebSocketServerServiceChannel"
val channel = NotificationChannel(channelId, "WebSocket Server", NotificationManager.IMPORTANCE_DEFAULT)
channel.setSound(null, null)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val groupId = "WebSocketServerServiceGroup"
val group = NotificationChannelGroupCompat.Builder(groupId)
.setName("WebSocket Server")
.build()
notificationManager.createNotificationChannelGroup(group)

val channel = NotificationChannelCompat.Builder(channelId, NotificationManager.IMPORTANCE_DEFAULT)
.setName("WebSocket Server")
.setGroup(groupId)
.setSound(null, null)
.build()
notificationManager.createNotificationChannel(channel)

val copyIntent = Intent(this, ClipboardReceiver::class.java)
Expand All @@ -160,11 +176,12 @@ class WebSocketServerService : Service() {
}

val notificationBuilder = NotificationCompat.Builder(this, "WebSocketServerServiceChannel")
.setContentTitle("Relay running at ws://${Settings.host}:${Settings.port}")
.setContentTitle(getString(R.string.relay_running_at_ws, Settings.host, Settings.port.toString()))
.setSmallIcon(R.drawable.ic_notification)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.addAction(R.drawable.ic_launcher_background, "Copy Address", copyPendingIntent)
.addAction(R.drawable.ic_launcher_background, getString(R.string.copy_address), copyPendingIntent)
.setContentIntent(resultPendingIntent)
.setGroup(groupId)

return notificationBuilder.build()
}
Expand Down
Loading

0 comments on commit 11e9655

Please sign in to comment.