forked from omni/OmniCards
refactor: updated sync with WearOS logic
This commit is contained in:
@@ -23,7 +23,7 @@ android {
|
||||
minSdk = 30
|
||||
targetSdk = 36
|
||||
versionCode = generateVersionCode()
|
||||
versionName = "4.2.1"
|
||||
versionName = "4.3.0"
|
||||
|
||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
|
||||
@@ -76,6 +76,8 @@ class MainActivity : ComponentActivity() {
|
||||
|
||||
cards.clear()
|
||||
cards.addAll(db.getCards())
|
||||
|
||||
syncCardsWithWatch(this, db.getCards())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,12 +116,13 @@ fun MainPage(
|
||||
NavigationPageData(stringResource(R.string.do_add_card), Icons.Outlined.Add, EditCardActivity::class.java),
|
||||
NavigationPageData(stringResource(R.string.do_sync), Icons.Outlined.Sync) {
|
||||
Toast.makeText(context, context.getString(R.string.sync_in_progress), Toast.LENGTH_SHORT).show()
|
||||
sendToWatch(context, "/updateCards", Json.encodeToString(getDbFun().getCards())) { isSuccess ->
|
||||
if (isSuccess) {
|
||||
Toast.makeText(context, context.getString(R.string.sync_is_successful), Toast.LENGTH_SHORT).show()
|
||||
} else {
|
||||
Toast.makeText(context, context.getString(R.string.sync_is_fail), Toast.LENGTH_LONG).show()
|
||||
}
|
||||
|
||||
val isSuccess = syncCardsWithWatch(context, getDbFun().getCards())
|
||||
|
||||
if (isSuccess) {
|
||||
Toast.makeText(context, context.getString(R.string.sync_is_successful), Toast.LENGTH_SHORT).show()
|
||||
} else {
|
||||
Toast.makeText(context, context.getString(R.string.sync_is_fail), Toast.LENGTH_LONG).show()
|
||||
}
|
||||
},
|
||||
NavigationPageData(stringResource(R.string.open_backup_menu), Icons.Outlined.Backup, BackupActivity::class.java),
|
||||
|
||||
@@ -14,7 +14,9 @@ import com.journeyapps.barcodescanner.BarcodeEncoder
|
||||
import org.json.JSONObject
|
||||
import java.io.ByteArrayOutputStream
|
||||
import android.util.Base64
|
||||
import android.widget.Toast
|
||||
import androidx.compose.ui.graphics.asImageBitmap
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
fun generateCode(value: String, format: BarcodeFormat): Bitmap {
|
||||
val writer = MultiFormatWriter()
|
||||
@@ -33,12 +35,14 @@ fun generateCode(value: String, format: BarcodeFormat): Bitmap {
|
||||
return BarcodeEncoder().createBitmap(matrix)
|
||||
}
|
||||
|
||||
fun sendToWatch(context: Context, path: String, message: String, onResult: (Boolean) -> Unit) {
|
||||
fun sendToWatch(context: Context, path: String, message: String): Boolean {
|
||||
val nodeClient = Wearable.getNodeClient(context)
|
||||
|
||||
var isSuccess = false
|
||||
|
||||
nodeClient.connectedNodes.addOnSuccessListener { nodes ->
|
||||
if (nodes.isEmpty()) {
|
||||
onResult(false)
|
||||
isSuccess = false
|
||||
|
||||
return@addOnSuccessListener
|
||||
}
|
||||
@@ -59,13 +63,19 @@ fun sendToWatch(context: Context, path: String, message: String, onResult: (Bool
|
||||
}
|
||||
|
||||
if (completedCount == nodes.size) {
|
||||
onResult(isAnySuccess)
|
||||
isSuccess = isAnySuccess
|
||||
}
|
||||
}.addOnFailureListener(context.mainExecutor) {
|
||||
onResult(false)
|
||||
isSuccess = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return isSuccess
|
||||
}
|
||||
|
||||
fun syncCardsWithWatch(context: Context, cards: List<CardInfo>): Boolean {
|
||||
return sendToWatch(context, "/updateCards", Json.encodeToString(cards))
|
||||
}
|
||||
|
||||
fun checkCardData(name: String, value: String): Int? {
|
||||
|
||||
Reference in New Issue
Block a user