Advertisement
yo2man

Loading the page first

Jul 5th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.98 KB | None | 0 0
  1. //In the MVC pattern, we have our layout as the view and our story classes as the model.
  2. // Let's load the story into the layout in the controller: StoryActivity.
  3. // So let's go back to our StoryActivity which is where we are hooking up our story.
  4.  
  5.  
  6. StoryActivity.java
  7.  
  8. package teamtreehouse.com.interactivestory.ui;
  9.  
  10. import android.app.Activity;
  11. import android.content.Intent;
  12. import android.graphics.drawable.Drawable;
  13. import android.os.Bundle;
  14. import android.util.Log;
  15. import android.view.View;
  16. import android.widget.Button;
  17. import android.widget.ImageView;
  18. import android.widget.TextView;
  19.  
  20. import teamtreehouse.com.interactivestory.R;
  21. import teamtreehouse.com.interactivestory.model.Page;
  22. import teamtreehouse.com.interactivestory.model.Story;
  23.  
  24.  
  25. public class StoryActivity extends Activity {
  26.  
  27.     public static final String TAG = StoryActivity.class.getSimpleName();
  28.    
  29.    
  30.     private Story mStory = new Story(); //[1] //We have that new story class (story.java), so let's create a member variable and initialize it as a Story object. [1]
  31.     private ImageView mImageView; // for the image in the story
  32.     private TextView mTextView;   // for the text in the story
  33.     private Button mChoice1;      // for the two buttons in the story
  34.     private Button mChoice2;      // for the two buttons in the story
  35.     private String mName;
  36.     private Page mCurrentPage;
  37.  
  38.     @Override
  39.     protected void onCreate(Bundle savedInstanceState) {
  40.         super.onCreate(savedInstanceState);
  41.         setContentView(R.layout.activity_story);
  42.  
  43.         Intent intent = getIntent();
  44.         mName = intent.getStringExtra(getString(R.string.key_name));
  45.  
  46.         if (mName == null) {
  47.             mName = "Friend";
  48.         }
  49.         Log.d(TAG, mName);
  50.  
  51.         mImageView = (ImageView)findViewById(R.id.storyImageView); // for the image in the story
  52.         mTextView = (TextView)findViewById(R.id.storyTextView);    // for the text in the story
  53.         mChoice1 = (Button)findViewById(R.id.choiceButton1);       // for the two buttons in the story
  54.         mChoice2 = (Button)findViewById(R.id.choiceButton2);       // for the two buttons in the story
  55.                  // now we have our variables that represent our views in the layout, and now we can manipulate these directly in code.*
  56.  
  57.         loadPage(0);
  58.     }
  59.  
  60.     // *So let's manipulate them down here in loadPage.
  61.     private void loadPage(int choice) {  //[1] Next, we want to load a page from the story both when the activity is first created and when a button is tapped on. So it sounds like we need a method because we're going to do the same thing twice.
  62.  
  63.         mCurrentPage = mStory.getPage(choice);
  64.         Drawable drawable = getResources().getDrawable(mCurrentPage.getImageId());
  65.         mImageView.setImageDrawable(drawable);
  66.  
  67.         String pageText = mCurrentPage.getText();
  68.         // Add the name if placeholder included. Won't add if no placeholder (placeholder being the name that fills %1$S )
  69.         pageText = String.format(pageText, mName);
  70.         mTextView.setText(pageText);
  71.  
  72.         if (mCurrentPage.isFinal()) {
  73.             mChoice1.setVisibility(View.INVISIBLE);
  74.             mChoice2.setText("PLAY AGAIN");
  75.             mChoice2.setOnClickListener(new View.OnClickListener() {
  76.                 @Override
  77.                 public void onClick(View v) {
  78.                     finish();
  79.                 }
  80.             });
  81.         }
  82.         else{
  83.             mChoice1.setText(mCurrentPage.getChoice1().getText());
  84.             mChoice2.setText(mCurrentPage.getChoice2().getText());
  85.  
  86.             mChoice1.setOnClickListener(new View.OnClickListener() {
  87.                 @Override
  88.                 public void onClick(View v) {
  89.                     int nextPage = mCurrentPage.getChoice1().getNextPage();
  90.                     loadPage(nextPage);
  91.                 }
  92.             });
  93.  
  94.             mChoice2.setOnClickListener(new View.OnClickListener() {
  95.                 @Override
  96.                 public void onClick(View v) {
  97.                     int nextPage = mCurrentPage.getChoice2().getNextPage();
  98.                     loadPage(nextPage);
  99.                 }
  100.             });
  101.         }
  102.     }
  103.  
  104. }
  105.  
  106. //----------------------------------------------------------------------------------------------
  107.  
  108. package teamtreehouse.com.interactivestory.model;
  109.  
  110. import teamtreehouse.com.interactivestory.R;
  111.  
  112. public class Story {
  113.     private Page[] mPages;
  114.  
  115.     public Story() {
  116.         mPages = new Page[7];
  117.  
  118.         mPages[0] = new Page(
  119.                 R.drawable.page0,
  120.                 "On your return trip from studying Saturn's rings, you hear a distress signal that seems to be coming from the surface of Mars. It's strange because there hasn't been a colony there in years. Even stranger, it's calling you by name: \"Help me, %1$s, you're my only hope.\"",
  121.     // %1$S = plug in string = percent 1, dollar S.
  122.     // percent one is the order number of the parameter.
  123.     // So we only have one parameter so it's just going to be percent one.
  124.     // If we wanted to replace more than thing, we could use different numbers here.
  125.     // And then the dollar sign s is just for a simple string.
  126.     // Simple
  127.  
  128.                 new Choice("Stop and investigate", 1),
  129.                 new Choice("Continue home to Earth", 2));
  130.  
  131.         mPages[1] = new Page(
  132.                 R.drawable.page1,
  133.                 "You deftly land your ship near where the distress signal originated. You didn't notice anything strange on your fly-by, but there is a cave in front of you. Behind you is an abandoned rover from the early 21st century.",
  134.                 new Choice("Explore the cave", 3),
  135.                 new Choice("Explore the rover", 4));
  136.  
  137.         mPages[2] = new Page(
  138.                 R.drawable.page2,
  139.                 "You continue your course to Earth. Two days later, you receive a transmission from HQ saying that they have detected some sort of anomaly on the surface of Mars near an abandoned rover. They ask you to investigate, but ultimately the decision is yours because your mission has already run much longer than planned and supplies are low.",
  140.                 new Choice("Head back to Mars to investigate", 4),
  141.                 new Choice("Continue home to Earth", 6));
  142.  
  143.         mPages[3] = new Page(
  144.                 R.drawable.page3,
  145.                 "Your EVA suit is equipped with a headlamp, which you use to navigate the cave. After searching for a while your oxygen levels are starting to get pretty low. You know you should go refill your tank, but there's a very faint light up ahead.",
  146.                 new Choice("Refill at ship and explore the rover", 4),
  147.                 new Choice("Continue towards the faint light", 5));
  148.  
  149.         mPages[4] = new Page(
  150.                 R.drawable.page4,
  151.                 "The rover is covered in dust and most of the solar panels are broken. But you are quite surprised to see the on-board system booted up and running. In fact, there is a message on the screen: \"%1$s, come to 28.543436, -81.369031.\" Those coordinates aren't far, but you don't know if your oxygen will last there and back.",
  152.                 new Choice("Explore the coordinates", 5),
  153.                 new Choice("Return to Earth", 6));
  154.  
  155.         mPages[5] = new Page(
  156.                 R.drawable.page5,
  157.                 "After a long walk slightly uphill, you end up at the top of a small crater. You look around, and are overjoyed to see your favorite android, %1$s-S1124. It had been lost on a previous mission to Mars! You take it back to your ship and fly back to Earth.");
  158.  
  159.         mPages[6] = new Page(
  160.                 R.drawable.page6,
  161.                 "You arrive home on Earth. While your mission was a success, you forever wonder what was sending that signal. Perhaps a future mission will be able to investigate...");
  162.     }
  163.  
  164.     public Page getPage(int pageNumber) {  //[1] // Pass in page number
  165.         return mPages[pageNumber];
  166.     }
  167. }
  168.  
  169.  
  170.  
  171. // https://teamtreehouse.com/library/build-an-interactive-story-app/finishing-the-user-interface/loading-the-first-page
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement