Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Finishing the Presenter5:25 with Ben Deitch
In this video we'll see how we can update our view, and then we'll finish up our presenter!
We're updating our model but now we need to update our view as well, 0:00 which can be a little bit tricky especially with Android. 0:04 We'll need to wait for 0:08 the activities on create method before we can create the view. 0:08 So we aren't going to be able to initialize our view right from the get go. 0:12 To remedy this let's create a property for our view and 0:16 then once our view has been created, we'll call a function and 0:20 our game presenter class to populate our view property. 0:23 First let's make a property for our view and since we're going to be setting its 0:26 value later with a function, we'll need to declare this property as mutable. 0:31 At the the top of our class, let's type var view, give it a type of GameView. 0:35 And then, since we don't have anything to set it to yet, and 0:44 we have to set it to something, let's set it to null, which gives us an error, 0:48 null cannot be a value of non null type GameView. 0:53 Okay, so what does that mean? 0:58 Well in Kotlin if you want an object to be able to equal null you need to add 1:00 a question mark after the datatype, this is one of my favorite parts about Kotlin. 1:05 It's not much but when you have to take the time to think about whether a property 1:11 can be null or not I find that I end up writing better code. 1:15 And definitely have a lot fewer null pointer exceptions. 1:20 Anyway, now that we've got our view property let's create 1:23 a function to populate it. 1:25 And let's name it setGameView. 1:27 Let's make it taken the gameview as a parameter and then 1:30 inside this function let's set our view property equal to our gameView parameter. 1:35 And now that we've got access to our view let's update it at the bottom of our 1:42 onDeckTap function, view.update and 1:46 pass in our GameModel. 1:51 Nice, but what's this are all about? 1:54 Only safe or 1:57 non null asserted calls are allowed on a noble receiver of type game view. 1:58 This is just another way that Kotlin makes sure we really know what we're doing 2:04 when dealing with nulls. 2:08 You see Kotlin knows our view property can be null. 2:10 Kotlin also knows that it's possible that we're calling this 2:15 update function on a null object, which makes Kotlin very unhappy. 2:19 To solve this issue Kotlin gives us two options. 2:24 We can add a question mark before the call, and 2:28 now if our view is null, we won't call the update function and 2:31 this whole line will just return null or on the other hand 2:35 if our view is not null, then it will call the update function just like we want. 2:41 The other way we can handle this is to use two exclamation points. 2:46 When we precede a call with two exclamation points, 2:50 we are promising Kotlin that whatever is before the exclamation points is not 2:53 going to be null and if we promise that something isn't going to be null and 2:57 then it is null we'll get a null pointer exception. 3:02 So just to be on the safe side let's go back to using a safe call 3:06 by adding in our question mark. 3:10 Great work. 3:14 Now that we're done with the onDeckTap function 3:15 let's move on to our onWasteTap function. 3:17 Let's declare the function fun onWasteTap. 3:20 And then inside, let's update the model, 3:25 GameModel.onWasteTap and then update the view. 3:29 View? 3:34 To make sure that we null for the whole statement if our view is null, 3:36 .update and pass in our GameModel. 3:40 And actually, 3:43 wouldn't it be nice if we didn't have to pass in the GameModel each time? 3:44 After all, it is a singleton. 3:49 Let's head over to our GameView interface and see if we can't add our GameModel 3:51 instance as a default value, = GameModel. 3:56 Now back in our game presenter we can stop passing in our model as a parameter. 4:02 Nice, getting back to our two remaining functions to give us a head start. 4:10 Let's just copy them in from our game model class. 4:14 Then let's delete the function bodies, And 4:29 starting with onFoundationTap we need to first call 4:37 GameModel.onFoundationTap, and pass along the foundationIndex. 4:41 Then we need to update our view. 4:50 Finally, for our on tableau tap function, we need to call GameModel.onTableauTap. 4:53 Pass in our TableauIndex and our cardIndex and then update our view. 5:01 All right, we've completely finished our game of solitaire. 5:10 And while I know we won't be getting to the UI part until the next course, 5:13 that doesn't mean we can't see some sweet solitaire action in this course. 5:17 In the next stage, 5:21 we'll look into creating a test to make sure that our game is working. 5:22
You need to sign up for Treehouse in order to download course files.Sign up