Advertisement
yo2man

Adding new data model to the object and finishing the model

Jul 11th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.83 KB | None | 0 0
  1. f// Adding A New Data Model To The Object And Finishing The Data Model
  2.  
  3. // The response we get from the Forecast API give us a list of daily forecasts for
  4. // the next week, just what we need.
  5. // We're all ready getting this data in the JSON response, but
  6. // we haven't accessed it yet.
  7. // Let's start by adding this new data to our data model.
  8.  
  9. // in the JSON we used the "currently" datas, there are also minutely, hourly, daily. We only need a few data from each: https://api.forecast.io/forecast/d5faf0cb201f103df4dde0b8b0a4f490/37.8267,-122.423
  10. // if we wanted to know more about the data and what each represents, go to the API Docs: https://developer.forecast.io/docs/v2
  11.  
  12. /*
  13. Okay, let's go back to the actual JSON response, and
  14. let's start by taking a look at this hourly forecast.
  15. So inside, we have an array of JSON objects, and
  16. they're accessed by the key named Data.
  17. This makes for a nice and direct translation.
  18. We will use an array of Java objects in our data model.
  19. Each object will be an our item that will correspond to a data item in here.
  20. Our simplified model will hold the time, a short summary of the weather conditions,
  21. the temperature which we see here, and the weather icon.
  22.  
  23. Following the MVC pattern, our data model is made up of classes that represent the data we get from Forecast.api. In the previous project we had CurrentWeather. We will now create a few more classes for each.
  24.  
  25. */
  26.  
  27. // Let's tackle the first class: We'll create class to hold the data for each hour, and then we'll create an array of these hour items to hold the whole hourly forecast array.
  28.  
  29.  
  30. Hour.java
  31.  
  32. package com.teamtreehouse.stormy.weather;
  33.  
  34.  
  35. public class Hour {
  36.     private long mTime; // use 'long' to hold the Time //use private to keep it in the class
  37.     private String mSummary; //use 'String' to hold the summary, etc. simple.
  38.     private double mTemperature;
  39.     private String mIcon; // 'String' to hold the icon
  40.     private String mTimezone; //need the timezone to display the hour properly
  41.  
  42.     // Step 2. code>generate>getters and setters> done. simple
  43.  
  44.     public String getIcon() {
  45.         return mIcon;
  46.     }
  47.  
  48.     public void setIcon(String icon) {
  49.         mIcon = icon;
  50.     }
  51.  
  52.     public String getSummary() {
  53.         return mSummary;
  54.     }
  55.  
  56.     public void setSummary(String summary) {
  57.         mSummary = summary;
  58.     }
  59.  
  60.     public double getTemperature() {
  61.         return mTemperature;
  62.     }
  63.  
  64.     public void setTemperature(double temperature) {
  65.         mTemperature = temperature;
  66.     }
  67.  
  68.     public long getTime() {
  69.         return mTime;
  70.     }
  71.  
  72.     public void setTime(long time) {
  73.         mTime = time;
  74.     }
  75.  
  76.     public String getTimezone() {
  77.         return mTimezone;
  78.     }
  79.  
  80.     public void setTimezone(String timezone) {
  81.         mTimezone = timezone;
  82.     }
  83. }
  84.  
  85.  
  86. //------------------------------------------------------------------------------------
  87. // Like we did for hour, do it for daily JSOn data. Create a class and create an array for those items.
  88. Day.java
  89.  
  90. package com.teamtreehouse.stormy.weather;
  91.  
  92. public class Day {
  93.     private long mTime;
  94.     private String mSummary;
  95.     private double mTemperatureMax; //max temp of the day
  96.     private String mIcon;
  97.     private String mTimeZone;
  98.  
  99.  
  100.     public String getIcon() {
  101.         return mIcon;
  102.     }
  103.  
  104.     public void setIcon(String icon) {
  105.         mIcon = icon;
  106.     }
  107.  
  108.     public String getSummary() {
  109.         return mSummary;
  110.     }
  111.  
  112.     public void setSummary(String summary) {
  113.         mSummary = summary;
  114.     }
  115.  
  116.     public double getTemperatureMax() {
  117.         return mTemperatureMax;
  118.     }
  119.  
  120.     public void setTemperatureMax(double temperatureMax) {
  121.         mTemperatureMax = temperatureMax;
  122.     }
  123.  
  124.     public long getTime() {
  125.         return mTime;
  126.     }
  127.  
  128.     public void setTime(long time) {
  129.         mTime = time;
  130.     }
  131.  
  132.     public String getTimeZone() {
  133.         return mTimeZone;
  134.     }
  135.  
  136.     public void setTimeZone(String timeZone) {
  137.         mTimeZone = timeZone;
  138.     }
  139.  
  140. }
  141.  
  142.  
  143. //-------------------------------------------------------------------------------------
  144. /*
  145. So now we have three different model classes; current weather, day, and hour.
  146. But we need a way to group them all together.
  147. We don't yet have arrays to hold the hourly and daily forecast.
  148. Our solution is to again model our Java classes after the data in the API.
  149. We are getting data as a response from a forecast server and
  150. it's all bundled together in one big JSON object.
  151. Let's create our own object to bundle up our own model classes.
  152. We'll call it forecast and it will have current weather object,
  153. an array of hour items and an array of day items.
  154. */
  155.  
  156.  
  157. Forecast.java
  158.  
  159. package com.teamtreehouse.stormy.weather;
  160.  
  161. public class Forecast {
  162.     private Current mCurrent;
  163.     private Hour[] mHourlyForecast;
  164.     private Day[] mDailyForecast;
  165.  
  166.     public Current getCurrent() {
  167.         return mCurrent;
  168.     }
  169.  
  170.     public void setCurrent(Current current) {
  171.         mCurrent = current;
  172.     }
  173.  
  174.     public Day[] getDailyForecast() {
  175.         return mDailyForecast;
  176.     }
  177.  
  178.     public void setDailyForecast(Day[] dailyForecast) {
  179.         mDailyForecast = dailyForecast;
  180.     }
  181.  
  182.     public Hour[] getHourlyForecast() {
  183.         return mHourlyForecast;
  184.     }
  185.  
  186.     public void setHourlyForecast(Hour[] hourlyForecast) {
  187.         mHourlyForecast = hourlyForecast;
  188.     }
  189. }
  190.  
  191.  
  192. //------------------------------------------------------------------------------
  193.  
  194. //Refactor Renamed Current Weather to Current!
  195.  
  196.  
  197. package com.teamtreehouse.stormy.weather;
  198.  
  199. import com.teamtreehouse.stormy.R;
  200.  
  201. import java.text.SimpleDateFormat;
  202. import java.util.Date;
  203. import java.util.TimeZone;
  204.  
  205. public class Current {
  206.  
  207.     private String mIcon; // The Icon // it comes from Forecast API as a string, we convert it to an int
  208.     private long mTime; //Time
  209.     private double mTemperature; //Temp
  210.     private double mHumidity; //humidity
  211.     private double mPrecipChance; //chance of precipitation
  212.     private String mSummary; //summary at the bottom
  213.  
  214.     private String mTimeZone;
  215.  
  216.     public String getTimeZone() {
  217.         return mTimeZone;
  218.     }
  219.  
  220.     public void setTimeZone(String timeZone) {
  221.         mTimeZone = timeZone;
  222.     }
  223.  
  224.     // Step 2: Generate getters and setters for everything: Code>Generate...>Getters and Setters:
  225.     public double getHumidity() {
  226.         return mHumidity;
  227.     }
  228.  
  229.     public void setHumidity(double humidity) {
  230.         mHumidity = humidity;
  231.     }
  232.  
  233.     public String getIcon() {
  234.         return mIcon;
  235.     }
  236.     // now we want to add a new method to our model that gets the appropriate image
  237.     // based on the icon value we are getting from the forecast API.
  238.     // To get the image, we need to int ID that gets generated for each drawable resource.
  239.  
  240.     public int getIconId(){
  241.         // clear-day, clear-night, rain, snow, sleet, wind, fog, cloudy, partly-cloudy-day, or partly-cloudy-night
  242.         int iconId = R.drawable.clear_day; //set this one in default, forecast api documentation says to "set a sensible icon as default"
  243.  
  244.         if(mIcon.equals("clear-day")){
  245.             iconId = R.drawable.clear_day; //notice that the ids have _ compared to string "-" because we arent allowed to have dashes as resource names in Android Studio
  246.         }
  247.         else if (mIcon.equals("clear-night")){
  248.             iconId = R.drawable.clear_night;
  249.         }
  250.         else if (mIcon.equals("rain")) {
  251.             iconId = R.drawable.rain;
  252.         }
  253.         else if (mIcon.equals("snow")) {
  254.             iconId = R.drawable.snow;
  255.         }
  256.         else if (mIcon.equals("sleet")) {
  257.             iconId = R.drawable.sleet;
  258.         }
  259.         else if (mIcon.equals("wind")) {
  260.             iconId = R.drawable.wind;
  261.         }
  262.         else if (mIcon.equals("fog")) {
  263.             iconId = R.drawable.fog;
  264.         }
  265.         else if (mIcon.equals("cloudy")) {
  266.             iconId = R.drawable.cloudy;
  267.         }
  268.         else if (mIcon.equals("partly-cloudy-day")) {
  269.             iconId = R.drawable.partly_cloudy;
  270.         }
  271.         else if (mIcon.equals("partly-cloudy-night")) {
  272.             iconId = R.drawable.cloudy_night;
  273.         }
  274.  
  275.         return iconId;
  276.     }
  277.  
  278.     public void setIcon(String icon) {
  279.         mIcon = icon;
  280.     }
  281.  
  282.     public int getPrecipChance() {
  283.         double precipPercentage = mPrecipChance * 100; //like what we did with mTemperature
  284.         return (int)Math.round(precipPercentage);
  285.     }
  286.  
  287.     public void setPrecipChance(double precipChance) {
  288.         mPrecipChance = precipChance;
  289.     }
  290.  
  291.     public String getSummary() {
  292.         return mSummary;
  293.     }
  294.  
  295.     public void setSummary(String summary) {
  296.         mSummary = summary;
  297.     }
  298.  
  299.     public int getTemperature() {
  300.         return (int)Math.round(mTemperature); //return rounded decimal places of temperature that is cast to an int
  301.     }
  302.  
  303.     public void setTemperature(double temperature) {
  304.         mTemperature = temperature;
  305.     }
  306.  
  307.     public long getTime() {
  308.         return mTime;
  309.     }
  310.  
  311.     public String getFormattedTime() {
  312.         SimpleDateFormat formatter = new SimpleDateFormat("h:mm, a"); // this special Java called SimpleDateFormat converts unix date format to locale "normal" format
  313.         formatter.setTimeZone(TimeZone.getTimeZone(getTimeZone())); //set time zone, logging it from MainActivity
  314.  
  315.         Date dateTime = new Date(getTime() * 1000);
  316.         String timeString = formatter.format(dateTime);
  317.  
  318.         return timeString;
  319.     }
  320.  
  321.     public void setTime(long time) {
  322.         mTime = time;
  323.     }
  324.  
  325. }
  326.  
  327. //-----------------------------------------------------------------------------
  328. // created a new subpackage under com.treehouse.stormy called "weather" to organize things better. Move current, day, forecast, hour classes into it.
  329. // then do pretty much the same thing for MainActivity and Alert Dialog Fragment class. simple.
  330.  
  331. //But I do want to point out one thing, whenever you are moving activities into
  332. // a different package, you might want to check the Android manifest file.
  333. // That's up here in the manifest's folder, so I'll double click on that.
  334. // And sometimes when we move activities from one package to another,
  335. // it causes an error in here, because the Android name attribute for
  336. // each activity element may not be updated.
  337. // It should be fine, like it is here, you see that it now has the UI package.
  338. // But if your app crashes after refactoring packages for
  339. // activities you may need to track down the error in here. *
  340.  
  341. <?xml version="1.0" encoding="utf-8"?>
  342. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  343.     package="com.teamtreehouse.stormy" >
  344.  
  345.     <!--add this. It solves the MainNetwork Permission error by requesting permission to use the internet -->
  346.     <uses-permission android:name="android.permission.INTERNET"/>
  347.     <!--permission for getActiveNetwork method -->
  348.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  349.  
  350.     <application
  351.         android:allowBackup="true"
  352.         android:icon="@mipmap/ic_launcher"
  353.         android:label="@string/app_name"
  354.         android:theme="@style/AppTheme" >
  355.         <activity
  356.             android:name=".UI.MainActivity"     <----check this*
  357.             android:label="@string/app_name"
  358.             android:screenOrientation="portrait">
  359.             <intent-filter>
  360.                 <action android:name="android.intent.action.MAIN" />
  361.  
  362.                 <category android:name="android.intent.category.LAUNCHER" />
  363.             </intent-filter>
  364.         </activity>
  365.     </application>
  366.  
  367. </manifest>
  368.  
  369. //-----------------------------------------------------------------------------
  370. // https://teamtreehouse.com/library/android-lists-and-adapters/updating-the-data-model/adding-a-new-data-model-object
  371. // https://teamtreehouse.com/library/android-lists-and-adapters/updating-the-data-model/finishing-the-data-model
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement