Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
5.0 kB
3
Indexable
Never
@HiltViewModel
class GoalExpansionViewModel @Inject constructor(
    private val ucs: CURDUseCases,           //database repo 
    private val cq : QueriesToGetChildren,  //database repo
) : ViewModel()
{
    private val _state = MutableStateFlow(GoalExpansionScreenState())
    val state = _state.asStateFlow()

//database has parent child refrences stroed as parentId
//So the function does what it says in mutableList, I also have a function that can return flow but I didnt used it
//The childrens can be several levels down, but this function will return us only immediate children

    var cl = state.map {
        cq.getImmediateChildrenForAnIdInMutableList(it.headerId) 
    }

// need to initiate for initial childrens to show up asap

    init {
        cl = state.map {
            cq.getImmediateChildrenForAnIdInMutableList(it.headerId)
        }
        }



    fun onEvent(e : GoalExpansionScreenEvent)
    {
        when(e)
        {

            is GoalExpansionScreenEvent.RealmEntityChanged ->
            {
                _state.update {
                    it.copy(
                        rE = e.value
                    )
                }
            }

            is GoalExpansionScreenEvent.SelectedIdChanged ->
            {
                _state.update {
                    it.copy(
                        selectedId = e.value
                    )
                }
            }

            is GoalExpansionScreenEvent.SelectedNameChanged ->
            {
                _state.update {
                    it.copy(
                        selectedName = e.value
                    )
                }
            }

            is GoalExpansionScreenEvent.TitleChanged ->
            {
                _state.update {
                    it.copy(
                        title = e.value
                    )
                }
            }

            is GoalExpansionScreenEvent.HeaderTitleChanged ->
            {
                _state.update {
                    it.copy(
                        headerName = e.value
                    )
                }
            }

            is GoalExpansionScreenEvent.HeaderIdChanged ->
            {
                _state.update {
                    it.copy(
                        headerId = e.value
                    )
                }
            }

            is GoalExpansionScreenEvent.ModeChanged -> {
                _state.update {
                    it.copy(
                        mode = e.value
                    )
                }
            }

            is GoalExpansionScreenEvent.CompletionChanged ->
            {
                _state.update {
                    it.copy(
                        isCompleted = e.value
                    )
                }
            }

            GoalExpansionScreenEvent.MarkCompleteOrInComplete ->
            {

                    viewModelScope.launch(Dispatchers.IO)
                    {
                        ucs.u.updateANodeCompletion(
                            rE = state.value.rE,
                            isCompleted = state.value.isCompleted
                        )


                    }

            }

//This is the function that I used after saving every entry to update the list of childrens

            GoalExpansionScreenEvent.RefreshListOfImmediateChildren ->
            {
                viewModelScope.launch(Dispatchers.IO)
                {
                    cl = state.map {
                        cq.getImmediateChildrenForAnIdInMutableList(it.headerId)
                    }
                }
            }


            GoalExpansionScreenEvent.ChangeDialogVisibility ->
            {
                _state.update {
                    it.copy(
                        showDialog = !state.value.showDialog
                    )
                }
            }

            GoalExpansionScreenEvent.SaveOrEdit ->
            {
                viewModelScope.launch(Dispatchers.IO) {


                    when(state.value.mode)
                    {

                        ModeConstants.ADD->{
                            ucs.a.addAChildNode(
                                cname = state.value.title,
                                pId = state.value.headerId
                            )
                        }
                        ModeConstants.EDIT->{
                            val id = state.value.selectedId
                            val title = state.value.title
                            ucs.u.updateANodeName(
                                id = id,
                                updatedName = title
                            )
                        }
                    }

                }

            }

            GoalExpansionScreenEvent.Delete ->
            {
                viewModelScope.launch(Dispatchers.IO)
                {
                    ucs.d.deleteAChildNode(state.value.rE)
                }
            }
        }
    }


}