121e66d527
- 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
210 lines
7.6 KiB
Kotlin
210 lines
7.6 KiB
Kotlin
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
|
|
import androidx.lifecycle.lifecycleScope
|
|
import androidx.lifecycle.repeatOnLifecycle
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
import com.google.android.material.appbar.MaterialToolbar
|
|
import com.google.android.material.button.MaterialButton
|
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
|
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
|
|
import com.misaki.schedule.ui.ScheduleDialog
|
|
import com.misaki.schedule.ui.SettingsDialog
|
|
import kotlinx.coroutines.launch
|
|
import java.time.LocalDate
|
|
import java.time.format.DateTimeFormatter
|
|
import java.util.Locale
|
|
|
|
class MainActivity : AppCompatActivity() {
|
|
private lateinit var repository: ScheduleRepository
|
|
private lateinit var prefs: Prefs
|
|
private lateinit var syncManager: SyncManager
|
|
private lateinit var adapter: ScheduleAdapter
|
|
private lateinit var list: RecyclerView
|
|
private lateinit var emptyText: TextView
|
|
private lateinit var dateText: TextView
|
|
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)
|
|
setContentView(R.layout.activity_main)
|
|
|
|
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)
|
|
dateText = findViewById(R.id.dateText)
|
|
syncStatus = findViewById(R.id.syncStatus)
|
|
|
|
adapter = ScheduleAdapter(
|
|
onEdit = { showScheduleDialog(it) },
|
|
onLongPress = { confirmDelete(it) },
|
|
onDoneChanged = { item, done ->
|
|
repository.setDone(item.block.id, done, prefs.deviceId)
|
|
syncManager.triggerSync()
|
|
BackgroundScheduler.syncNow(this)
|
|
},
|
|
)
|
|
list.layoutManager = LinearLayoutManager(this)
|
|
list.adapter = adapter
|
|
|
|
findViewById<FloatingActionButton>(R.id.addButton).setOnClickListener {
|
|
val start = TimeUtils.defaultStart(selectedDate)
|
|
showScheduleDialog(null, start)
|
|
}
|
|
findViewById<MaterialButton>(R.id.todayButton).setOnClickListener {
|
|
selectedDate = LocalDate.now()
|
|
refresh()
|
|
}
|
|
findViewById<View>(R.id.previousDay).setOnClickListener {
|
|
selectedDate = selectedDate.minusDays(1)
|
|
refresh()
|
|
}
|
|
findViewById<View>(R.id.nextDay).setOnClickListener {
|
|
selectedDate = selectedDate.plusDays(1)
|
|
refresh()
|
|
}
|
|
|
|
val toolbar = findViewById<MaterialToolbar>(R.id.toolbar)
|
|
toolbar.setOnMenuItemClickListener { item ->
|
|
when (item.itemId) {
|
|
R.id.action_sync -> {
|
|
syncManager.reload()
|
|
true
|
|
}
|
|
R.id.action_settings -> {
|
|
SettingsDialog.show(this, prefs, syncManager)
|
|
true
|
|
}
|
|
else -> false
|
|
}
|
|
}
|
|
|
|
lifecycleScope.launch {
|
|
repeatOnLifecycle(Lifecycle.State.STARTED) {
|
|
launch { repository.revision.collect { refresh() } }
|
|
launch {
|
|
syncManager.status.collect { status ->
|
|
syncStatus.text = status.text
|
|
syncStatus.setTextColor(
|
|
ContextCompat.getColor(
|
|
this@MainActivity,
|
|
if (status.error) android.R.color.holo_red_dark else R.color.seed,
|
|
)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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()
|
|
}
|
|
|
|
override fun onStop() {
|
|
syncManager.stop()
|
|
super.onStop()
|
|
}
|
|
|
|
override fun onDestroy() {
|
|
syncManager.shutdown()
|
|
super.onDestroy()
|
|
}
|
|
|
|
private fun refresh() {
|
|
dateText.text = selectedDate.format(dateFormatter)
|
|
val items = repository.itemsForDate(selectedDate)
|
|
adapter.submit(items)
|
|
emptyText.visibility = if (items.isEmpty()) View.VISIBLE else View.GONE
|
|
}
|
|
|
|
private fun showScheduleDialog(item: ScheduleItem?, fallbackStart: java.time.LocalDateTime? = null) {
|
|
val start = item?.let { TimeUtils.localTime(it.block.startAt) } ?: fallbackStart ?: TimeUtils.defaultStart(selectedDate)
|
|
val end = item?.let { TimeUtils.localTime(it.block.endAt) } ?: start.plusHours(1)
|
|
val dialog = ScheduleDialog(
|
|
this,
|
|
if (item == null) getString(R.string.add_schedule) else item.task.title,
|
|
item?.task?.title.orEmpty(),
|
|
start,
|
|
end,
|
|
) { title, newStart, newEnd ->
|
|
if (item == null) {
|
|
repository.create(title, newStart, newEnd, prefs.deviceId)
|
|
} else {
|
|
repository.update(item.block.id, title, newStart, newEnd, prefs.deviceId)
|
|
}
|
|
selectedDate = newStart.toLocalDate()
|
|
syncManager.triggerSync()
|
|
BackgroundScheduler.syncNow(this)
|
|
refresh()
|
|
}
|
|
dialog.show()
|
|
}
|
|
|
|
private fun confirmDelete(item: ScheduleItem) {
|
|
MaterialAlertDialogBuilder(this)
|
|
.setTitle(R.string.delete_title)
|
|
.setMessage(getString(R.string.delete_message, item.task.title))
|
|
.setNegativeButton(R.string.cancel, null)
|
|
.setPositiveButton(R.string.delete) { _, _ ->
|
|
repository.delete(item.block.id)
|
|
syncManager.triggerSync()
|
|
BackgroundScheduler.syncNow(this)
|
|
refresh()
|
|
}
|
|
.show()
|
|
}
|
|
}
|