Skip to content

Commit

Permalink
implement base64 decode;
Browse files Browse the repository at this point in the history
implement questions load screen;
implement questions state saving;
implement session token saving;
  • Loading branch information
dfloureiro committed Nov 20, 2017
1 parent 2d50367 commit 1f409c6
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 19 deletions.
38 changes: 38 additions & 0 deletions app/src/main/java/com/dfl/trivia/Base64Decoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.dfl.trivia;

import android.util.Base64;
import com.dfl.trivia.data.questions.Result;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
* Created by Loureiro on 18/11/2017.
*/

public class Base64Decoder {

public static List<Result> decodeResults(List<Result> results)
throws UnsupportedEncodingException {
ArrayList<Result> resultList = new ArrayList<>();
for (Result result : results) {
result.setCategory(decodeString(result.getCategory()));
result.setCorrectAnswer(decodeString(result.getCorrectAnswer()));
result.setDifficulty(decodeString(result.getDifficulty()));
result.setQuestion(decodeString(result.getQuestion()));
result.setType(decodeString(result.getType()));
ArrayList<String> incorrectAnswers = new ArrayList<>();
for (String answer : result.getIncorrectAnswers()) {
incorrectAnswers.add(decodeString(answer));
}
result.setIncorrectAnswers(incorrectAnswers);
resultList.add(result);
}
return resultList;
}

private static String decodeString(String base64) throws UnsupportedEncodingException {
byte[] data = Base64.decode(base64, Base64.DEFAULT);
return new String(data, "UTF-8");
}
}
5 changes: 4 additions & 1 deletion app/src/main/java/com/dfl/trivia/main/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.dfl.trivia.main;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import com.dfl.trivia.R;
Expand All @@ -25,6 +27,7 @@ public class MainActivity extends AppCompatActivity {
transaction.commit();
}

new MainPresenter(mainActivityFragment, new RequestFactory());
new MainPresenter(mainActivityFragment, new RequestFactory(),
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()));
}
}
13 changes: 9 additions & 4 deletions app/src/main/java/com/dfl/trivia/main/MainPresenter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dfl.trivia.main;

import android.content.SharedPreferences;
import android.util.Log;
import com.dfl.trivia.main.model.Category;
import com.dfl.trivia.networking.RequestFactory;
Expand All @@ -15,15 +16,17 @@

