feat(android): add reminders and background sync
- Add local overdue reminders with notification channel and tap-to-date\n- Extract shared sync engine for foreground and WorkManager sync\n- Schedule 15-minute sync/reminder work and restore tasks after boot\n- Request notification permission and document Android background limits
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
package com.misaki.schedule
|
||||
|
||||
import android.Manifest
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import android.widget.TextView
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.lifecycle.Lifecycle
|
||||
@@ -17,6 +22,8 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||
import com.misaki.schedule.data.Prefs
|
||||
import com.misaki.schedule.data.ScheduleItem
|
||||
import com.misaki.schedule.data.ScheduleRepository
|
||||
import com.misaki.schedule.background.BackgroundScheduler
|
||||
import com.misaki.schedule.background.NotificationHelper
|
||||
import com.misaki.schedule.data.TimeUtils
|
||||
import com.misaki.schedule.sync.SyncManager
|
||||
import com.misaki.schedule.ui.ScheduleAdapter
|
||||
@@ -38,6 +45,9 @@ class MainActivity : AppCompatActivity() {
|
||||
private lateinit var syncStatus: TextView
|
||||
private var selectedDate: LocalDate = LocalDate.now()
|
||||
private val dateFormatter = DateTimeFormatter.ofPattern("yyyy年M月d日 EEEE", Locale.CHINA)
|
||||
private val notificationPermission = registerForActivityResult(
|
||||
ActivityResultContracts.RequestPermission(),
|
||||
) { }
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
@@ -46,6 +56,17 @@ class MainActivity : AppCompatActivity() {
|
||||
repository = ScheduleRepository(this)
|
||||
prefs = Prefs(this)
|
||||
syncManager = SyncManager(this, prefs, repository)
|
||||
NotificationHelper.ensureChannel(this)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
|
||||
ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) !=
|
||||
PackageManager.PERMISSION_GRANTED
|
||||
) {
|
||||
notificationPermission.launch(Manifest.permission.POST_NOTIFICATIONS)
|
||||
}
|
||||
BackgroundScheduler.update(this, prefs)
|
||||
intent.getStringExtra("date")?.let { value ->
|
||||
TimeUtils.dateOf(value)?.let { selectedDate = it }
|
||||
}
|
||||
|
||||
list = findViewById(R.id.scheduleList)
|
||||
emptyText = findViewById(R.id.emptyText)
|
||||
@@ -58,6 +79,7 @@ class MainActivity : AppCompatActivity() {
|
||||
onDoneChanged = { item, done ->
|
||||
repository.setDone(item.block.id, done, prefs.deviceId)
|
||||
syncManager.triggerSync()
|
||||
BackgroundScheduler.syncNow(this)
|
||||
},
|
||||
)
|
||||
list.layoutManager = LinearLayoutManager(this)
|
||||
@@ -115,6 +137,17 @@ class MainActivity : AppCompatActivity() {
|
||||
refresh()
|
||||
}
|
||||
|
||||
override fun onNewIntent(intent: Intent) {
|
||||
super.onNewIntent(intent)
|
||||
setIntent(intent)
|
||||
intent.getStringExtra("date")?.let { value ->
|
||||
TimeUtils.dateOf(value)?.let {
|
||||
selectedDate = it
|
||||
refresh()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
syncManager.start()
|
||||
@@ -154,6 +187,7 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
selectedDate = newStart.toLocalDate()
|
||||
syncManager.triggerSync()
|
||||
BackgroundScheduler.syncNow(this)
|
||||
refresh()
|
||||
}
|
||||
dialog.show()
|
||||
@@ -167,6 +201,7 @@ class MainActivity : AppCompatActivity() {
|
||||
.setPositiveButton(R.string.delete) { _, _ ->
|
||||
repository.delete(item.block.id)
|
||||
syncManager.triggerSync()
|
||||
BackgroundScheduler.syncNow(this)
|
||||
refresh()
|
||||
}
|
||||
.show()
|
||||
|
||||
Reference in New Issue
Block a user