Skip to content

Commit

Permalink
Add a button to clear old data
Browse files Browse the repository at this point in the history
  • Loading branch information
greenart7c3 committed Jan 12, 2025
1 parent 9e6a412 commit d081d78
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 185 deletions.
83 changes: 0 additions & 83 deletions app/src/main/java/com/greenart7c3/nostrsigner/NostrSigner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,21 @@ import com.vitorpamplona.quartz.events.Event
import com.vitorpamplona.quartz.events.EventInterface
import com.vitorpamplona.quartz.events.MetadataEvent
import com.vitorpamplona.quartz.utils.TimeUtils
import com.vitorpamplona.quartz.utils.TimeUtils.ONE_WEEK
import java.util.Timer
import java.util.TimerTask
import java.util.UUID
import java.util.concurrent.ConcurrentHashMap
import kotlin.coroutines.cancellation.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

class NostrSigner : Application() {
private var timer: Timer? = null
val client: NostrClient = NostrClient(OkHttpWebSocket.Builder())
val applicationIOScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
private var databases = ConcurrentHashMap<String, AppDatabase>()
lateinit var settings: AmberSettings
var job: Job? = null
val status = MutableStateFlow("")

val isOnMobileDataState = mutableStateOf(false)
val isOnWifiDataState = mutableStateOf(false)
Expand Down Expand Up @@ -110,79 +100,6 @@ class NostrSigner : Application() {
}
}

timer?.cancel()
timer = Timer()
timer?.schedule(
object : TimerTask() {
override fun run() {
job?.cancelChildren()
job?.cancel()
job = applicationIOScope.launch {
LocalPreferences.allSavedAccounts(this@NostrSigner).forEach {
databases[it.npub]?.let { database ->
try {
status.value = "Deleting old log entries from ${it.npub}"
val oneWeek = System.currentTimeMillis() - ONE_WEEK
val oneWeekAgo = TimeUtils.oneWeekAgo()
val countHistory = database.applicationDao().countOldHistory(oneWeekAgo)
if (countHistory > 0) {
status.value = "Deleting $countHistory old history entries"
var logs = database.applicationDao().getOldHistory(oneWeekAgo)
var count = 0
while (logs.isNotEmpty()) {
count++
status.value = "Deleting ${100 * count}/$countHistory old history entries"
logs.forEach { history ->
database.applicationDao().deleteHistory(history)
}
logs = database.applicationDao().getOldHistory(oneWeekAgo)
}
}

val countNotification = database.applicationDao().countOldNotification(oneWeekAgo)
if (countNotification > 0) {
status.value = "Deleting $countNotification old notification entries"
var logs = database.applicationDao().getOldNotification(oneWeekAgo)
var count = 0
while (logs.isNotEmpty()) {
count++
status.value = "Deleting ${100 * count}/$countNotification old notification entries"
logs.forEach { history ->
database.applicationDao().deleteNotification(history)
}
logs = database.applicationDao().getOldNotification(oneWeekAgo)
}
}

val countLog = database.applicationDao().countOldLog(oneWeek)
if (countLog > 0) {
status.value = "Deleting $countLog old notification entries"
var logs = database.applicationDao().getOldLog(oneWeek)
var count = 0
while (logs.isNotEmpty()) {
count++
status.value = "Deleting ${100 * count}/$countLog old log entries"
logs.forEach { history ->
database.applicationDao().deleteLog(history)
}
logs = database.applicationDao().getOldLog(oneWeek)
}
}
status.value = ""
} catch (e: Exception) {
if (e is CancellationException) throw e
Log.e("NostrSigner", "Error deleting old log entries", e)
status.value = ""
}
}
}
}
}
},
0,
3_600_000,
)

