Skip to content

Commit

Permalink
Update for 2nd edition of book
Browse files Browse the repository at this point in the history
  • Loading branch information
darinwilson committed Sep 12, 2015
1 parent 9bfee50 commit 2d68b58
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 39 deletions.
1 change: 1 addition & 0 deletions chapter02/GeoQuiz/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.repl_history
build
vendor
tags
.DS_Store
nbproject
Expand Down
6 changes: 3 additions & 3 deletions chapter02/GeoQuiz/Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source 'https://rubygems.org'
source "https://rubygems.org"

gem 'rake'
# Add your dependencies here:
gem "rake"
gem "motion-gradle"
5 changes: 5 additions & 0 deletions chapter02/GeoQuiz/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
GEM
remote: https://rubygems.org/
specs:
motion-gradle (1.4.0)
rake (10.4.2)

PLATFORMS
ruby

DEPENDENCIES
motion-gradle
rake

BUNDLED WITH
1.10.6
7 changes: 5 additions & 2 deletions chapter02/GeoQuiz/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Motion::Project::App.setup do |app|
app.name = "GeoQuiz"
app.package = "net.darinwilson.android.geoquiz"
app.main_activity = "QuizActivity"
app.theme = "@android:style/Theme.Holo.Light"
app.development { app.archs << 'x86' } #for genymotion support
app.theme = "@style/Theme.AppCompat.Light.DarkActionBar"
app.archs = ["x86"] unless ARGV.include?("device") || ARGV.include?("release")
app.gradle do
dependency "com.android.support:appcompat-v7:22.1.0"
end
end
13 changes: 13 additions & 0 deletions chapter02/GeoQuiz/app/question.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Question
attr_accessor :text_res_id
attr_accessor :answer_true

def initialize(text_res_id, answer_true)
@text_res_id = text_res_id
@answer_true = answer_true
end

def true?
answer_true == true
end
end
49 changes: 31 additions & 18 deletions chapter02/GeoQuiz/app/quiz_activity.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class QuizActivity < Android::App::Activity
class QuizActivity < Android::Support::V7::App::AppCompatActivity
attr_accessor :true_button
attr_accessor :false_button
attr_accessor :next_button
attr_accessor :previous_button
attr_accessor :question_view
attr_reader :question_bank
attr_accessor :question_text_view
attr_accessor :question_bank
attr_accessor :current_index

Toast = Android::Widget::Toast
Expand All @@ -14,30 +14,31 @@ def onCreate(savedInstanceState)
setContentView(R::Layout::Activity_quiz)

@question_bank = [
TrueFalse.new(R::String::Question_oceans, true),
TrueFalse.new(R::String::Question_mideast, false),
TrueFalse.new(R::String::Question_africa, false),
TrueFalse.new(R::String::Question_americas, true),
TrueFalse.new(R::String::Question_asia, true),
Question.new(R::String::Question_oceans, true),
Question.new(R::String::Question_mideast, false),
Question.new(R::String::Question_africa, false),
Question.new(R::String::Question_americas, true),
Question.new(R::String::Question_asia, true),
]
@current_index = 0

@question_view = findViewById(R::Id::Question_text_view)
@question_view.onClickListener = self
update_question
@question_text_view = findViewById(R::Id::Question_text_view)
# remember that in RubyMotion setText can be written as text=
@question_text_view.text = question_bank[current_index].text_res_id

@true_button = findViewById(R::Id::True_button)
@false_button = findViewById(R::Id::False_button)
@true_button.onClickListener = @false_button.onClickListener = self

@next_button = findViewById(R::Id::Next_button)
@next_button.onClickListener = self
@previous_button = findViewById(R::Id::Previous_button)
@previous_button.onClickListener = self
@true_button.onClickListener =
@false_button.onClickListener =
@next_button.onClickListener =
@previous_button.onClickListener =
@question_text_view.onClickListener = self
end

def onClick(view)
if view == next_button || view == previous_button || view == question_view
if view == next_button || view == previous_button || view == question_text_view
self.current_index =
(view == previous_button ? current_index - 1 : current_index + 1) % question_bank.length
update_question
Expand All @@ -51,12 +52,24 @@ def onClick(view)
end
Toast.makeText(self, message_id, Toast::LENGTH_SHORT).show()
end

end

def onCreateOptionsMenu(menu)
getMenuInflater().inflate(R::Menu::Menu_quiz, menu)
true
end

def onOptionsItemSelected(item)
if (item.itemId == R::Id::Action_settings)
return true
end
super
end

private

def update_question
question_view.setText(question_bank[current_index].question)
question_text_view.text = question_bank[current_index].text_res_id
end

end
13 changes: 0 additions & 13 deletions chapter02/GeoQuiz/app/true_false.rb

This file was deleted.

3 changes: 1 addition & 2 deletions chapter02/GeoQuiz/resources/layout/activity_quiz.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
android:id="@+id/question_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
/>
android:padding="24dp" />

<LinearLayout
android:layout_width="wrap_content"
Expand Down
10 changes: 10 additions & 0 deletions chapter02/GeoQuiz/resources/menu/menu_quiz.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".QuizActivity">
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>

2 changes: 1 addition & 1 deletion chapter02/GeoQuiz/resources/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
<string name="true_button">True</string>
<string name="false_button">False</string>
<string name="next_button">Next</string>
<string name="previous_button">Previous</string>
<string name="menu_settings">Settings</string>
<string name="correct_toast">Correct!</string>
<string name="incorrect_toast">Incorrect!</string>
<string name="action_settings">Settings</string>

<string name="question_oceans">The Pacific Ocean is larger than the Atlantic Ocean.</string>
<string name="question_mideast">The Suez Canal connects the Red Sea and the Indian Ocean.</string>
Expand Down

0 comments on commit 2d68b58

Please sign in to comment.