Skip to content

Instantly share code, notes, and snippets.

@Shimmen
Created October 5, 2016 16:10
Show Gist options
  • Select an option

  • Save Shimmen/096c98b4cd0ec09dfd36fae769ca82a1 to your computer and use it in GitHub Desktop.

Select an option

Save Shimmen/096c98b4cd0ec09dfd36fae769ca82a1 to your computer and use it in GitHub Desktop.
package teamtim.teamtimapp.activities;
import android.app.Activity;
import android.content.Intent;
import java.util.List;
import teamtim.teamtimapp.database.DatabaseInterface;
import teamtim.teamtimapp.database.MockDatabase;
import teamtim.teamtimapp.database.WordQuestion;
class Result {
// Result data ...
}
public class PlayGameActivity {
private static PlayGameActivity current;
public static PlayGameActivity getCurrent() {
return current;
}
interface OnResultCallback {
void onResult(Result result);
}
OnResultCallback onResultCallback;
// (onCreate)
public PlayGameActivity() {
current = this;
}
public void setOnResult(OnResultCallback resultCallback) {
this.onResultCallback = resultCallback;
}
public void setWordQuestion(WordQuestion q) {
// ...
}
// Called on user input by Android
public void userSubmittedAnswer() {
// Calculate points etc.
Result r = new Result();
// Call the callback with the result
onResultCallback.onResult(r);
}
}
class SPC implements PlayGameActivity.OnResultCallback {
List<WordQuestion> wordQuestions;
int currentQuestionIndex = 0;
public void startGame(Activity root, String selectedCategory) {
// Fetch questions
wordQuestions = MockDatabase.getInstance().getQuestions(selectedCategory, 10);
// Create play game activity (in its onCreate, save the instance)
root.startActivity(new Intent());
PlayGameActivity current = PlayGameActivity.getCurrent();
current.setOnResult(this);
// Set the first word question
current.setWordQuestion(wordQuestions.get(currentQuestionIndex));
}
@Override
public void onResult(Result result) {
// Handle result...
PlayGameActivity current = PlayGameActivity.getCurrent();
// Load next question if possible
currentQuestionIndex++;
if (currentQuestionIndex < wordQuestions.size()) {
WordQuestion next = wordQuestions.get(currentQuestionIndex);
current.setWordQuestion(next);
} else {
// Load the end game activity
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment