Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ package com.goldenraven.padawanwallet.data.chapters

import androidx.lifecycle.LiveData
import androidx.room.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow

@Dao
interface ChapterDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun addChapter(chapter: Chapter)

@Query("SELECT * FROM chapters_db ORDER BY id ASC")
fun readAllChapters(): LiveData<List<Chapter>>
fun readAllChapters(): Flow<List<Chapter>>

@Query("UPDATE chapters_db SET completed = :completed WHERE id = :id")
suspend fun setCompleted(id: Int, completed: Boolean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
package com.goldenraven.padawanwallet.data.chapters

import android.util.Log
import androidx.lifecycle.LiveData
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.runBlocking

private const val TAG = "ChapterRepository"

class ChapterRepository(private val chapterDao: ChapterDao) {
val readAllData: LiveData<List<Chapter>> = chapterDao.readAllChapters()

val readAllData: Flow<List<Chapter>> = chapterDao.readAllChapters()

internal suspend fun getChapter(id: Int): Chapter {
Log.i(TAG, "Querying for chapter $id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

package com.goldenraven.padawanwallet.data.tx

import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import kotlinx.coroutines.flow.Flow

@Dao
interface TxDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun addTx(tx: Tx)

@Query("SELECT * FROM transaction_history ORDER BY date DESC")
fun readAllTx(): LiveData<List<Tx>>
fun readAllTx(): Flow<List<Tx>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

package com.goldenraven.padawanwallet.data.tx

import androidx.lifecycle.LiveData
import kotlinx.coroutines.flow.Flow

class TxRepository(private val txDao: TxDao) {
val readAllData: LiveData<List<Tx>> = txDao.readAllTx()
val readAllData: Flow<List<Tx>> = txDao.readAllTx()

fun addTx(tx: Tx) {
txDao.addTx(tx = tx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
Expand Down Expand Up @@ -42,7 +41,7 @@ private const val TAG = "ChaptersRootScreen"

@Composable
internal fun ChaptersRootScreen(chaptersViewModel: ChaptersViewModel, navController: NavController) {
val selectedChapterData: Chapter? by chaptersViewModel.selectedChapterData.observeAsState()
val selectedChapterData: Chapter? by chaptersViewModel.selectedChapterData.collectAsState()
val selectedChapterTagline = chaptersViewModel.getChapterPages(chaptersViewModel.selectedChapter.value)
val defaultChapter = Chapter(0, "", "", "", false)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ import android.util.Log
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.goldenraven.padawanwallet.R
import com.goldenraven.padawanwallet.data.chapters.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

private const val TAG = "ChaptersViewModel"

class ChaptersViewModel(application: Application) : AndroidViewModel(application) {
val selectedChapter: MutableState<Int> = mutableStateOf(1)

private val readAllData: LiveData<List<Chapter>>
private val readAllData: MutableStateFlow<List<Chapter>> = MutableStateFlow(emptyList())
private val chapterRepository: ChapterRepository

val selectedChapterData: MutableLiveData<Chapter> = MutableLiveData<Chapter>(Chapter(id = 0, title = "", type = "", difficulty = "", completed = false))
val selectedChapterData: MutableStateFlow<Chapter> = MutableStateFlow<Chapter>(Chapter(id = 0, title = "", type = "", difficulty = "", completed = false))

private val _chapterPageMap: Map<Int, List<List<ChapterElement>>>

Expand Down Expand Up @@ -53,7 +53,12 @@ class ChaptersViewModel(application: Application) : AndroidViewModel(application
}
}
// this variable is null on first access, and hence triggers the reinitialization of the database content
readAllData = chapterRepository.readAllData
viewModelScope.launch {
chapterRepository.readAllData
.collect { result ->
readAllData.value = result
}
}
Log.i(TAG, "readAllData variable is ${readAllData.value}")
updateSelectedChapter(1) // TODO Change to most recent chapter
_chapterPageMap = initChapterPageMap()
Expand Down Expand Up @@ -95,7 +100,7 @@ class ChaptersViewModel(application: Application) : AndroidViewModel(application
val chapter: Chapter = async {
chapterRepository.getChapter(id)
}.await()
selectedChapterData.setValue(chapter)
selectedChapterData.value = chapter
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.platform.LocalConfiguration
Expand All @@ -42,7 +38,7 @@ internal fun ReceiveScreen(
navController: NavHostController,
viewModel: WalletViewModel
) {
val address by viewModel.address.observeAsState("Generate new address")
val address by viewModel.address.collectAsState("Generate new address")
var QR by remember {
mutableStateOf<ImageBitmap?>(null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.rotate
Expand Down Expand Up @@ -63,8 +62,8 @@ internal fun WalletRootScreen(
walletViewModel: WalletViewModel
) {
val balance by walletViewModel.balance.collectAsState()
val transactionList by walletViewModel.readAllData.observeAsState(initial = emptyList())
val isOnlineStatus by walletViewModel.isOnlineVariable.observeAsState()
val transactionList by walletViewModel.readAllData.collectAsState(initial = emptyList())
val isOnlineStatus by walletViewModel.isOnlineVariable.collectAsState()
val tempOpenFaucetDialog = walletViewModel.openFaucetDialog
val context = LocalContext.current

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import androidx.compose.material.SnackbarDuration
import androidx.compose.material.SnackbarHostState
import androidx.compose.runtime.*
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.goldenraven.padawanwallet.BuildConfig
import com.goldenraven.padawanwallet.data.*
Expand Down Expand Up @@ -48,16 +46,16 @@ private const val TAG = "WalletViewModel"
class WalletViewModel(
application: Application,
) : AndroidViewModel(application) {
val readAllData: LiveData<List<Tx>>
val readAllData: MutableStateFlow<List<Tx>> = MutableStateFlow<List<Tx>>(emptyList<Tx>())
private val repository: TxRepository
var openFaucetDialog: MutableState<Boolean> = mutableStateOf(false)

private var _balance: MutableStateFlow<ULong> = MutableStateFlow(0u)
val balance: StateFlow<ULong>
get() = _balance

private var _address: MutableLiveData<String> = MutableLiveData("")
val address: LiveData<String>
private var _address: MutableStateFlow<String> = MutableStateFlow("No address yet")
val address: StateFlow<String>
get() = _address

var QRState: MutableStateFlow<QRUIState> = MutableStateFlow(QRUIState.NoQR)
Expand All @@ -66,14 +64,19 @@ class WalletViewModel(
val isRefreshing: StateFlow<Boolean>
get() = _isRefreshing.asStateFlow()

var isOnlineVariable: MutableLiveData<Boolean> = MutableLiveData(false)
var isOnlineVariable: MutableStateFlow<Boolean> = MutableStateFlow(false)

init {
Log.i(TAG, "The WalletScreen viewmodel is being initialized...")

val txDao: TxDao = TxDatabase.getDatabase(application).txDao()
repository = TxRepository(txDao)
readAllData = repository.readAllData
viewModelScope.launch {
repository.readAllData
.collect { result ->
readAllData.value = result
}
}

if (isOnlineVariable.value == true && !Wallet.blockchainIsInitialized()) {
Wallet.createBlockchain()
Expand Down Expand Up @@ -116,7 +119,7 @@ class WalletViewModel(

private fun getLastUnusedAddress(): AddressInfo {
val address = Wallet.getLastUnusedAddress()
_address.setValue(address.address)
_address.value = address.address
return address
}

Expand Down Expand Up @@ -233,7 +236,7 @@ class WalletViewModel(
false
}
Log.i(TAG, "Updating online status to $onlineStatus")
isOnlineVariable.setValue(onlineStatus)
isOnlineVariable.value = onlineStatus
}

fun broadcastTransaction(
Expand Down