I'm a relative newbie to coding and mobile app development, but I'm keen, enthusiastic and want to learn!

This is the first question I have posed on SO, if I break etiquette in the way I ask, apologies in advance.

I am writing an app to track swimmers' personal best times, I have near completed my coding using Room to query sql db, and using the normal dao, repository and viewmodel architecture to return my swim time and qualification time. The last step is to subtract the swimmers' race time from the event qualification time. This is where I am struggling!

Originally, I tried nesting my livedata objects as below, but when doing the arithmetic in the inner, the outer livedata is not accessible.

**Code snippet:**

swimMeetViewModel.getQualificationTimes(queryQualTimes(scSwimDistance, scHKTable, swimMeetViewModel.getAgeGroup())).observe(getViewLifecycleOwner(), new Observer<List<String>>() {
                @Override
                public void onChanged(@Nullable List<String> qualificationTime) {

                    freeQual = qualificationTime.get(0);
                    free_qual.setText(freeQual);
                   
                    swimMeetViewModel.getFastTimes(queryFastTimes(swimMeetTable, AppConstants.free, scSwimDistance, SCMEET_TRUE)).observe(getViewLifecycleOwner(), new Observer<List<String>>() {
                        @Override
                        public void onChanged(@Nullable List<String> fastTime) {

                        freeTime = fastTime.get(0);
                        free_time.setText(freeTime);
                        }
                        freeDelta.setValue(toMMSS(toMilli(freeTime) - toMilli(freeQual)));
                    });
               }
         });

After some digging around on SO, I came across the following question that suggested that mediatorlivedata was the tool I needed to work with multiple livedata sources. However, I cannot seem to use the suggested code to solve my problem.

Livedata mathematical operations

Any assistance in helping to understand this code and explain how mediatorlivedata works would be gratefully accepted. Unfortunately, the Android Developer Reference is not very helpful in this regard.

Thanks in advance,

ilh

1

There are 1 best solutions below

0
On

I am also a beginner, so there can be mistakes in my answer, but I would like to suggest this. "Assuming qualificationTime.get(0) and fastTime.get(0) returns an Int"

swimMeetViewModel.getQualificationTimes(queryQualTimes(scSwimDistance, scHKTable, swimMeetViewModel.getAgeGroup())).observe(getViewLifecycleOwner(), new Observer<List<String>>() {
                @Override
                public void onChanged(@Nullable List<String> qualificationTime) {

                    freeQual = qualificationTime.get(0);
                    free_qual.setText(freeQual);
                    try{
                            freeDelta.setValue(toMMSS(toMilli(Integer.parseInt(free_time.getText())) - toMilli(freeQual)));
                    } catch(NumberFormatException e){}
               }
         });
swimMeetViewModel.getFastTimes(queryFastTimes(swimMeetTable, AppConstants.free, scSwimDistance, SCMEET_TRUE)).observe(getViewLifecycleOwner(), new Observer<List<String>>() {
                        @Override
                        public void onChanged(@Nullable List<String> fastTime) {

                        freeTime = fastTime.get(0);
                        free_time.setText(freeTime);
                        try{
                            freeDelta.setValue(toMMSS(toMilli(freeTime) - toMilli(Integer.parseInt(free_qual.getText()))));
                        } catch(NumberFormatException e){}
                        }
                    });