1 00:00:01,150 --> 00:00:04,320 It's about time we see something happen on the screen. 2 00:00:04,320 --> 00:00:07,560 If we tap on the deck enough, it should eventually become empty and 3 00:00:07,560 --> 00:00:09,300 switch to the blue card. 4 00:00:09,300 --> 00:00:13,270 But for that to happen, we need to call our deck views update method 5 00:00:13,270 --> 00:00:15,328 inside main activities update method. 6 00:00:15,328 --> 00:00:18,824 Let's start in main activity, by creating a new 7 00:00:18,824 --> 00:00:23,696 mutable variable at the top of the class to represent our deck view. 8 00:00:23,696 --> 00:00:25,400 Var deckView. 9 00:00:28,170 --> 00:00:31,070 And for the type, let's specify DeckView?. 10 00:00:32,700 --> 00:00:38,450 Right when MainActivity is initialized, we don't yet have a context. 11 00:00:38,450 --> 00:00:41,440 And we definitely haven't called on create yet. 12 00:00:41,440 --> 00:00:46,380 So there's no way for us to have an instance of DeckView right from the start. 13 00:00:46,380 --> 00:00:47,902 So we'll set it to null for now. 14 00:00:49,313 --> 00:00:51,792 Now that we've got our DeckView property, 15 00:00:51,792 --> 00:00:54,413 let's set it equal to our DeckView function. 16 00:01:00,339 --> 00:01:07,710 And then inside the update function and main activity, let's call deckView.update. 17 00:01:10,070 --> 00:01:15,490 And now we need to decide whether to use a safe call or a non null asserted call. 18 00:01:15,490 --> 00:01:19,520 This update function is triggered by the game presenter whenever a user takes 19 00:01:19,520 --> 00:01:24,010 an action, and users can only take actions on views. 20 00:01:24,010 --> 00:01:28,720 Which means at this point, the views should exist so we should be fine and 21 00:01:28,720 --> 00:01:30,850 asserting that our deck view is not normal. 22 00:01:33,680 --> 00:01:35,110 All right now let's run the app. 23 00:01:36,870 --> 00:01:41,580 And then tap the deck a bunch of times, AKA, exactly 24 times. 24 00:01:49,870 --> 00:01:54,970 And there we go, we've emptied the deck and we see the blue card. 25 00:01:54,970 --> 00:01:58,700 One more tap, and we've recycled the waste pile into the deck. 26 00:02:00,050 --> 00:02:03,340 Now that we've got our deck view out of the way, let's move on to our waste pile. 27 00:02:04,880 --> 00:02:13,089 Let's create a new class named waste pile view. 28 00:02:17,030 --> 00:02:19,066 And then let's make it extend image view. 29 00:02:23,200 --> 00:02:26,722 And then let's add the context parameter just like we had to do last time. 30 00:02:38,907 --> 00:02:40,990 Next, let's add our init function. 31 00:02:43,010 --> 00:02:49,560 And inside, let's set the imageResource to our emptyPileDrawable. 32 00:02:51,080 --> 00:02:52,895 Then, we just need to add an onClick listener. 33 00:02:55,816 --> 00:03:00,890 And inside, all we need to do is call GamePresenter.onWasteTap. 34 00:03:00,890 --> 00:03:06,620 With our new function finished, it's time to move on to the update function. 35 00:03:07,940 --> 00:03:12,910 Let's type fun update, and add our brackets. 36 00:03:12,910 --> 00:03:16,317 And inside this function, if there are cards in the waste pile, 37 00:03:16,317 --> 00:03:18,560 we need to show the top card. 38 00:03:18,560 --> 00:03:21,220 Otherwise we need to show an empty pile. 39 00:03:21,220 --> 00:03:25,430 Let's start by getting a list of which cards are in the waste pile with. 40 00:03:25,430 --> 00:03:30,150 Val cards equals game model .waste pile. 41 00:03:31,690 --> 00:03:37,460 Then let's set image resource Equal to the result of 42 00:03:37,460 --> 00:03:42,910 if (cards.size > 0 and 43 00:03:42,910 --> 00:03:47,560 then let's type two spaces for now and 44 00:03:47,560 --> 00:03:52,460 then add our else and if it is equal to zero it should show the emptyPileDrawable. 45 00:03:53,810 --> 00:03:58,820 Now we just need to find a way to get the right image resource for a card. 46 00:03:58,820 --> 00:04:01,998 So back in our two spaces, 47 00:04:01,998 --> 00:04:07,632 let's call a function that doesn't exist yet 48 00:04:07,632 --> 00:04:13,730 called getResIdForCard(cards.last()). 49 00:04:13,730 --> 00:04:16,300 Then let's use Alt+Enter to create this method. 50 00:04:19,030 --> 00:04:21,870 And let's change the parameter name from last to card. 51 00:04:23,030 --> 00:04:25,850 Inside this function we're going to need to be able to tell 52 00:04:25,850 --> 00:04:28,600 Android the name of the resource we want to show. 53 00:04:29,920 --> 00:04:33,660 Luckily, our resources have nice predictable names. 54 00:04:33,660 --> 00:04:38,934 They're all the word card followed by the suit followed by the value. 55 00:04:41,940 --> 00:04:45,750 So let's start by creating a new val named resource name. 56 00:04:47,620 --> 00:04:52,350 And let's set it equal to, and quotes, the word card. 57 00:04:53,950 --> 00:04:58,189 Followed by a string template to get the card suit. 58 00:04:59,348 --> 00:05:01,970 Card.suit. 59 00:05:01,970 --> 00:05:03,480 And then another string template for 60 00:05:03,480 --> 00:05:07,590 the cards value, which we want to get by using our cards map. 61 00:05:09,070 --> 00:05:17,520 So cardsMap[card.value]. 62 00:05:17,520 --> 00:05:20,285 Finally, at the end, let's add a call to 63 00:05:20,285 --> 00:05:25,570 .toLowerCase, to make sure that everything matches. 64 00:05:26,710 --> 00:05:31,778 Then, on the next line, let's return 65 00:05:31,778 --> 00:05:37,619 context.resources.getIdentifier, and 66 00:05:37,619 --> 00:05:43,007 then pass in the resource name for the name 67 00:05:46,168 --> 00:05:52,928 drawable as a string for the type, And 68 00:05:52,928 --> 00:05:59,090 context.packageName for the package, nice. 69 00:06:00,630 --> 00:06:04,782 We're probably going to want this to getResIdForCard method and 70 00:06:04,782 --> 00:06:06,749 more than just this one class. 71 00:06:06,749 --> 00:06:11,258 So instead of having this method be private to our waste pile view, 72 00:06:11,258 --> 00:06:13,925 let's get rid of the private keyword. 73 00:06:16,900 --> 00:06:22,203 And then cut this function and paste it into main 74 00:06:22,203 --> 00:06:27,121 activity, below our empty pile drawable. 75 00:06:27,121 --> 00:06:31,927 For the last step, since it's our views that need the resource ID's let's make 76 00:06:31,927 --> 00:06:34,130 this an extension function on view. 77 00:06:36,098 --> 00:06:41,175 And there we go back and wastePileView all we have left is adding our 78 00:06:41,175 --> 00:06:47,730 Ankle View call just like we did with our DeckView, but before we get to that. 79 00:06:47,730 --> 00:06:49,410 Let's take another look at our DeckView. 80 00:06:51,370 --> 00:06:54,710 At the bottom of our DeckView, we have the DeckView factory, 81 00:06:54,710 --> 00:06:57,930 where we take in a context and return an instance of deck view. 82 00:06:59,290 --> 00:07:02,934 And just like any other lambda expression with one parameter, 83 00:07:02,934 --> 00:07:07,208 we are allowed to omit that parameter and instead just use the it keyword. 84 00:07:13,315 --> 00:07:15,750 Well not exactly. 85 00:07:15,750 --> 00:07:20,684 We can only do that if Cullen is able to figure out what it is, 86 00:07:20,684 --> 00:07:23,371 and Cullen can't do that here. 87 00:07:23,371 --> 00:07:27,096 But what we can do is cut this lambda expression, and 88 00:07:27,096 --> 00:07:30,490 paste it in as the first parameter to anko view. 89 00:07:32,950 --> 00:07:37,190 This first parameter is a function with one context parameter. 90 00:07:37,190 --> 00:07:42,260 So here, it has to be a context and everything works. 91 00:07:42,260 --> 00:07:45,732 Meaning we no longer need our deck view factory, sweet. 92 00:07:48,149 --> 00:07:53,869 Now, to finish out our waste pile view let's copy this ankoView view from 93 00:07:53,869 --> 00:08:00,060 deckView, and then in our wastePileView, let's paste it in below the class. 94 00:08:02,010 --> 00:08:06,140 Then let's change the function name from deckView to wastePileView. 95 00:08:08,700 --> 00:08:12,630 And change the init function to be an extension function on WastePileView. 96 00:08:14,280 --> 00:08:15,000 And lastly, 97 00:08:15,000 --> 00:08:18,790 let's change the factory function to return an instance of WastePileView. 98 00:08:20,520 --> 00:08:24,900 Now that we've finished our WastePileView, let's head back to main activity and 99 00:08:24,900 --> 00:08:26,550 create a new property to store it. 100 00:08:28,430 --> 00:08:32,960 Let's create it right below deck view And name it wastePileView. 101 00:08:35,740 --> 00:08:39,461 Give it a type of WastePileView?, and set it equal to null. 102 00:08:41,989 --> 00:08:47,050 Then inside our layout let's delete the imageView below our deck view And 103 00:08:47,050 --> 00:08:52,125 then set our new wastePileView property equal to our new wastePileView. 104 00:08:55,250 --> 00:09:01,261 Then let's add a call to lparams and pass in the code dimensions. 105 00:09:05,328 --> 00:09:09,014 Finally, inside the update method lets add 106 00:09:09,014 --> 00:09:13,210 a null asserted call to wastePileView.update. 107 00:09:13,210 --> 00:09:17,447 Now, if we run the app we should be able to click on the deck and 108 00:09:17,447 --> 00:09:19,958 see our waste pile start updating. 109 00:09:22,625 --> 00:09:25,165 Looks great.