feat: add Android sync client
- Add Kotlin/Material 3 Android project with SQLite outbox and LWW\n- Implement registration, push/pull, WebSocket notifications, and settings\n- Add schedule list, date navigation, create/edit/delete, and completion\n- Add JVM LWW/time tests and build a debug APK\n- Document Android v1 scope and validation
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
package com.misaki.schedule
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import android.widget.TextView
|
||||
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.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)
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
|
||||
repository = ScheduleRepository(this)
|
||||
prefs = Prefs(this)
|
||||
syncManager = SyncManager(this, prefs, repository)
|
||||
|
||||
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()
|
||||
},
|
||||
)
|
||||
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 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()
|
||||
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()
|
||||
refresh()
|
||||
}
|
||||
.show()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user