runBlocking {
settings = LocalPreferences.loadSettingsFromEncryptedStorage()
}
Expand Down
145 changes: 62 additions & 83 deletions app/src/main/java/com/greenart7c3/nostrsigner/ui/PermissionsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -32,15 +29,12 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavController
import com.greenart7c3.nostrsigner.NostrSigner
import com.greenart7c3.nostrsigner.R
import com.greenart7c3.nostrsigner.database.AppDatabase
import com.greenart7c3.nostrsigner.models.Account
import com.greenart7c3.nostrsigner.models.TimeUtils
import com.greenart7c3.nostrsigner.service.toShortenHex
import com.vitorpamplona.quartz.encoders.toHexKey
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@Composable
fun PermissionsScreen(
Expand All @@ -49,95 +43,80 @@ fun PermissionsScreen(
navController: NavController,
database: AppDatabase,
) {
var isLoading by remember { mutableStateOf(false) }
val applications = database.applicationDao().getAllFlow(account.signer.keyPair.pubKey.toHexKey()).collectAsStateWithLifecycle(emptyList())

LaunchedEffect(Unit) {
isLoading = true
launch(Dispatchers.IO) {
NostrSigner.getInstance().job?.join()
isLoading = false
}
}

if (isLoading) {
val status = NostrSigner.getInstance().status.collectAsStateWithLifecycle()
CenterCircularProgressIndicator(modifier, status.value)
} else {
val applications = database.applicationDao().getAllFlow(account.signer.keyPair.pubKey.toHexKey()).collectAsStateWithLifecycle(emptyList())

Column(
modifier,
) {
if (applications.value.isEmpty()) {
Text(
text = stringResource(R.string.congratulations_your_new_account_is_ready),
fontSize = 20.sp,
fontWeight = FontWeight.Bold,
)
Column(
modifier,
) {
if (applications.value.isEmpty()) {
Text(
text = stringResource(R.string.congratulations_your_new_account_is_ready),
fontSize = 20.sp,
fontWeight = FontWeight.Bold,
)

Text(
buildAnnotatedString {
append(stringResource(R.string.your_account_is_ready_to_use))
withLink(
LinkAnnotation.Url(
"https://" + stringResource(R.string.nostr_app),
styles = TextLinkStyles(
style = SpanStyle(
textDecoration = TextDecoration.Underline,
),
Text(
buildAnnotatedString {
append(stringResource(R.string.your_account_is_ready_to_use))
withLink(
LinkAnnotation.Url(
"https://" + stringResource(R.string.nostr_app),
styles = TextLinkStyles(
style = SpanStyle(
textDecoration = TextDecoration.Underline,
),
),
) {
append(" " + stringResource(R.string.nostr_app))
}
},
)
} else {
applications.value.forEach { applicationWithHistory ->
Row(
modifier = Modifier
.fillMaxSize()
.padding(vertical = 4.dp)
.clickable {
navController.navigate("Permission/${applicationWithHistory.application.key}")
},
verticalAlignment = Alignment.CenterVertically,
),
) {
Column(
verticalArrangement = Arrangement.Center,
append(" " + stringResource(R.string.nostr_app))
}
},
)
} else {
applications.value.forEach { applicationWithHistory ->
Row(
modifier = Modifier
.fillMaxSize()
.padding(vertical = 4.dp)
.clickable {
navController.navigate("Permission/${applicationWithHistory.application.key}")
},
verticalAlignment = Alignment.CenterVertically,
) {
Column(
verticalArrangement = Arrangement.Center,
) {
Text(
modifier = Modifier.padding(top = 16.dp),
text = applicationWithHistory.application.name.ifBlank { applicationWithHistory.application.key.toShortenHex() },
fontSize = 24.sp,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
) {
Text(
modifier = Modifier.padding(top = 16.dp),
text = applicationWithHistory.application.name.ifBlank { applicationWithHistory.application.key.toShortenHex() },
fontSize = 24.sp,
modifier = Modifier.padding(top = 4.dp, bottom = 16.dp),
text = applicationWithHistory.application.key.toShortenHex(),
fontSize = 16.sp,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
) {
Text(
modifier = Modifier.padding(top = 4.dp, bottom = 16.dp),
text = applicationWithHistory.application.key.toShortenHex(),
fontSize = 16.sp,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
Text(
modifier = Modifier.padding(top = 4.dp, bottom = 16.dp),
text = if (applicationWithHistory.latestTime == null) stringResource(R.string.never) else TimeUtils.formatLongToCustomDateTime(applicationWithHistory.latestTime * 1000),
fontSize = 16.sp,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
}

Spacer(Modifier.weight(1f))
HorizontalDivider(
color = MaterialTheme.colorScheme.primary,
Text(
modifier = Modifier.padding(top = 4.dp, bottom = 16.dp),
text = if (applicationWithHistory.latestTime == null) stringResource(R.string.never) else TimeUtils.formatLongToCustomDateTime(applicationWithHistory.latestTime * 1000),
fontSize = 16.sp,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
}

Spacer(Modifier.weight(1f))
HorizontalDivider(
color = MaterialTheme.colorScheme.primary,
)
}
}
}
Expand Down
Loading

0 comments on commit d081d78

Please sign in to comment.