1
00:00:04,200 --> 00:00:05,500
Welcome back, everyone.

2
00:00:06,200 --> 00:00:07,700
Our report's coming along.

3
00:00:08,200 --> 00:00:10,560
In this video, we'll implement sorting.

4
00:00:11,560 --> 00:00:13,760
When the user taps one of the column headings,

5
00:00:14,160 --> 00:00:16,360
the data will be sorted by that column.

6
00:00:19,160 --> 00:00:22,040
Examine the DurationsViewModel code carefully

7
00:00:22,040 --> 00:00:24,040
to decide how you're going to achieve that.

8
00:00:24,440 --> 00:00:26,640
Yes, it's challenge time.

9
00:00:27,520 --> 00:00:30,520
Remember that the durations report activity

10
00:00:30,520 --> 00:00:33,020
will respond to the user tapping the headings,

11
00:00:33,420 --> 00:00:36,420
and the ViewModel will take care of loading the data.

12
00:00:38,720 --> 00:00:42,820
Add an onClick listener to the headings in the DurationsReport

13
00:00:42,820 --> 00:00:46,820
class to allow the data to be sorted by the various columns.

14
00:00:47,320 --> 00:00:49,620
Test your solution to make sure it works.

15
00:00:50,520 --> 00:00:54,620
Pause the video now and come back when you're ready to see my solution.

16
00:00:58,620 --> 00:00:59,820
So how did you get on?

17
00:01:00,480 --> 00:01:04,580
The only changes we need to make are in the DurationsReport class.

18
00:01:05,570 --> 00:01:09,670
And DurationsViewModel already deals with re-querying the data

19
00:01:09,670 --> 00:01:12,170
when its sort order property changes.

20
00:01:14,870 --> 00:01:16,370
For my solution,

21
00:01:16,370 --> 00:01:19,170
I'm going to get the DurationsReport class

22
00:01:19,170 --> 00:01:23,270
to implement the view.OnClickListener interface,

23
00:01:24,040 --> 00:01:26,340
that's probably the easiest way

24
00:01:26,340 --> 00:01:29,340
to use the same onClick listener for all the headings.

25
00:01:29,890 --> 00:01:33,390
Although, you could have declared an onClick listener object instead.

26
00:01:45,390 --> 00:01:49,690
I'll get android studio to generate the function after oncreate,

27
00:01:50,490 --> 00:01:51,850
and then we'll add the code.

28
00:02:23,210 --> 00:02:27,310
We've got an error on line 38 because we should be using

29
00:02:27,310 --> 00:02:29,010
the safe call operator.

30
00:02:29,510 --> 00:02:31,870
Using safe call is one solution,

31
00:02:32,230 --> 00:02:34,630
but this is an implementation of the views

32
00:02:34,630 --> 00:02:36,630
on ClickListener interface.

33
00:02:36,990 --> 00:02:40,990
What are the chances of this function being called by something that's null.

34
00:02:41,790 --> 00:02:44,290
Remember that google haven't finished annotating

35
00:02:44,290 --> 00:02:47,590
all the java methods and parameters with non-null.

36
00:02:48,190 --> 00:02:50,290
The view parameter can't be null.

37
00:02:50,290 --> 00:02:53,840
And another solution is to remove the nullable type marker

38
00:02:53,840 --> 00:02:55,200
from the declaration.

39
00:02:58,600 --> 00:03:01,600
All right. When one of the column headings is tapped,

40
00:03:02,000 --> 00:03:06,100
this function will receive the corresponding text view as an argument.

41
00:03:06,760 --> 00:03:10,860
We can use the ID of the text view to decide which one was tapped.

42
00:03:11,660 --> 00:03:15,650
A simple when statement says the view model sort order property.

43
00:03:16,310 --> 00:03:19,110
We've already got the ViewModel to reload the data

44
00:03:19,110 --> 00:03:20,770
when sort order changes,

45
00:03:21,130 --> 00:03:22,630
so that's all we need to do.

46
00:03:22,990 --> 00:03:26,690
In oncreate, we set the OnClick listener

47
00:03:26,690 --> 00:03:28,690
for each of the heading widgets.

48
00:03:45,890 --> 00:03:49,190
Remember, the description column won't exist in portrait.

49
00:03:50,070 --> 00:03:54,670
Using the save call operator will make sure we don't get a null pointer exception

