feat: add history sync worker
This commit is contained in:
@@ -37,6 +37,7 @@ import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import cafe.adriel.lyricist.ProvideStrings
|
||||
import com.bitcointxoko.gudariwallet.data.HistoricalSyncStore
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
@@ -58,8 +59,6 @@ class MainActivity : AppCompatActivity() {
|
||||
this, 0,
|
||||
Intent(this, javaClass).apply {
|
||||
addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
|
||||
// Do NOT add FLAG_ACTIVITY_NEW_TASK — this causes
|
||||
// balDontBringExistingBackgroundTaskStackToFg = true
|
||||
},
|
||||
PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
|
||||
)
|
||||
@@ -89,6 +88,7 @@ class MainActivity : AppCompatActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
secretStore = EncryptedSecretStore(applicationContext)
|
||||
val syncStore = HistoricalSyncStore(applicationContext)
|
||||
val credentialsState = secretStore.credentials
|
||||
.stateIn(
|
||||
scope = lifecycleScope,
|
||||
@@ -97,7 +97,7 @@ class MainActivity : AppCompatActivity() {
|
||||
)
|
||||
|
||||
// Must be first — before any notification is posted
|
||||
NotificationHelper.createChannels(applicationContext)
|
||||
// NotificationHelper.createChannels(applicationContext)
|
||||
|
||||
// Read hash from the intent that launched the activity (cold start from notification)
|
||||
pendingPaymentHash.value =
|
||||
@@ -127,7 +127,6 @@ class MainActivity : AppCompatActivity() {
|
||||
LaunchedEffect(Unit) {
|
||||
WalletNotificationService.start(this@MainActivity)
|
||||
requestNotificationPermissionIfNeeded()
|
||||
// promptBatteryOptimizationIfNeeded()
|
||||
}
|
||||
WalletScreen(
|
||||
vm = vm,
|
||||
@@ -139,11 +138,9 @@ class MainActivity : AppCompatActivity() {
|
||||
} else {
|
||||
OnboardingScreen(
|
||||
secretStore = secretStore,
|
||||
syncStore = syncStore,
|
||||
onRequestNotificationPermission = { requestNotificationPermissionIfNeeded() }, // ← new
|
||||
onComplete = {
|
||||
// WalletNotificationService.start(this@MainActivity)
|
||||
// No permission/battery calls here — handled in-flow
|
||||
}
|
||||
onComplete = {}
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -218,37 +215,4 @@ class MainActivity : AppCompatActivity() {
|
||||
requestNotificationPermission.launch(Manifest.permission.POST_NOTIFICATIONS)
|
||||
}
|
||||
}
|
||||
|
||||
private fun promptBatteryOptimizationIfNeeded() {
|
||||
val pm = getSystemService(POWER_SERVICE) as PowerManager
|
||||
if (pm.isIgnoringBatteryOptimizations(packageName)) return
|
||||
|
||||
val prefs = getSharedPreferences("gudari_ui_prefs", MODE_PRIVATE)
|
||||
if (prefs.getBoolean("battery_opt_prompted", false)) return
|
||||
prefs.edit { putBoolean("battery_opt_prompted", true) }
|
||||
|
||||
AlertDialog.Builder(this)
|
||||
.setTitle("Enable background notifications")
|
||||
.setMessage(
|
||||
"To receive payment alerts when the app is closed, allow Gudari Wallet " +
|
||||
"to run without battery restrictions.\n\n" +
|
||||
"Tap 'Open Settings', then select Battery → Unrestricted."
|
||||
)
|
||||
.setPositiveButton("Open Settings") { _, _ -> openBatterySettings() }
|
||||
.setNegativeButton("Not now", null)
|
||||
.show()
|
||||
}
|
||||
|
||||
private fun openBatterySettings() {
|
||||
val appDetailsIntent =
|
||||
Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
|
||||
data = "package:$packageName".toUri()
|
||||
}
|
||||
if (tryStartActivity(appDetailsIntent)) return
|
||||
tryStartActivity(Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS))
|
||||
}
|
||||
|
||||
private fun tryStartActivity(intent: Intent): Boolean {
|
||||
return try { startActivity(intent); true } catch (e: ActivityNotFoundException) { false }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user