Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video we refactor the app to start using a View / Presenter model!
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We've just learned that the Android STK
0:00
contains a different set of classes
than we would find on a device.
0:02
Notably, a device contains all of
the classes for the Android framework, and
0:06
the STK only includes the classes
we need to know about to make apps.
0:11
But before we get back to testing our app,
0:16
let's take a minute to clean
up the mess we created.
0:18
Let's close all the files
that aren't in our project.
0:22
And let's also remove the test options
block from our build.gradle file.
0:34
Then let's hit Sync Now and
also minimize the Android monitor.
0:42
Now let's flip back to
our MainActivityTest and
0:47
see what needs to be changed.
0:50
We just saw that we aren't
able to create an activity.
0:52
So we can't use anything
involving MainActivity,
0:56
which leaves us with nothing.
0:59
It looks like we'll need to take
a different approach to testing this.
1:02
First, in the non-testing Java package,
so just the regular one,
1:07
we're going to create a new
interface named MainActivityView.
1:12
This interface will have one method for
every action we can take on our view.
1:28
So, one method for updating the TextView,
1:34
void changeTextViewText,
1:40
which takes in a string of text.
1:43
One method for
changing the background color,
1:47
void changeBackgroundColor,
which takes an int for
1:52
the color, and one method for
launching the other activity,
1:57
void launchOtherActivity(Class activity).
2:03
Here's where we need to pass in the class
of the activity we'd like to launch,
2:08
looking good.
2:12
Now let's make MainActivity implement
our new MainActivityView interface.
2:13
And use Alt+Enter to
implement the three methods,
2:23
then let's fill in these new methods.
2:27
changeTextViewText will take the textView,
and
2:30
we'll call setText and
pass in the text parameter.
2:34
changeBackgroundColor will
take the linearLayout and
2:42
set the background color to
the color from the parameter.
2:46
And let's just copy in this last one.
2:53
And then change OtherActivity.Class
to be our activity parameter.
3:00
All right, we've created and implemented
our MainActivityView interface.
3:06
You might think that now we should update
the rest of this code to use these new
3:11
methods, but actually these methods
should never be called by the activity.
3:15
The only class allowed to call these new
methods is our MainActivityPresenter
3:21
class, which doesn't exist yet.
3:26
So let's create a new class
named MainActivityPresenter.
3:28
Then at the top of the class, let's create
3:40
a new MainActivityView field named view.
3:44
And let's also add a constructor
to populate this new field.
3:49
Then we need to add a method for
each of our three actions.
4:06
For each action we'll just be calling the
appropriate method from MainActivityView.
4:10
Let's start by creating a new
method called editTextUpdated,
4:16
public void editTextUpdated and
4:21
it takes in a string,
which we'll name text.
4:27
And when this method is called,
all it should do is call
4:32
view.changeTextViewText while passing
along the text view parameter.
4:35
Next, let's do the same thing for
changing the background color.
4:43
Let's create a new method named
colorSelected, public void colorSelected,
4:47
which takes in an integer for
the index of the selected color.
4:55
Then let's cut out the switch
statement from MainActivity.
5:02
And paste it inside this
new colorSelected method,
5:12
and let's change
linearLayout.setBackgroundColor
5:18
to view.changeBackgroundColor.
5:24
Finally, to finish off our
presenter let's add one last method
5:38
named LaunchOtherActivityButtonClicked.
5:43
ButtonClicked, then let's make it
take in a class named activity,
5:51
and let's add the call to
view.launchOtherActivity and
5:58
pass along the activity,
and that will do it.
6:04
All that's left is to update
MainActivity to make use of our new
6:09
MainActivityPresenter over
a main activity.
6:13
Let's start by adding a new
MainActivityPresenter field named
6:17
presenter.
6:21
Then, right above where we initialize our
views, let's initialize our new presenter.
6:27
Presenter = new MainActivityPresenter and
pass in this for
6:38
the MainActivityView parameter because
MainActivity implements MainActivityView.
6:43
Now instead of having
the activity update our views,
6:53
we'll have the presenter update our views.
6:57
So when a user edits the editText
instead of calling textView.setText,
7:00
we'll let the presenter
handle it by calling
7:05
presenter.editTextUpdated.
7:13
Similarly, when a user selects
a color from the spinner,
7:17
we'll let the presenter
handle it by calling
7:21
presenter.colorSelected and
passing in the index.
7:24
And it's the same story for
launching our other activity.
7:30
Instead of having MainActivity handle it,
7:34
we'll delegate it to
the presenter by calling
7:36
presenter.launchOtherActivityButtonClicked
and
7:39
passing in our OtherActivity.class.
7:47
All right, let's change our
configuration back to app and
7:52
then run the app real quick to make
sure that everything still works.
7:56
Looks great.
8:11
In the next video we'll take a closer
look at what all of this means, and
8:12
more importantly, we'll see how it helps
make unit testing a whole lot easier.
8:16
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up