50
00:03:54,970 --> 00:03:57,270
when attempting to set its OnClick listener,

51
00:03:57,770 --> 00:03:59,270
I'll run the app to test it.

52
00:04:04,370 --> 00:04:06,570
I'll start by tapping the date column.

53
00:04:08,070 --> 00:04:10,670
That sorts the data by date, which is good.

54
00:04:11,170 --> 00:04:14,370
The duration column also works fine.

55
00:04:15,670 --> 00:04:17,670
That scrambled the task names

56
00:04:18,070 --> 00:04:20,070
letting us test the task name column.

57
00:04:22,570 --> 00:04:24,130
That also seems to be working.

58
00:04:25,130 --> 00:04:28,930
If you get strange results when sorting by date, don't worry,

59
00:04:29,290 --> 00:04:31,090
there's a bug in this program

60
00:04:31,390 --> 00:04:35,090
but you won't notice it unless you're testing on a different time zone

61
00:04:35,090 --> 00:04:35,990
to GMT.

62
00:04:36,790 --> 00:04:39,150
The more hours away from GMT you are,

63
00:04:39,150 --> 00:04:41,650
the more chance there is you'll experience a bug.

64
00:04:42,150 --> 00:04:44,150
I'll be talking about this later.

65
00:04:44,550 --> 00:04:47,650
First, we're going to implement all the functionality

66
00:04:47,650 --> 00:04:50,150
then we'll look at spotting and fixing this bug.

67
00:04:50,930 --> 00:04:54,590
All right. We should also test sorting in landscape

68
00:04:54,890 --> 00:04:58,490
because we've got an extra column when displaying the report in landscape.

69
00:05:02,090 --> 00:05:04,350
This highlights a problem with our data

70
00:05:04,900 --> 00:05:07,000
as far as testing the reports goes.

71
00:05:07,660 --> 00:05:10,160
Our descriptions are a bit artificial,

72
00:05:10,820 --> 00:05:14,020
that was useful to validate the data in the database earlier,

73
00:05:14,270 --> 00:05:16,870
but it doesn't let us test sorting very easily.

74
00:05:17,670 --> 00:05:21,270
Tapping the description column doesn't do a lot

75
00:05:21,630 --> 00:05:25,090
because the task names and the task description sort

76
00:05:25,090 --> 00:05:27,090
in almost the same order.

77
00:05:27,590 --> 00:05:28,690
For a thorough test,

78
00:05:28,690 --> 00:05:32,190
you should change all the task descriptions to something more realistic.

79
00:05:32,990 --> 00:05:36,590
I'm not going to do that on the video, it does work though.

80
00:05:37,250 --> 00:05:39,750
A quick test is to sort on duration,

81
00:05:41,350 --> 00:05:42,850
and then tap on description

82
00:05:43,250 --> 00:05:44,750
to sort the column that way.

83
00:05:45,250 --> 00:05:47,050
That seems to be working as well.

84
00:05:47,550 --> 00:05:50,000
Part of the reason this was so simple

85
00:05:50,000 --> 00:05:52,900
was because of the clear separation between the

86
00:05:52,900 --> 00:05:56,560
DurationReports class and the DurationsViewModel.

87
00:05:57,160 --> 00:06:00,160
One question we often get asked by students is,

88
00:06:00,160 --> 00:06:02,660
how can i learn to design my code well?

89
00:06:03,060 --> 00:06:05,060
That comes with experience.

90
00:06:05,420 --> 00:06:08,420
Be prepared to create classes that are far from perfect.

91
00:06:08,780 --> 00:06:11,980
The more times you get it wrong, the sooner you'll get it right.

92
00:06:13,200 --> 00:06:17,000
It's not always easy to spot good design until you come to make a change.

93
00:06:17,800 --> 00:06:21,160
If the change is fairly easy and involves a few classes,

94
00:06:21,460 --> 00:06:23,260
then the design is probably good.

95
00:06:23,960 --> 00:06:27,460
If you really struggle and have to make changes all over the place,

96
00:06:27,460 --> 00:06:30,460
that's an indication that the design could be improved.

97
00:06:30,760 --> 00:06:32,960
All right. I'll stop this video here.

98
00:06:33,460 --> 00:06:37,160
In the next video, we'll make a start on creating the menu

99
00:06:37,160 --> 00:06:39,930
for the reports activity. See you then.