public class MainPresenter implements MainContract.Presenter {

private final MainContract.View view;
private MainContract.View view;
private RequestFactory requestFactory;
private CompositeDisposable compositeDisposable;
private SharedPreferences sharedPreferences;

private CompositeDisposable compositeDisposable;
private ArrayList<Category> categoryArrayList;

MainPresenter(MainContract.View view, RequestFactory requestFactory) {
MainPresenter(MainContract.View view, RequestFactory requestFactory, SharedPreferences sharedPreferences) {
this.view = view;
this.requestFactory = requestFactory;
this.sharedPreferences = sharedPreferences;

compositeDisposable = new CompositeDisposable();
categoryArrayList = new ArrayList<>();
Expand Down Expand Up @@ -81,6 +84,8 @@ public void getSessionToken() {
}

private void saveSessionToken(String sessionToken) {
Log.d("TOKEN", sessionToken);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("token", sessionToken);
editor.apply();
}
}
3 changes: 2 additions & 1 deletion app/src/main/java/com/dfl/trivia/networking/OpentdbApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ public interface OpentdbApi {

@GET("api.php") Flowable<QuestionsResponse> getQuestions(@Query("token") String token,
@Query("amount") int amount, @Query("category") String category,
@Query("difficulty") String difficulty, @Query("type") String questionType);
@Query("difficulty") String difficulty, @Query("type") String questionType,
@Query("encode") String encode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ public Flowable<QuestionsResponse> getQuestionsRequest(String token, int amount,
String difficulty, String questionType) {
return NetworkModule.newInstance()
.getOpentdbApi()
.getQuestions(token, amount, category, difficulty, questionType);
.getQuestions(token, amount, category, difficulty, questionType, "base64");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.dfl.trivia.question;

import android.app.FragmentTransaction;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import com.dfl.trivia.R;
Expand Down Expand Up @@ -30,7 +32,8 @@ public class QuestionActivity extends AppCompatActivity {
transaction.commit();
}

String token = null;
String token = PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
.getString("token", "");
int amount = getIntent().getIntExtra(AMOUNT, 10);
String categoryId = getIntent().getStringExtra(CATEGORY_ID);
String difficulty = getIntent().getStringExtra(DIFFICULTY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -26,6 +27,7 @@
*/
public class QuestionActivityFragment extends Fragment implements QuestionContract.View {

@BindView(R.id.loading_spinner) ContentLoadingProgressBar loadingProgressBar;
@BindView(R.id.categorie_title) TextView categoryTitle;
@BindView(R.id.difficulty_title) TextView difficultyTitle;
@BindView(R.id.question_text) TextView questionText;
Expand Down Expand Up @@ -131,4 +133,11 @@ public QuestionActivityFragment() {
.show();
getActivity().finish();
}

@Override public void finishLoading() {
categoryTitle.setVisibility(View.VISIBLE);
difficultyTitle.setVisibility(View.VISIBLE);
questionText.setVisibility(View.VISIBLE);
loadingProgressBar.setVisibility(View.GONE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ interface View extends BaseView<Presenter> {
void showResponse(String correctAnswer);

void showResults(int numberOfCorrectAnswers, int totalNumberOfAnswers);

void finishLoading();
}

interface Presenter extends BasePresenter<State> {
Expand Down
13 changes: 7 additions & 6 deletions app/src/main/java/com/dfl/trivia/question/QuestionPresenter.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.dfl.trivia.question;

import android.util.Log;

import com.dfl.trivia.Base64Decoder;
import com.dfl.trivia.data.questions.QuestionsResponse;
import com.dfl.trivia.data.questions.Result;
import com.dfl.trivia.networking.RequestFactory;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;

import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -56,6 +54,7 @@ public class QuestionPresenter implements QuestionContract.Presenter {
questionPosition = state.getCurrentQuestionPosition();
numberOfCorrectAnswers = state.getNumberOfCorrectAnswers();
showCurrentQuestion();
view.finishLoading();
} else {
getQuestionList();
}
Expand All @@ -74,10 +73,12 @@ private void getQuestionList() {
requestFactory.getQuestionsRequest(token, amount, categoryId, difficulty, questionType)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map(QuestionsResponse::getResults)
.subscribe(results -> {
questionsList = results;
//.map(questionsResponse -> Base64Decoder.decodeResults(questionsResponse.getResults()))
.filter(questionsResponse -> questionsResponse.getResponseCode() != 0)
.subscribe(questionsResponse -> {
questionsList = Base64Decoder.decodeResults(questionsResponse.getResults());
showCurrentQuestion();
view.finishLoading();
}, error -> Log.e("Error", error.getMessage())));
}

Expand Down
21 changes: 16 additions & 5 deletions app/src/main/res/layout/fragment_question.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,26 @@
android:id="@+id/categorie_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
tools:visibility="visible"
/>

<TextView
android:id="@+id/difficulty_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/categorie_title"
android:visibility="gone"
tools:visibility="visible"
/>

<TextView
android:id="@+id/question_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/difficulty_title"
android:visibility="gone"
tools:visibility="visible"
/>

<LinearLayout
Expand Down Expand Up @@ -69,8 +75,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/white"
android:text="Button"
android:background="@android:color/holo_orange_light"
/>

<Button
Expand All @@ -79,7 +84,6 @@
android:layout_height="match_parent"
android:layout_weight="1.0"
android:background="@android:color/holo_green_light"
android:text="Button"
/>

<Button
Expand All @@ -88,7 +92,6 @@
android:layout_height="match_parent"
android:layout_weight="1.0"
android:background="@android:color/holo_blue_light"
android:text="Button"
/>

<Button
Expand All @@ -97,7 +100,15 @@
android:layout_height="match_parent"
android:layout_weight="1.0"
android:background="@android:color/holo_red_light"
android:text="Button"
/>
</LinearLayout>

<android.support.v4.widget.ContentLoadingProgressBar
android:id="@+id/loading_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
tools:visibility="gone"
style="?android:attr/progressBarStyleLarge"
/>
</RelativeLayout>

0 comments on commit 1f409c6

Please sign in to comment.