Skip to content

Commit

Permalink
Allow scanning tag multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
andras-adam committed Oct 13, 2022
1 parent ecc7498 commit 53b5c11
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 40 deletions.
8 changes: 4 additions & 4 deletions app/src/main/java/com/virtualtag/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class MainActivity : ComponentActivity() {
}
val scanCard: () -> Unit = { navController.navigate("scan") }
val addCard: () -> Unit = { navController.navigate("add") }
val viewCard: (id: String) -> Unit = { id -> navController.navigate("card/$id") }
val editCard: (id: String) -> Unit = { id -> navController.navigate("edit/$id") }
val viewCard: (id: Int) -> Unit = { id -> navController.navigate("card/$id") }
val editCard: (id: Int) -> Unit = { id -> navController.navigate("edit/$id") }
// Render content
VirtualTagTheme {
NavHost(navController = navController, startDestination = "home") {
Expand Down Expand Up @@ -67,7 +67,7 @@ class MainActivity : ComponentActivity() {
) {
CardScreen(
model = cardViewModel,
id = it.arguments?.getString("id") ?: "0",
id = it.arguments?.getString("id")?.toInt() ?: 0,
editCard = editCard,
goBack = goBack,
goHome = goHome
Expand All @@ -80,7 +80,7 @@ class MainActivity : ComponentActivity() {
) {
EditScreen(
model = cardViewModel,
id = it.arguments?.getString("id") ?: "0",
id = it.arguments?.getString("id")?.toInt() ?: 0,
goBack = goBack,
goHome = goHome
)
Expand Down
14 changes: 7 additions & 7 deletions app/src/main/java/com/virtualtag/app/db/Card.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import androidx.room.PrimaryKey

@Entity
data class Card(
/*
* The tag ID from android.nfc.Tag should be the primary key, so in case the user tries to add
* the same Tag twice, then OnConflictStrategy can easily handle the duplicates within the DAO
* (note - the tag ID coming from android.nfc.Tag cannot be handled as Integer)
*/
@PrimaryKey
val id: String,
@PrimaryKey(autoGenerate = true)
val id: Int,

// Display properties
val name: String,
val color: String,

// Tag properties
val serialNumber: String,
val techList: String,

// MifareClassic properties
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/virtualtag/app/db/CardDAO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ interface CardDao {
fun getAllCards(): LiveData<List<Card>>

@Query("SELECT * FROM card WHERE card.id = :id")
fun getCardById(id: String): LiveData<Card>
fun getCardById(id: Int): LiveData<Card>

@Insert(onConflict = OnConflictStrategy.REPLACE)
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun addCard(card: Card)

@Query("UPDATE card SET name = :name, color = :color WHERE card.id = :id")
fun updateCard(id: String, name: String, color: String)
fun updateCard(id: Int, name: String, color: String)

@Delete
fun deleteCard(card: Card)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/virtualtag/app/db/CardDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import androidx.room.RoomDatabase

@Database(
entities = [Card::class],
version = 3
version = 4
)
abstract class CardDB : RoomDatabase() {
abstract fun cardDao(): CardDao
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.virtualtag.app.R

@Composable
fun MifareClassicView(card: Card) {
val id = formatHex(card.id)
val serialNumber = formatHex(card.serialNumber)
val techList = card.techList
.replace("android.nfc.tech.", "")
.replace(",", ", ")
Expand All @@ -37,7 +37,7 @@ fun MifareClassicView(card: Card) {
item {
DataRow(
title = stringResource(R.string.serial_number),
content = id,
content = serialNumber,
icon = Icons.Filled.Key)
}
item {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.virtualtag.app.R

@Composable
fun MifareUltralightView(card: Card) {
val id = formatHex(card.id)
val serialNumber = formatHex(card.serialNumber)
val techList = card.techList
.replace("android.nfc.tech.", "")
.replace(",", ", ")
Expand All @@ -39,7 +39,7 @@ fun MifareUltralightView(card: Card) {
item {
DataRow(
title = stringResource(R.string.serial_number),
content = id,
content = serialNumber,
icon = Icons.Filled.Key)
}
item {
Expand Down
27 changes: 13 additions & 14 deletions app/src/main/java/com/virtualtag/app/ui/screens/AddScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,16 @@ fun AddScreen(
var color by remember { mutableStateOf("#fff8f8f8") }

fun addCardToDb() {
val card = Card(
id = scannedTag.value?.id?.toHex() ?: "0",
if (name.text == "") {
Toast.makeText(context, context.getString(R.string.empty_name_error), Toast.LENGTH_SHORT).show()
return
}
model.addCard(Card(
id = 0,
name = name.text,
color = color,
// Tag properties
serialNumber = scannedTag.value?.id?.toHex() ?: "0",
techList = scannedTag.value?.techList?.joinToString(",") ?: "",
// MifareClassic properties
mifareClassicAtqa = mifareClassicInfo.value?.atqa,
Expand All @@ -64,8 +70,7 @@ fun AddScreen(
mifareUltralightAtqa = mifareUltralightInfo.value?.atqa,
mifareUltralightSak = mifareUltralightInfo.value?.sak,
mifareUltralightData = mifareUltralightInfo.value?.data,
)
model.addCard(card)
))
Toast.makeText(context, context.getString(R.string.card_added_success), Toast.LENGTH_SHORT).show()
goHome()
}
Expand Down Expand Up @@ -158,16 +163,10 @@ fun AddScreen(
.weight(1f)
.padding(start = 4.dp)
) {
PrimaryButton(text = "Ok", onClick = {
if (name.text == "") {
return@PrimaryButton Toast.makeText(
context,
context.getString(R.string.empty_name_error),
Toast.LENGTH_SHORT
).show()
}
addCardToDb()
}, modifier = Modifier.padding(top = 8.dp, bottom = 8.dp))
PrimaryButton(
text = stringResource(R.string.ok),
onClick = { addCardToDb() },
modifier = Modifier.padding(top = 8.dp, bottom = 8.dp))
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/virtualtag/app/ui/screens/CardScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import com.virtualtag.app.viewmodels.CardViewModel
@Composable
fun CardScreen(
model: CardViewModel,
id: String,
editCard: (id: String) -> Unit,
id: Int,
editCard: (id: Int) -> Unit,
goBack: () -> Unit,
goHome: () -> Unit
) {
Expand All @@ -51,7 +51,7 @@ fun CardScreen(
}
},
actions = {
IconButton(onClick = { editCard(card.value?.id ?: "") }) {
IconButton(onClick = { editCard(card.value?.id ?: 0) }) {
Icon(Icons.Filled.Edit, null)
}
IconButton(onClick = { deleteDialogOpen = true }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import com.virtualtag.app.utils.colorToString
import com.virtualtag.app.utils.stringToColor

@Composable
fun EditScreen(model: CardViewModel, id: String, goBack: () -> Unit, goHome: () -> Unit) {
fun EditScreen(model: CardViewModel, id: Int, goBack: () -> Unit, goHome: () -> Unit) {
val context = LocalContext.current
val card = model.getCardById(id).observeAsState(null)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import com.virtualtag.app.R
import com.virtualtag.app.ui.components.*

@Composable
fun HomeScreen(model: CardViewModel, viewCard: (id: String) -> Unit, scanCard: () -> Unit) {
fun HomeScreen(model: CardViewModel, viewCard: (id: Int) -> Unit, scanCard: () -> Unit) {
val context = LocalContext.current
val cardList = model.getAllCards().observeAsState(listOf())
var errorDialogOpen by remember { mutableStateOf(false) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ class CardViewModel(application: Application) : ViewModel() {

fun getAllCards(): LiveData<List<Card>> = cardDB.cardDao().getAllCards()

fun getCardById(id: String): LiveData<Card> = cardDB.cardDao().getCardById(id)
fun getCardById(id: Int): LiveData<Card> = cardDB.cardDao().getCardById(id)

fun addCard(card: Card) {
CoroutineScope(Dispatchers.IO).launch {
cardDB.cardDao().addCard(card)
}
}

fun updateCard(id: String, name: String, color: String) {
fun updateCard(id: Int, name: String, color: String) {
CoroutineScope(Dispatchers.IO).launch {
cardDB.cardDao().updateCard(id, name, color)
}
Expand Down

0 comments on commit 53b5c11

Please sign in to comment.