Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
It's about time we see something happen! In this video we'll update the screen so we can see some of the action!
-
0:01
It's about time we see something happen on the screen.
-
0:04
If we tap on the deck enough, it should eventually become empty and
-
0:07
switch to the blue card.
-
0:09
But for that to happen, we need to call our deck views update method
-
0:13
inside main activities update method.
-
0:15
Let's start in main activity, by creating a new
-
0:18
mutable variable at the top of the class to represent our deck view.
-
0:23
Var deckView.
-
0:28
And for the type, let's specify DeckView?.
-
0:32
Right when MainActivity is initialized, we don't yet have a context.
-
0:38
And we definitely haven't called on create yet.
-
0:41
So there's no way for us to have an instance of DeckView right from the start.
-
0:46
So we'll set it to null for now.
-
0:49
Now that we've got our DeckView property,
-
0:51
let's set it equal to our DeckView function.
-
1:00
And then inside the update function and main activity, let's call deckView.update.
-
1:10
And now we need to decide whether to use a safe call or a non null asserted call.
-
1:15
This update function is triggered by the game presenter whenever a user takes
-
1:19
an action, and users can only take actions on views.
-
1:24
Which means at this point, the views should exist so we should be fine and
-
1:28
asserting that our deck view is not normal.
-
1:33
All right now let's run the app.
-
1:36
And then tap the deck a bunch of times, AKA, exactly 24 times.
-
1:49
And there we go, we've emptied the deck and we see the blue card.
-
1:54
One more tap, and we've recycled the waste pile into the deck.
-
2:00
Now that we've got our deck view out of the way, let's move on to our waste pile.
-
2:04
Let's create a new class named waste pile view.
-
2:17
And then let's make it extend image view.
-
2:23
And then let's add the context parameter just like we had to do last time.
-
2:38
Next, let's add our init function.
-
2:43
And inside, let's set the imageResource to our emptyPileDrawable.
-
2:51
Then, we just need to add an onClick listener.
-
2:55
And inside, all we need to do is call GamePresenter.onWasteTap.
-
3:00
With our new function finished, it's time to move on to the update function.
-
3:07
Let's type fun update, and add our brackets.
-
3:12
And inside this function, if there are cards in the waste pile,
-
3:16
we need to show the top card.
-
3:18
Otherwise we need to show an empty pile.
-
3:21
Let's start by getting a list of which cards are in the waste pile with.
-
3:25
Val cards equals game model .waste pile.
-
3:31
Then let's set image resource Equal to the result of
-
3:37
if (cards.size > 0 and
-
3:42
then let's type two spaces for now and
-
3:47
then add our else and if it is equal to zero it should show the emptyPileDrawable.
-
3:53
Now we just need to find a way to get the right image resource for a card.
-
3:58
So back in our two spaces,
-
4:01
let's call a function that doesn't exist yet
-
4:07
called getResIdForCard(cards.last()).
-
4:13
Then let's use Alt+Enter to create this method.
-
4:19
And let's change the parameter name from last to card.
-
4:23
Inside this function we're going to need to be able to tell
-
4:25
Android the name of the resource we want to show.
-
4:29
Luckily, our resources have nice predictable names.
-
4:33
They're all the word card followed by the suit followed by the value.
-
4:41
So let's start by creating a new val named resource name.
-
4:47
And let's set it equal to, and quotes, the word card.
-
4:53
Followed by a string template to get the card suit.
-
4:59
Card.suit.
-
5:01
And then another string template for
-
5:03
the cards value, which we want to get by using our cards map.
-
5:09
So cardsMap[card.value].
-
5:17
Finally, at the end, let's add a call to
-
5:20
.toLowerCase, to make sure that everything matches.
-
5:26
Then, on the next line, let's return
-
5:31
context.resources.getIdentifier, and
-
5:37
then pass in the resource name for the name
-
5:46
drawable as a string for the type, And
-
5:52
context.packageName for the package, nice.
-
6:00
We're probably going to want this to getResIdForCard method and
-
6:04
more than just this one class.
-
6:06
So instead of having this method be private to our waste pile view,
-
6:11
let's get rid of the private keyword.
-
6:16
And then cut this function and paste it into main
-
6:22
activity, below our empty pile drawable.
-
6:27
For the last step, since it's our views that need the resource ID's let's make
-
6:31
this an extension function on view.
-
6:36
And there we go back and wastePileView all we have left is adding our
-
6:41
Ankle View call just like we did with our DeckView, but before we get to that.
-
6:47
Let's take another look at our DeckView.
-
6:51
At the bottom of our DeckView, we have the DeckView factory,
-
6:54
where we take in a context and return an instance of deck view.
-
6:59
And just like any other lambda expression with one parameter,
-
7:02
we are allowed to omit that parameter and instead just use the it keyword.
-
7:13
Well not exactly.
-
7:15
We can only do that if Cullen is able to figure out what it is,
-
7:20
and Cullen can't do that here.
-
7:23
But what we can do is cut this lambda expression, and
-
7:27
paste it in as the first parameter to anko view.
-
7:32
This first parameter is a function with one context parameter.
-
7:37
So here, it has to be a context and everything works.
-
7:42
Meaning we no longer need our deck view factory, sweet.
-
7:48
Now, to finish out our waste pile view let's copy this ankoView view from
-
7:53
deckView, and then in our wastePileView, let's paste it in below the class.
-
8:02
Then let's change the function name from deckView to wastePileView.
-
8:08
And change the init function to be an extension function on WastePileView.
-
8:14
And lastly,
-
8:15
let's change the factory function to return an instance of WastePileView.
-
8:20
Now that we've finished our WastePileView, let's head back to main activity and
-
8:24
create a new property to store it.
-
8:28
Let's create it right below deck view And name it wastePileView.
-
8:35
Give it a type of WastePileView?, and set it equal to null.
-
8:41
Then inside our layout let's delete the imageView below our deck view And
-
8:47
then set our new wastePileView property equal to our new wastePileView.
-
8:55
Then let's add a call to lparams and pass in the code dimensions.
-
9:05
Finally, inside the update method lets add
-
9:09
a null asserted call to wastePileView.update.
-
9:13
Now, if we run the app we should be able to click on the deck and
-
9:17
see our waste pile start updating.
-
9:22
Looks great.
You need to sign up for Treehouse in order to download course files.
Sign up