提问人:Uttam Nath 提问时间:10/29/2023 最后编辑:Uttam Nath 更新时间:10/29/2023 访问量:54
flow { collect{} emit{} } 数据在用例中不重新显示
flow { collect{} emit{} } data not retning in usecase
问:
我想得到不同的格式,因为我写了一个逻辑,但方法将数据存储在entryGroup中,但在方法显示空并且不返回数据之后。collect{}
collect{}
entryGroup
使用案例
class GetEntriesByGroup(
private val repository: EntryRepository
) {
operator fun invoke(): Flow<List<EntryGroup>> {
return flow {
try {
val entries = repository.getEntries()
val entryGroups = mutableListOf<EntryGroup>()
entries.collect { entryList ->
val groupedEntries = entryList.groupBy { it.timestamp }
var index = 0
groupedEntries.map { (datetime, entries) ->
index++
var total = 0.0
entries.map {
total = if (it.paymentType) total + it.amount else total - it.amount
}
val adjustedEntries = entries.map { entry ->
Entry(
id = entry.id,
bookId = entry.bookId,
title = entry.title,
description = entry.description,
amount = entry.amount,
paymentType = entry.paymentType,
paymentMethod = entry.paymentMethod,
timestamp = entry.timestamp
)
}
entryGroups.add(
EntryGroup(
id = index,
datetime = datetime,
total = total,
data = adjustedEntries
)
)
Log.i("uttam2", entryGroups.toString())
}
Log.i("uttam3", entryGroups.toString())
}
val sortedEntryGroups = entryGroups.sortedByDescending { it.datetime }
emit(sortedEntryGroups)
} catch (e: Exception) {
// Handle and log the error
Log.e("GetEntriesByGroup", "Error in data retrieval: ${e.message}")
}
}
}
}
数据类
data class EntryGroup(
val id: Int,
val data: List<Entry>,
val datetime: Long,
val total: Double
)
data class Entry(
@PrimaryKey val id: Int? = null,
val bookId: Int,
val title: String,
val description: String,
val amount: Double,
val paymentType: Boolean,
val paymentMethod: Int,
val timestamp: Long
)
输入数据:
listOf{
BookEntryStruct(1, "Enrty 1", 400, "Aug 14, 2023"),
BookEntryStruct(1, "Enrty 2", -100.0, "Aug 14, 2023"),
BookEntryStruct(1, "Enrty 3", -50.0, "Aug 15, 2023"),
BookEntryStruct(1, "Enrty 4", -100.0, "Aug 15, 2023"),
}
我想要这样的输出:
BookTabStruct(
1,
"Aug 15, 2023",
150.0,
listOf(
BookEntryStruct(4, "Enrty 4", -100.0, 150.0 ),
BookEntryStruct(3, "Enrty 3", -50.0, 250.0 ),
)
),
BookTabStruct(
2,
"Aug 14, 2023",
300.0,
listOf(
BookEntryStruct(2, "Enrty 2", -100.0, 300.0 ),
BookEntryStruct(1, "Enrty 1", 400.0, 400.0 ),
)
)
应用模块提供程序
@Module
@InstallIn(SingletonComponent::class)
object AppModule {
...
@Provides
@Singleton
fun providerEntryUseCases(repository: EntryRepository): EntryUseCases{
return EntryUseCases(
getEntries = GetEntries(repository),
getEntriesByGroup = GetEntriesByGroup(repository),
...
)
}
}
存储库 Dao
class EntryRepositoryImpl(
private val dao: EntryDao
): EntryRepository {
override fun getEntries(): Flow<List<Entry>> {
return dao.getEntries()
}
}
interface EntryRepository {
fun getEntries(): Flow<List<Entry>>
}
视图模型
@HiltViewModel
class ViewBookViewModel @Inject constructor(
private val entryUseCases: EntryUseCases
) : ViewModel() {
private val _state = mutableStateOf(EntryState())
val state: State<EntryState> = _state
private var getEntryJob: Job? = null
init {
getEntryByGroup()
}
private fun getEntryByGroup(){
getEntryJob?.cancel()
entryUseCases.getEntriesByGroup()
.onEach { entryGroup ->
_state.value = state.value.copy(
entryGroup = entryGroup
)
}
.launchIn(viewModelScope)
}
}
按日期分组返回数据
答:
0赞
Uttam Nath
10/29/2023
#1
我用这个逻辑来得到答案!
map{}
第一种 map 方法用于根据日期将 enriese 转换为 entryGroups。map{}
secode map 方法用于将项目添加到组中并获取总金额。catch{}
当某些东西想要 worg 时,catch block run。
使用案例
class GetEntriesByGroup(
private val repository: EntryRepository
) {
@RequiresApi(Build.VERSION_CODES.O)
operator fun invoke(): Flow<List<EntryGroup>> {
return repository.getEntries()
.map { entries ->
entries.groupBy { LocalDate.ofEpochDay(it.timestamp / 86400000) }
}
.map { groupedEntries ->
val entryGroups = mutableListOf<EntryGroup>()
var runningTotal = 0.0
for ((date, entries) in groupedEntries) {
runningTotal += entries.sumByDouble { if (it.paymentType) it.amount else -it.amount }
val adjustedEntries = entries.sortedByDescending { it.timestamp }.mapIndexed { i,entry ->
if(i < 4) {
Entry(
id = entry.id,
bookId = entry.bookId,
title = entry.title,
description = entry.description,
amount = entry.amount,
paymentType = entry.paymentType,
paymentMethod = entry.paymentMethod,
timestamp = entry.timestamp
)
}else{
null
}
}.mapNotNull { it }
entryGroups.add(
EntryGroup(
id = entryGroups.size + 1,
datetime = date.toEpochDay() * 86400000, // Convert back to milliseconds
total = runningTotal,
data = adjustedEntries
)
)
}
entryGroups.sortedByDescending { it.datetime }
}
.catch { e ->
// Handle and log the error
Log.e("GetEntriesByGroup", "Error in data retrieval: ${e.message}")
}
}
}
评论
1赞
Tenfour04
10/30/2023
是的,问题在于 DAO 流是无限的,因此调用它永远不会完成。使用是正确的处理方法。collect
map
评论
repository.getEntries()