@HiltViewModelclassTimeSheetsViewModel@Injectconstructor(valrepository:Repository):ViewModel(){privatevar_timeSheetsNetworkState:Flow<PagingData<TimeSheetModel>>?=nullfungetTimeSheets():Flow<PagingData<TimeSheetModel>>?{if(_timeSheetsNetworkState!=null){return_timeSheetsNetworkState}try{varisAllTime=falsevarisThisMonth=falsevarfirstItem=0if(SalesforceSDKManager.getInstance().userAccountManager.currentUser.userId!=null){valuserId=SalesforceSDKManager.getInstance().userAccountManager.currentUser.userIdvalnewResult: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 listreturn@insertSeparatorsnull}if(before==null){// we're at the beginning of the listisThisMonth=getLabel(after).contentEquals("This Month")isAllTime=getLabel(after).contentEquals("All Time")return@insertSeparatorsTimeSheetModel.SeparatorItem(getLabel(after))}elseif(firstItem==1){isThisMonth=getLabel(before).contentEquals("This Month")isAllTime=getLabel(before).contentEquals("All Time")return@insertSeparatorsTimeSheetModel.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 separatornull}}}.cachedIn(viewModelScope)_timeSheetsNetworkState=newResultreturnnewResult}}catch(e:Exception){Log.e("TimeSheetsViewModel","getTimeSheets: Exception: ${e}, ${e.cause}")e.printStackTrace()}returnnull}valtimeSheetsNetworkState:Flow<PagingData<TimeSheetModel>>get()=_timeSheetsNetworkState!!}sealedclassTimeSheetModel{dataclassTimeSheetItem(valtimeSheet:TimeSheet):TimeSheetModel()dataclassSeparatorItem(valtitle:String):TimeSheetModel()}
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.