-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modifications in UI and logic in library
- Loading branch information
1 parent
c7743b2
commit 9519969
Showing
10 changed files
with
143 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,58 @@ | ||
package com.vogel.nfc_reader | ||
|
||
|
||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.vogel.nfc_reader.nfc.api.CardReader | ||
import com.vogel.nfc_reader.nfc.api.CardReaderObservable | ||
import com.vogel.nfc_reader.nfc.api.CardReaderListener | ||
import com.vogel.nfc_reader.nfc.model.CardData | ||
import com.vogel.nfc_reader.nfc.utils.NFCState | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class MainViewModel @Inject constructor() : ViewModel() { | ||
|
||
|
||
class MainViewModel : ViewModel() { | ||
val cardReader = CardReader.newInstance() | ||
val cardReaderObservable = CardReaderObservable.newInstance(cardReader) | ||
} | ||
val cardReaderListener = CardReaderListener.newInstance(cardReader) | ||
|
||
var uiState by mutableStateOf(UiState()) | ||
private set | ||
|
||
init { | ||
listener() | ||
} | ||
|
||
fun listener() { | ||
viewModelScope.launch { | ||
cardReaderListener.nfcStatus.collect { | ||
uiState = uiState.copy(settingsButtonVisible = false) | ||
uiState = uiState.copy( | ||
cardStateText = when (it) { | ||
NFCState.CardLost -> "Keep it steady, card lost!" | ||
is NFCState.Error -> CardData.fetchErrorInformation(it.throwable) | ||
NFCState.Disabled -> { | ||
uiState = uiState.copy(settingsButtonVisible = true) | ||
"NFC is disabled. Go to settings to activate it" | ||
} | ||
|
||
NFCState.NotSupported -> "NFC is not supported in this device" | ||
NFCState.ReadyToScan -> "The device is ready to scan" | ||
NFCState.StartReading -> "The device is reading the card..." | ||
is NFCState.Success -> CardData.fetchCardInformation(it.card) | ||
} | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
data class UiState( | ||
val cardStateText: String = "", | ||
val settingsButtonVisible: Boolean = false | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.