1
00:00:06,050 --> 00:00:10,660
Everybody else is gone this is Caleb with Dev slopes and we're going to be revisiting a project from

2
00:00:10,720 --> 00:00:17,800
earlier in this section and it's the one where we built this calculator app OK we made an actual project

3
00:00:17,800 --> 00:00:18,670
here.

4
00:00:18,850 --> 00:00:23,670
We have a view controller of course we have some incomes for various things.

5
00:00:23,830 --> 00:00:30,760
And then in our model section we have what's called a calculator manager or I guess I should say a calculation

6
00:00:30,820 --> 00:00:32,060
manager.

7
00:00:32,060 --> 00:00:38,580
Now the interesting thing is is that this works very similarly to what is called a view model.

8
00:00:38,650 --> 00:00:41,590
And a view model is essentially a layer.

9
00:00:41,950 --> 00:00:47,110
Well you're familiar with model view controller and you know what each of those are individually Model

10
00:00:47,110 --> 00:00:51,590
View view model is another paradigm where there is of course models.

11
00:00:51,730 --> 00:00:59,800
There are controllers and then there are what are called view models and a view model is basically responsible

12
00:00:59,800 --> 00:01:05,170
for converting the data objects from the model and then presenting them in a easily displayable way.

13
00:01:05,200 --> 00:01:09,660
And that's basically what we're doing here is we've basically created a view model accidentally.

14
00:01:09,790 --> 00:01:14,950
This handles all of the logic that otherwise would need to be written in the view controller but we're

15
00:01:14,950 --> 00:01:20,800
abstracting it out of view controller and into a much more manageable layer called a view model so we're

16
00:01:20,800 --> 00:01:28,210
actually going to go ahead and rename this calculator calculator or view model.

17
00:01:28,210 --> 00:01:29,190
All right.

18
00:01:29,200 --> 00:01:34,900
And of course if we're going to be renaming this we need to rename everything so look for a calculation

19
00:01:35,290 --> 00:01:41,770
manager and we're going to replace that with calculator view model.

20
00:01:42,320 --> 00:01:42,820
OK.

21
00:01:43,060 --> 00:01:43,530
And click.

22
00:01:43,540 --> 00:01:48,490
All and it's going to go ahead and replace all of those and we should be good in here there isn't another

23
00:01:48,490 --> 00:01:50,070
instance of calculator if you model.

24
00:01:50,080 --> 00:01:55,830
But if I try to build this we're going to get a lot of problems particularly in the calculator View

25
00:01:55,840 --> 00:01:57,790
Controller and in our tests.

26
00:01:58,000 --> 00:02:00,560
So we're going to go ahead and do the same.

27
00:02:00,610 --> 00:02:07,090
Find and Replace find calculation manager and replace with calculator view model and actually the process

28
00:02:07,090 --> 00:02:08,380
that we use to do this.

29
00:02:08,380 --> 00:02:14,340
Creating an instance wups shoot well we're going to go back and put lowercase C's here I'm going to

30
00:02:14,350 --> 00:02:19,380
copy this anywhere that we are using that instance we're going to replace it.

31
00:02:19,960 --> 00:02:25,000
Of course copying and pasting is never the best option but it is an option.

32
00:02:25,000 --> 00:02:27,780
So we have a calculator view model here.

33
00:02:27,820 --> 00:02:35,800
Let's actually rename this file to Kelk Q. later or view model Swift.

34
00:02:35,820 --> 00:02:39,540
This is also calculator view modeled out Swift.

35
00:02:39,770 --> 00:02:41,040
And there we go.

36
00:02:41,040 --> 00:02:42,200
Very cool.

37
00:02:42,210 --> 00:02:48,390
Now if we look around make sure that we've done everything the right way.

38
00:02:48,810 --> 00:02:54,250
And if we come in here we can go ahead and type calculator view model tests.

39
00:02:54,340 --> 00:02:54,840
OK.

40
00:02:55,200 --> 00:02:57,250
I'm going to rename the file that as well.

41
00:02:57,330 --> 00:03:02,360
And I'm going to name this commented file name the same thing.

42
00:03:02,370 --> 00:03:08,010
Now of course calculation manager no longer exists but calculator view model does exist.

43
00:03:08,160 --> 00:03:13,050
And so this is going to be a set of tests to actually test our calculator view model to make sure that

44
00:03:13,050 --> 00:03:19,710
it works the same way as before and you know what I don't actually see any other instances here so I'm

45
00:03:19,710 --> 00:03:24,220
going to build it and see if it all builds successfully which it does.

46
00:03:24,240 --> 00:03:30,450
And so a view model allows us to essentially separate all of our logic like we have here.

47
00:03:30,460 --> 00:03:35,580
All of all of the things that basically convert the values perform the calculations.

48
00:03:35,700 --> 00:03:41,820
Basically what works with the data and creates it into a presentable way we can access through our instance

49
00:03:41,850 --> 00:03:48,840
of the view model so we can basically just create an instance of it and then call all of our logic on

50
00:03:48,840 --> 00:03:50,190
our view model which is very cool.

51
00:03:50,190 --> 00:03:52,710
So let's go ahead and let's build and run our tests.

52
00:03:52,800 --> 00:04:00,600
Let's make sure that they still run properly and work to make sure every single test passes and OK one

53
00:04:00,600 --> 00:04:01,260
test failed.

54
00:04:01,260 --> 00:04:04,150
So what is up with that.

55
00:04:04,200 --> 00:04:06,790
OK so it's saying zero is not equal to 25.

56
00:04:06,810 --> 00:04:08,880
We must have changed something here.

57
00:04:09,170 --> 00:04:10,820
Let's take a look.

58
00:04:11,710 --> 00:04:18,260
So when we push a number button it appends it to the array.

59
00:04:18,290 --> 00:04:24,610
Oh but it never converts them back into a string and sets the labels so let's go ahead and type value

60
00:04:24,600 --> 00:04:30,910
label dot text and that should be equal to our view model dot string of five values because if you remember

61
00:04:31,270 --> 00:04:37,150
that basically takes our array which when we append we add 1 to the array then it converted into a string

62
00:04:37,150 --> 00:04:39,490
so it can be displayed in the label.

63
00:04:39,610 --> 00:04:40,560
That's super weird OK.

64
00:04:40,570 --> 00:04:42,320
So let's try running those tests again.

65
00:04:42,400 --> 00:04:46,690
This is why testing is super helpful because it helps you to catch these weird problems that sometimes

66
00:04:46,690 --> 00:04:52,220
you might accidentally introduce if you were to accidentally delete a line of code and check it out.

67
00:04:52,240 --> 00:04:53,630
It says all test succeeded.

68
00:04:53,650 --> 00:04:57,410
Let's go to the test navigator here and everything works.

69
00:04:57,430 --> 00:05:03,070
Our view model is successfully instantiated and this is how you use model view view model to test view

70
00:05:03,070 --> 00:05:03,550
controllers.

71
00:05:03,550 --> 00:05:08,100
It makes it much easier to actually test the logic which you want to test.

72
00:05:08,290 --> 00:05:13,330
So anyway this is Caleb with Devis Loeb's dot com let's head on over to our exercise.
