Snippets

John View Model Timesheets

Created by John De la cruz
@HiltViewModel
class TimeSheetsViewModel
@Inject
constructor(val repository: Repository) : ViewModel() {
    private var _timeSheetsNetworkState: Flow<PagingData<TimeSheetModel>>? = null


    fun getTimeSheets(): Flow<PagingData<TimeSheetModel>>? {
        if (_timeSheetsNetworkState != null) {
            return _timeSheetsNetworkState
        }

        try {
            var isAllTime = false
            var isThisMonth = false
            var firstItem = 0
            if (SalesforceSDKManager.getInstance().userAccountManager.currentUser.userId != null) {
                val userId =
                    SalesforceSDKManager.getInstance().userAccountManager.currentUser.userId

                val newResult: Flow<PagingData<TimeSheetModel>> = repository.getTimeSheets(userId)
                    .map { pagingData -> pagingData.map {
                        TimeSheetModel.TimeSheetItem(it)

                    } }
                    .map {
                        it.insertSeparators { before, after ->

                            firstItem++

                            if (after == null) {
                                // we're at the end of the list
                                return@insertSeparators null
                            }
                            if (before == null) {
                                // we're at the beginning of the list
                                isThisMonth = getLabel(after).contentEquals("This Month")
                                isAllTime = getLabel(after).contentEquals("All Time")

                                return@insertSeparators TimeSheetModel.SeparatorItem(getLabel(after))
                            }else if (firstItem == 1){

                                isThisMonth = getLabel(before).contentEquals("This Month")
                                isAllTime = getLabel(before).contentEquals("All Time")

                                return@insertSeparators TimeSheetModel.SeparatorItem(getLabel(before))
                            }

                            if (!isSameWeekYear(
                                    before,
                                    after
                                )
                                && isCurrentMonthYear(after)
                                && !isThisMonth

                            ) {

                                isThisMonth = getLabel(after).contentEquals("This Month")
                                isAllTime = getLabel(after).contentEquals("All Time")
                                TimeSheetModel.SeparatorItem(getLabel(after))

                            }
                            if (
                                !isCurrentMonthYear(after)
                                && !isCurrentWeekYear(after)
                                && !isAllTime
                            ) {
                                isThisMonth = getLabel(after).contentEquals("This Month")
                                isAllTime = getLabel(after).contentEquals("All Time")
                                TimeSheetModel.SeparatorItem(getLabel(after))

                            } else {
                                // no separator
                                null
                            }
                        }
                    }
                    .cachedIn(viewModelScope)
                _timeSheetsNetworkState = newResult
                return newResult
            }


        } catch (e: Exception) {
            Log.e("TimeSheetsViewModel", "getTimeSheets: Exception: ${e}, ${e.cause}")
            e.printStackTrace()

        }

        return null
    }


    val timeSheetsNetworkState: Flow<PagingData<TimeSheetModel>>
        get() = _timeSheetsNetworkState!!

}

sealed class TimeSheetModel {
    data class TimeSheetItem(val timeSheet: TimeSheet) : TimeSheetModel()
    data class SeparatorItem(val title: String) : TimeSheetModel()
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.