refactor: updated sync with WearOS logic

This commit is contained in:
2026-06-24 20:11:19 +03:00
parent bdde3d8fbf
commit ff214eb3d2
6 changed files with 54 additions and 18 deletions
@@ -11,6 +11,7 @@ import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
@@ -22,16 +23,25 @@ import ru.omni_devel.cards.R
class MainActivity : ComponentActivity() {
private lateinit var db: DbHelper
private val cardsState = mutableStateOf<List<CardInfo>>(emptyList())
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
db = DbHelper(this, null)
cardsState.value = db.getCards()
setContent {
App(db.getCards())
App(cardsState.value)
}
}
override fun onResume() {
super.onResume()
cardsState.value = db.getCards()
}
}
@SuppressLint("UnusedBoxWithConstraintsScope")
@@ -8,6 +8,8 @@ import com.google.android.gms.wearable.MessageEvent
import com.google.android.gms.wearable.WearableListenerService
import kotlinx.serialization.json.Json
import ru.omni_devel.cards.R
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.lifecycle.Lifecycle
class MessagesReceiver : WearableListenerService() {
private lateinit var db: DbHelper
@@ -28,13 +30,23 @@ class MessagesReceiver : WearableListenerService() {
db.clearDatabase()
db.addCards(cards)
Handler(Looper.getMainLooper()).post {
Toast.makeText(this, this.getString(R.string.sync_is_successful), Toast.LENGTH_SHORT).show()
val isAppInForeground = ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast(
Lifecycle.State.STARTED
)
val intent = Intent(this, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
if (isAppInForeground) {
Handler(Looper.getMainLooper()).post {
Toast.makeText(
this,
this.getString(R.string.sync_is_successful),
Toast.LENGTH_SHORT
).show()
val intent = Intent(this, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
}
startActivity(intent)
}
startActivity(intent)
}
}
}