1 00:00:00,620 --> 00:00:03,550 Okay, we're almost done with the model. 2 00:00:03,550 --> 00:00:07,180 There's just one more thing to consider, and account for. 3 00:00:07,180 --> 00:00:12,010 Right now, the icon property in our data model is storing a string value. 4 00:00:12,010 --> 00:00:15,030 That's because in the Dark Sky documentation, 5 00:00:15,030 --> 00:00:19,740 it says that the icon data point can have one of ten possible values. 6 00:00:19,740 --> 00:00:23,340 And those are returned as string representations. 7 00:00:23,340 --> 00:00:28,810 Our user interface mark-up, calls for the displaying of icons, not a string. 8 00:00:28,810 --> 00:00:32,540 Therefore, we need to map the string values from our JSON data, 9 00:00:32,540 --> 00:00:34,730 to relevant images for our users. 10 00:00:35,880 --> 00:00:40,540 Our amazing design team here at TreeHouse has created a great set of icons for 11 00:00:40,540 --> 00:00:42,630 our app, which we'll use up here. 12 00:00:44,050 --> 00:00:46,512 There's a link to download them in the teacher's notes. 13 00:00:51,495 --> 00:00:56,090 Once we have them unzipped, we can copy them directly into the project folder. 14 00:00:56,090 --> 00:00:59,265 They're formatted to allow us to overwrite the existing folders. 15 00:01:04,511 --> 00:01:07,835 To quickly open the res folder, right click on it. 16 00:01:10,980 --> 00:01:15,508 On Mac, you'll select this Reveal in Finder, on Windows it shows in Explorer. 17 00:01:18,177 --> 00:01:23,017 And now, we can simply drag all the drawable folders, to our project. 18 00:01:30,484 --> 00:01:32,357 And we'll take the mipmap folders, as well. 19 00:01:43,239 --> 00:01:46,699 Remember, we have multiple versions of each icon to account for 20 00:01:46,699 --> 00:01:48,940 different screen resolutions. 21 00:01:48,940 --> 00:01:53,020 With all of our images here in our drawable folder, we can expand one, and 22 00:01:53,020 --> 00:01:54,616 see all the different versions. 23 00:01:54,616 --> 00:01:57,770 If we double-click on one to preview it, 24 00:01:57,770 --> 00:02:01,240 we see that it is white on a transparent background. 25 00:02:01,240 --> 00:02:04,520 So the background we use in our layout, will show through. 26 00:02:04,520 --> 00:02:06,620 We'll talk more about that here in a bit. 27 00:02:06,620 --> 00:02:10,090 For now though, we want to add a new method to our model that will get 28 00:02:10,090 --> 00:02:15,010 the appropriate image, based on the icon value we are getting from our API. 29 00:02:15,010 --> 00:02:20,820 To get the image, we need the int ID that gets generated for the drawable resource. 30 00:02:20,820 --> 00:02:25,258 Once we set that up, it'll be nice and easy to use it in an image view. 31 00:02:25,258 --> 00:02:30,190 In CurrentWeather then, let's add a new method after our icons, 32 00:02:30,190 --> 00:02:33,106 after the icon Getter and Setter here. 33 00:02:37,661 --> 00:02:41,948 We'll make it public, return an int value, call it getIconId. 34 00:02:45,621 --> 00:02:49,460 Inside here, we're going to have a long switch statement to check for 35 00:02:49,460 --> 00:02:51,830 all the possible values. 36 00:02:51,830 --> 00:02:54,720 We'll need to account for all the values, so let's take 37 00:02:54,720 --> 00:02:59,020 another look at our API documentation to see what options we're working with. 38 00:03:00,730 --> 00:03:05,490 So again, we'll look at Response Format, down here under the currently data point. 39 00:03:07,350 --> 00:03:08,490 We're looking for icon. 40 00:03:10,910 --> 00:03:14,370 Here we see a list of all of our potential values. 41 00:03:14,370 --> 00:03:18,350 In situations like this, I like to copy and paste all of these values, and 42 00:03:18,350 --> 00:03:20,940 then add them as a comment in our code. 43 00:03:20,940 --> 00:03:29,497 So we'll copy, And we can paste them here into our getIconId method. 44 00:03:33,032 --> 00:03:35,540 And let's clean that up a little bit. 45 00:03:35,540 --> 00:03:36,040 There we go. 46 00:03:37,990 --> 00:03:42,730 So let's start here with a new int variable, and set it to a default value. 47 00:03:42,730 --> 00:03:45,324 We'll set it to the clear day icon by default. 48 00:03:45,324 --> 00:03:48,547 So int iconId 49 00:03:48,547 --> 00:03:55,875 R.drawable.clear_day. 50 00:03:59,140 --> 00:04:04,580 This will be useful should any new icons get added to the API in the future. 51 00:04:04,580 --> 00:04:06,490 They mentioned this in their documentation. 52 00:04:07,720 --> 00:04:12,080 Now for that long switch statement, I mentioned, to update icon ID based on 53 00:04:12,080 --> 00:04:16,930 the icon, we could use a bunch of if statements here, but 54 00:04:16,930 --> 00:04:19,270 the switch construct is a bit faster. 55 00:04:19,270 --> 00:04:21,620 And in my opinion, easier to read. 56 00:04:22,910 --> 00:04:25,490 So we'll switch based on the icon. 57 00:04:28,610 --> 00:04:32,030 Every possibility we want to catch, we define. 58 00:04:32,030 --> 00:04:33,510 So we'll start with clear day. 59 00:04:34,700 --> 00:04:36,758 So case of clear day. 60 00:04:41,830 --> 00:04:49,020 Our iconId, drawable.clear_day. 61 00:04:50,450 --> 00:04:54,800 Notice that the drawable names have underscores instead of dashes. 62 00:04:54,800 --> 00:04:57,830 We are getting dashes from the API, but 63 00:04:57,830 --> 00:05:02,050 we aren't allowed to have dashes in resource names and Android projects. 64 00:05:02,050 --> 00:05:04,850 Hence, this little conversion dance here. 65 00:05:04,850 --> 00:05:08,410 Now this next line of code is short but very important. 66 00:05:08,410 --> 00:05:12,540 We need to break the case, or it will fall through to the next case. 67 00:05:12,540 --> 00:05:18,640 The fall through allows one to write code, that runs, that meets multiple cases. 68 00:05:18,640 --> 00:05:21,650 In this situation, however, we don't want that. 69 00:05:21,650 --> 00:05:24,580 We will always need to call the keyword break, 70 00:05:24,580 --> 00:05:26,590 which will break out of our try clause. 71 00:05:27,710 --> 00:05:32,911 Break, our next case is for clear night. 72 00:05:36,450 --> 00:05:40,502 We'll set iconId R.drawable. 73 00:05:44,315 --> 00:05:45,500 Clear_night. 74 00:05:45,500 --> 00:05:46,290 And don't forget to break. 75 00:05:47,680 --> 00:05:52,070 There's a bunch of additional cases here, and it's kind of a lot to type. 76 00:05:52,070 --> 00:05:55,404 I'll put the rest in the teachers notes for easy copy and paste, 77 00:05:55,404 --> 00:05:57,213 and I'll just paste them in here. 78 00:06:00,875 --> 00:06:02,335 We'll remove this space. 79 00:06:04,956 --> 00:06:12,354 And then, we can return our IconId Very nice. 80 00:06:12,354 --> 00:06:16,782 [SOUND] We are finally done with the data model, and 81 00:06:16,782 --> 00:06:22,382 are ready to display all this data we've worked so hard to get. 82 00:06:22,382 --> 00:06:25,968 In the next section, we'll get the user interface, or 83 00:06:25,968 --> 00:06:29,110 UI, all set up to connect our data to the screen.