Skip to content

Commit 97c9cda

Browse files
Feat: Migrate LiveData to Kotlin Flows
1 parent 900a99a commit 97c9cda

File tree

11 files changed

+44
-40
lines changed

11 files changed

+44
-40
lines changed

app/src/main/java/com/goldenraven/padawanwallet/data/chapters/ChapterDao.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ package com.goldenraven.padawanwallet.data.chapters
77

88
import androidx.lifecycle.LiveData
99
import androidx.room.*
10+
import kotlinx.coroutines.flow.Flow
11+
import kotlinx.coroutines.flow.StateFlow
1012

1113
@Dao
1214
interface ChapterDao {
1315
@Insert(onConflict = OnConflictStrategy.REPLACE)
1416
suspend fun addChapter(chapter: Chapter)
1517

1618
@Query("SELECT * FROM chapters_db ORDER BY id ASC")
17-
fun readAllChapters(): LiveData<List<Chapter>>
19+
fun readAllChapters(): Flow<List<Chapter>>
1820

1921
@Query("UPDATE chapters_db SET completed = :completed WHERE id = :id")
2022
suspend fun setCompleted(id: Int, completed: Boolean)

app/src/main/java/com/goldenraven/padawanwallet/data/chapters/ChapterRepository.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
package com.goldenraven.padawanwallet.data.chapters
77

88
import android.util.Log
9-
import androidx.lifecycle.LiveData
9+
import kotlinx.coroutines.flow.Flow
10+
import kotlinx.coroutines.flow.StateFlow
1011
import kotlinx.coroutines.runBlocking
1112

1213
private const val TAG = "ChapterRepository"
1314

1415
class ChapterRepository(private val chapterDao: ChapterDao) {
15-
val readAllData: LiveData<List<Chapter>> = chapterDao.readAllChapters()
16+
17+
val readAllData: Flow<List<Chapter>> = chapterDao.readAllChapters()
1618

1719
internal suspend fun getChapter(id: Int): Chapter {
1820
Log.i(TAG, "Querying for chapter $id")

app/src/main/java/com/goldenraven/padawanwallet/data/tx/TxDao.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55

66
package com.goldenraven.padawanwallet.data.tx
77

8-
import androidx.lifecycle.LiveData
98
import androidx.room.Dao
109
import androidx.room.Insert
1110
import androidx.room.OnConflictStrategy
1211
import androidx.room.Query
12+
import kotlinx.coroutines.flow.Flow
1313

1414
@Dao
1515
interface TxDao {
1616
@Insert(onConflict = OnConflictStrategy.REPLACE)
1717
fun addTx(tx: Tx)
1818

1919
@Query("SELECT * FROM transaction_history ORDER BY date DESC")
20-
fun readAllTx(): LiveData<List<Tx>>
20+
fun readAllTx(): Flow<List<Tx>>
2121
}

app/src/main/java/com/goldenraven/padawanwallet/data/tx/TxRepository.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
package com.goldenraven.padawanwallet.data.tx
77

8-
import androidx.lifecycle.LiveData
8+
import kotlinx.coroutines.flow.Flow
99

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

1313
fun addTx(tx: Tx) {
1414
txDao.addTx(tx = tx)

app/src/main/java/com/goldenraven/padawanwallet/ui/chapters/ChaptersRootScreen.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import androidx.compose.foundation.layout.*
88
import androidx.compose.foundation.shape.RoundedCornerShape
99
import androidx.compose.material3.*
1010
import androidx.compose.runtime.*
11-
import androidx.compose.runtime.livedata.observeAsState
1211
import androidx.compose.ui.Alignment
1312
import androidx.compose.ui.Modifier
1413
import androidx.compose.ui.draw.drawBehind
@@ -38,7 +37,7 @@ private const val TAG = "ChaptersRootScreen"
3837

3938
@Composable
4039
internal fun ChaptersRootScreen(chaptersViewModel: ChaptersViewModel, navController: NavController) {
41-
val selectedChapterData: Chapter? by chaptersViewModel.selectedChapterData.observeAsState()
40+
val selectedChapterData: Chapter? by chaptersViewModel.selectedChapterData.collectAsState()
4241
val selectedChapterTagline = chaptersViewModel.getChapterPages(chaptersViewModel.selectedChapter.value)
4342
val defaultChapter = Chapter(0, "", "", "", false)
4443

app/src/main/java/com/goldenraven/padawanwallet/ui/chapters/ChaptersViewModel.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ import android.util.Log
1010
import androidx.compose.runtime.MutableState
1111
import androidx.compose.runtime.mutableStateOf
1212
import androidx.lifecycle.AndroidViewModel
13-
import androidx.lifecycle.LiveData
14-
import androidx.lifecycle.MutableLiveData
1513
import androidx.lifecycle.viewModelScope
1614
import com.goldenraven.padawanwallet.R
1715
import com.goldenraven.padawanwallet.data.chapters.*
1816
import kotlinx.coroutines.*
17+
import kotlinx.coroutines.flow.MutableStateFlow
18+
import kotlinx.coroutines.flow.StateFlow
1919

2020
private const val TAG = "ChaptersViewModel"
2121

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

25-
private val readAllData: LiveData<List<Chapter>>
25+
private val readAllData: MutableStateFlow<List<Chapter>> = MutableStateFlow(emptyList())
2626
private val chapterRepository: ChapterRepository
2727

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

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

@@ -53,7 +53,12 @@ class ChaptersViewModel(application: Application) : AndroidViewModel(application
5353
}
5454
}
5555
// this variable is null on first access, and hence triggers the reinitialization of the database content
56-
readAllData = chapterRepository.readAllData
56+
viewModelScope.launch {
57+
chapterRepository.readAllData
58+
.collect { result ->
59+
readAllData.value = result
60+
}
61+
}
5762
Log.i(TAG, "readAllData variable is ${readAllData.value}")
5863
updateSelectedChapter(1) // TODO Change to most recent chapter
5964
_chapterPageMap = initChapterPageMap()
@@ -95,7 +100,7 @@ class ChaptersViewModel(application: Application) : AndroidViewModel(application
95100
val chapter: Chapter = async {
96101
chapterRepository.getChapter(id)
97102
}.await()
98-
selectedChapterData.setValue(chapter)
103+
selectedChapterData.value = chapter
99104
}
100105
}
101106

app/src/main/java/com/goldenraven/padawanwallet/ui/wallet/ReceiveScreen.kt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,8 @@ import androidx.compose.foundation.text.selection.SelectionContainer
88
import androidx.compose.material3.Button
99
import androidx.compose.material3.ButtonDefaults
1010
import androidx.compose.material3.Text
11-
import androidx.compose.runtime.Composable
12-
import androidx.compose.runtime.LaunchedEffect
11+
import androidx.compose.runtime.*
1312
import androidx.compose.ui.Modifier
14-
import androidx.compose.runtime.getValue
15-
import androidx.compose.runtime.livedata.observeAsState
16-
import androidx.compose.runtime.mutableStateOf
17-
import androidx.compose.runtime.remember
18-
import androidx.compose.runtime.setValue
1913
import androidx.compose.ui.Alignment
2014
import androidx.compose.ui.graphics.ImageBitmap
2115
import androidx.compose.ui.unit.dp
@@ -37,7 +31,7 @@ internal fun ReceiveScreen(
3731
navController: NavHostController,
3832
viewModel: WalletViewModel
3933
) {
40-
val address by viewModel.address.observeAsState("Generate new address")
34+
val address by viewModel.address.collectAsState("Generate new address")
4135
var QR by remember {
4236
mutableStateOf<ImageBitmap?>(null)
4337
}

app/src/main/java/com/goldenraven/padawanwallet/ui/wallet/SendScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ internal fun SendScreen(navController: NavHostController, walletViewModel: Walle
6060

6161
val coroutineScope = rememberCoroutineScope()
6262

63-
val balance by walletViewModel.balance.observeAsState()
63+
val balance by walletViewModel.balance.collectAsState()
6464
val recipientAddress: MutableState<String> = rememberSaveable { mutableStateOf("") }
6565
val amount: MutableState<String> = rememberSaveable { mutableStateOf("") }
6666
val feeRate: MutableState<String> = rememberSaveable { mutableStateOf("") }

app/src/main/java/com/goldenraven/padawanwallet/ui/wallet/WalletRootScreen.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import androidx.compose.foundation.shape.RoundedCornerShape
1818
import androidx.compose.foundation.verticalScroll
1919
import androidx.compose.material3.*
2020
import androidx.compose.runtime.*
21-
import androidx.compose.runtime.livedata.observeAsState
2221
import androidx.compose.ui.Alignment
2322
import androidx.compose.ui.Modifier
2423
import androidx.compose.ui.draw.rotate
@@ -53,9 +52,9 @@ internal fun WalletRootScreen(
5352
navController: NavHostController,
5453
walletViewModel: WalletViewModel
5554
) {
56-
val balance by walletViewModel.balance.observeAsState()
57-
val transactionList by walletViewModel.readAllData.observeAsState(initial = emptyList())
58-
val isOnlineStatus by walletViewModel.isOnlineVariable.observeAsState()
55+
val balance by walletViewModel.balance.collectAsState()
56+
val transactionList by walletViewModel.readAllData.collectAsState(initial = emptyList())
57+
val isOnlineStatus by walletViewModel.isOnlineVariable.collectAsState()
5958
val tempOpenFaucetDialog = walletViewModel.openFaucetDialog
6059
val context = LocalContext.current
6160

app/src/main/java/com/goldenraven/padawanwallet/ui/wallet/WalletViewModel.kt

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import androidx.compose.material.SnackbarDuration
1515
import androidx.compose.material.SnackbarHostState
1616
import androidx.compose.runtime.*
1717
import androidx.lifecycle.AndroidViewModel
18-
import androidx.lifecycle.LiveData
19-
import androidx.lifecycle.MutableLiveData
2018
import androidx.lifecycle.viewModelScope
2119
import com.goldenraven.padawanwallet.BuildConfig
2220
import com.goldenraven.padawanwallet.data.*
@@ -48,30 +46,35 @@ private const val TAG = "WalletViewModel"
4846
class WalletViewModel(
4947
application: Application,
5048
) : AndroidViewModel(application) {
51-
val readAllData: LiveData<List<Tx>>
49+
val readAllData: MutableStateFlow<List<Tx>> = MutableStateFlow<List<Tx>>(emptyList<Tx>())
5250
private val repository: TxRepository
5351
var openFaucetDialog: MutableState<Boolean> = mutableStateOf(false)
5452

55-
private var _balance: MutableLiveData<ULong> = MutableLiveData(0u)
56-
val balance: LiveData<ULong>
53+
private var _balance: MutableStateFlow<ULong> = MutableStateFlow(0u)
54+
val balance: StateFlow<ULong>
5755
get() = _balance
5856

59-
private var _address: MutableLiveData<String> = MutableLiveData("No address yet")
60-
val address: LiveData<String>
57+
private var _address: MutableStateFlow<String> = MutableStateFlow("No address yet")
58+
val address: StateFlow<String>
6159
get() = _address
6260

6361
private val _isRefreshing: MutableStateFlow<Boolean> = MutableStateFlow(false)
6462
val isRefreshing: StateFlow<Boolean>
6563
get() = _isRefreshing.asStateFlow()
6664

67-
var isOnlineVariable: MutableLiveData<Boolean> = MutableLiveData(false)
65+
var isOnlineVariable: MutableStateFlow<Boolean> = MutableStateFlow(false)
6866

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

7270
val txDao: TxDao = TxDatabase.getDatabase(application).txDao()
7371
repository = TxRepository(txDao)
74-
readAllData = repository.readAllData
72+
viewModelScope.launch {
73+
repository.readAllData
74+
.collect { result ->
75+
readAllData.value = result
76+
}
77+
}
7578

7679
if (isOnlineVariable.value == true && !Wallet.blockchainIsInitialized()) {
7780
Wallet.createBlockchain()
@@ -114,7 +117,7 @@ class WalletViewModel(
114117

115118
private fun getLastUnusedAddress(): AddressInfo {
116119
val address = Wallet.getLastUnusedAddress()
117-
_address.setValue(address.address)
120+
_address.value = address.address
118121
return address
119122
}
120123

@@ -226,7 +229,7 @@ class WalletViewModel(
226229
false
227230
}
228231
Log.i(TAG, "Updating online status to $onlineStatus")
229-
isOnlineVariable.setValue(onlineStatus)
232+
isOnlineVariable.value = onlineStatus
230233
}
231234

232235
fun broadcastTransaction(

0 commit comments

Comments
 (0)