Search code examples
androidkotlinkotlin-flow

flow { collect{} emit{} } data not retning in usecase


i want got get in different formate for that i write a logic but collect{} method store data in entryGroup but after collect{} method entryGroup show empty and not return data.

Usecase

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

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
)

input data:

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"),
}

i want output like this:

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 ),
                
     )
)

App Module Provider

@Module
@InstallIn(SingletonComponent::class)
object AppModule {
    ...
    @Provides
    @Singleton
    fun providerEntryUseCases(repository: EntryRepository): EntryUseCases{
        return EntryUseCases(
            getEntries = GetEntries(repository),
            getEntriesByGroup = GetEntriesByGroup(repository),
            ...
        )
    }
}

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>>
}

ViewModel

@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)
    }
}

return data in group by date

Repository link: https://github.com/Uttamnath64/Expense-Manager

file like: https://github.com/Uttamnath64/Expense-Manager/blob/master/app/src/main/java/com/arvo/expensemanager/domain/usecase/entry/GetEntriesByGroup.kt


Solution

  • I use this logic to get answer!

    • map{} the first map method is use to convert entriese into entryGroups based on date.
    • map{} the secode map method is use to add items in group and get total of amount.
    • catch{} catch block run when some thing want worg.

    Usecase

    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}")
                }
        }
    }