Ray
Ray

Reputation: 1

how to make a variable truly unmutable

I'm struggling in my studies of Kotlin and jetpack Compose. Needless to say I've been at this (what I think should be) basic issue, for weeks with no result.

Looking for a way to make a variable truly unchangeable (until I dicide to change its value).

So, it is like this: I have a DB table and I want to count certain groups of records according to certain parameters. The function I use is all the same function and calls different queries in the DAO. It also stores the records in the same UISTATE var. So, yes the second call to the function overrides the value stored for the previus result.

Basically :

function that assigns 'myvar_B: var_A = 10 <<<<<< this var_A would be the UISTATE var where the result is stored (for all queries)

myvar_B = var_A

print myvar_B : 10

somewhere else in the project: var_A = 15 <<<<< because the query has been called and changes the UISTATE var's value

so now when I print myvar_B:

print myvar_B : 15.

Is there a way to secure the value of 'myvar_B', so it does not change until the function that assigns 'myvarB'.

If the function that makes the queries needed to more fully understand the issue, here it is:

private val _sortType = MutableStateFlow(SortType.CHECK_TO_ADD)
    private val _groceries = _sortType
        .flatMapLatest { _sortType ->
            when (_sortType) {
                SortType.ITEM_NAME -> dao.listAllItems()
                SortType.CHECK_TO_ADD -> dao.listAllItemsChecked()
                SortType.ONLY_CHECK_TO_ADD -> dao.listShoppingList()
                SortType.ONLY_CHECKED_CHECKED -> dao.listPurchasedItems()
            }

        }

    private val _state = MutableStateFlow(GroceriesState())
    val state = combine(_state, _sortType, _groceries) {
                                                       state, sortType, groceries ->
        state.copy(
            groceries = groceries,
            sortType = sortType
        )
    }.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), GroceriesState())

And here is the DAO, just in case:

//List purchased items
    @Query("SELECT * FROM grocery_table WHERE checkChecked = 1")
    fun listPurchasedItems() : Flow<List<Groceries>>


    //
    //       QUERIES IN THE SORT ROUTINE
    //
    //List the items in shopping list - INCLUDED IN SORT
    @Query("SELECT * FROM grocery_table WHERE checkToAdd = 1")
    fun listShoppingList() : Flow<List<Groceries>>


    //list all grocery items - INCLUDED IN SORT
    @Query("SELECT * FROM grocery_table ORDER BY itemName ASC")
    fun listAllItems(): Flow<List<Groceries>>


    //list all grocery items to create grocery list and add new items to DB- INCLUDED IN SORT
    @Query("SELECT * FROM grocery_table ORDER BY checkToAdd ASC, itemName ASC")
    fun listAllItemsChecked(): Flow<List<Groceries>>

Then in my composable I want to display the count of different groups:

 onEvent(GroceriesEvent.SortGroceries(SortType.ONLY_CHECKED_CHECKED))
    val countPurchased: Int = state.groceries.size
    onEvent(GroceriesEvent.SortGroceries(SortType.ONLY_CHECK_TO_ADD))
    val countSList:Int = state.groceries.size
Text(
                    text = "Shopping List",
                    modifier = Modifier
                        .weight(1f)
                        .padding(start = 40.dp),
                    fontSize = 17.sp,
                    fontWeight = FontWeight.SemiBold,
                    color = md_theme_dark_onTertiaryContainer
                )
                Text(
                    text = "${countSList}/${countPurchased}",
                    modifier = Modifier,
                    fontSize = 17.sp,
                    fontWeight = FontWeight.SemiBold,
                    color = md_theme_dark_onTertiaryContainer
                )

In my limited knowledge of Jetpack Compose, I tried to create val's in the UISTATE for the different counts, but the IDE got in the way with some explanation I didn't understand. I tried also to create val's in the ViewModel, but didn't work out either. I think the answer should be a special var which I don't know about; although the answer could somewhere else altogether. The fact that I haven't found the solution here and in other sources leads me to believe I may be barking up the wrong tree.

Upvotes: 0

Views: 66

Answers (0)

Related Questions