Coder Social home page Coder Social logo

expense-manager's People

Contributors

uttamnath64 avatar

Watchers

 avatar

expense-manager's Issues

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

return data in group by date

Repo 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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.