Injecting Platform-specific Code7:12 with Heath Hodgert
Let's see how we can get around the limitations of our Portable Class Library by injecting platform-specific code into our shared library.
This service is something we've all done before to avoid duplicate code, 0:00 and it's even more useful for cross platform development. 0:03 To make this more interesting, let's use our favorite pizza parlor's 0:07 phone number and call them after we do the calculation. 0:12 The problem is, making a phone call with IOS is implemented different than Android. 0:16 To solve this problem, let's create an interface that does what we want. 0:22 Then we can implement it for each platform and 0:26 inject it into our pizzaCalculatorService. 0:30 In this shared project, add an interface. 0:35 IPhoneDialer, with a single method, call(), 0:45 which takes a string as a phone number. 0:48 Also, change the constructor of the PizzaCalculatorService so 1:04 requires the IPhoneDialer. 1:08 Now add a new method PizzaCalculatorService called 1:43 CallPizzaParlor. 1:47 Which we'll use the IPhoneDialer call method. 1:49 This method is known as dependency injection which is a great pattern for 2:18 unit testing and cross-platform development. 2:23 Dependency injection allows us to add custom implementation as needed. 2:27 But, because we use an interface the behavior should not change. 2:32 There's more on dependency injection in the teacher's notes. 2:37 In the Android project, we need to implement the IPhoneDialer interface. 2:41 In a concrete class to create a new dialer class that implements IPhoneDialer. 2:45 The method is simple, but Android requires a context and an intent to navigate. 3:10 The context is the running application context which is the activity. 3:16 So we'll pass it into the constructor of the phone dialer. 3:21 The intent is basically details to the Android 3:25 platform to determine which view to show. 3:29 The intent is a very important component of Android apps, and 3:34 there is more information in the teacher's notes. 3:37 First, we need to create a uri using the Android Framework. 3:55 Then we need to create our intent. 4:19 And tell it what kind of intent it is. 4:31 Then pass in the uri. 4:41 Then using the context, we can start an activity with that intent we created. 4:48 Probably want to show the results of the calculation before we call the pizza 4:58 parlor, so add another button to the app, to call the pizza parlor. 5:02 And we go into Resources > Layouts > Main.axml. 5:07 And again let's go to the source. 5:14 And add a button. 5:23 Called callButton. 5:38 Since we added a new string call, we'll need to go back to values, 6:11 Strings, and add a new string for the call key. 6:20 Using strings is good practice. 6:35 So if you ever need to change languages, you have one place to change it. 6:37 Now let's go back to main activity so we can find the button. 6:43 Of type button. 6:59 With a resource ID of callButton. 7:06
You need to sign up for Treehouse in order to download course files.Sign up