Skip to content

Commit

Permalink
Implemented 'My Code' view, Closes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
steenburgh committed Mar 13, 2016
1 parent 1f5ea5d commit 434479f
Show file tree
Hide file tree
Showing 20 changed files with 284 additions and 33 deletions.
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
<activity
android:name=".GameActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" />
android:theme="@style/AppTheme.NoActionBar"
android:configChanges="orientation|screenSize" />
<activity
android:name=".LoginActivity"
android:label="@string/title_activity_login" />
Expand Down
38 changes: 28 additions & 10 deletions app/src/main/java/com/hvzhub/app/API/API.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.hvzhub.app.API;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.annotation.NonNull;
import android.widget.Toast;

import com.hvzhub.app.API.model.Login.Session;
import com.hvzhub.app.LoginActivity;
import com.hvzhub.app.R;

/**
* A wrapper for the JSON API
Expand All @@ -19,14 +22,9 @@ public class API {
public static final String PREFS_API = "prefs_api"; // SharedPreferences file for API
public static final String PREFS_SESSION_ID = "sessionID";
public static final String PREFS_CHAPTER_URL = "chapterUrl";
public static final String PREFS_GAME_ID = "gameID";

private static final String BASE_PATH = "http://10.0.2.2:8080/api/v1/";
private static final String LOGIN = "login";
private static final String FIELD_PASSWORD = "password";
private static final String FIELD_EMAIL = "email";
private static final String FIELD_SESSION_ID = "uuid";

private String sessionID = null;

/* This is implemented as a 'singleton'
* This means that API is an object that can only be instantiated once
Expand All @@ -51,9 +49,29 @@ public HvZHubClient getHvZHubClient() {
return mHvZHubClient;
}

public void logout() {
/**
* Logout, and close the provided activity
*
* @param activity The activity calling logout.
@ @param closeActivity whether or not to close the activity after logging out
*/
public void logout(Activity activity, boolean closeActivity) {
SharedPreferences.Editor prefs = mCtx.getSharedPreferences(API.PREFS_API, Context.MODE_PRIVATE).edit();
prefs.putString(API.PREFS_SESSION_ID, null);
prefs.apply();
Toast t = Toast.makeText(
mCtx,
R.string.you_have_been_logged_out,
Toast.LENGTH_LONG
);
t.show();

if (closeActivity) {
activity.finish();
}
Intent i = new Intent(activity, LoginActivity.class);
activity.startActivity(i);


}
}
13 changes: 10 additions & 3 deletions app/src/main/java/com/hvzhub/app/API/HvZHubClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.hvzhub.app.API;

import com.hvzhub.app.API.model.Chapters.ChapterListContainer;
import com.hvzhub.app.API.model.Code;
import com.hvzhub.app.API.model.Login.LoginRequest;
import com.hvzhub.app.API.model.Login.Session;
import com.hvzhub.app.API.model.Status;
Expand All @@ -12,15 +13,21 @@
import retrofit2.http.Path;

public interface HvZHubClient {
@POST("login")
Call<Session> login(@Body LoginRequest loginRequest);

@POST("chapters")
Call<ChapterListContainer> chapters(@Body Uuid uuid);

@POST("/chapters/{chapter_url}/join")
@POST("chapters/{chapter_url}/join")
Call<Status> join(
@Path("chapter_url") String chapterUrl,
@Body Uuid uuid
);

@POST("login")
Call<Session> login(@Body LoginRequest loginRequest);
@POST("games/{id}/my_code")
Call<Code> getMyCode(
@Path("id") int id,
@Body Uuid uuid
);
}
5 changes: 5 additions & 0 deletions app/src/main/java/com/hvzhub/app/API/model/Code.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.hvzhub.app.API.model;

public class Code {
public String code;
}
21 changes: 13 additions & 8 deletions app/src/main/java/com/hvzhub/app/ChapterSelectionActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
Expand All @@ -18,10 +19,8 @@
import android.widget.ProgressBar;

import com.hvzhub.app.API.API;
import com.hvzhub.app.API.ErrorUtils;
import com.hvzhub.app.API.HvZHubClient;
import com.hvzhub.app.API.NetworkUtils;
import com.hvzhub.app.API.model.APIError;
import com.hvzhub.app.API.model.Chapters.Chapter;
import com.hvzhub.app.API.model.Chapters.ChapterListContainer;
import com.hvzhub.app.API.model.Status;
Expand Down Expand Up @@ -54,6 +53,12 @@ protected void onCreate(Bundle savedInstanceState) {
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO: Remove this
SharedPreferences.Editor prefs = getSharedPreferences(API.PREFS_API, Context.MODE_PRIVATE).edit();
prefs.putString(API.PREFS_CHAPTER_URL, "u_of_test");
prefs.putInt(API.PREFS_GAME_ID, 3);
prefs.apply();

// TODO: Remove this
Intent i = new Intent(ChapterSelectionActivity.this, GameActivity.class);
startActivity(i);
Expand Down Expand Up @@ -216,12 +221,12 @@ public boolean onOptionsItemSelected(MenuItem item) {
String chapterUrl = getSharedPreferences(API.PREFS_API, MODE_PRIVATE).getString(API.PREFS_CHAPTER_URL, null);
// If no chapter was selected, log them out and take them back to the login screen
if (chapterUrl == null) {
API.getInstance(this).logout();
Intent i = new Intent(ChapterSelectionActivity.this, LoginActivity.class);
startActivity(i);
// Logout and finish() this activity
API.getInstance(this).logout(this, true);
} else {
// Else, just close this activity
finish();
}
// Else, close this activity
finish();
return true;
default:
// If we got here, the user's action was not recognized.
Expand Down
21 changes: 13 additions & 8 deletions app/src/main/java/com/hvzhub/app/GameActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
public class GameActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener{

private ChatFragment cf;
private NewsFragment nf;
private ChatFragment chatFragment;
private NewsFragment newsFragment;
private MyCodeFragment myCodeFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -61,16 +62,16 @@ public void switchToTab(int id) {
Fragment toSwitch = null;
switch(id) {
case R.id.nav_news:
if (nf == null) {
nf = NewsFragment.newInstance(null, null);
if (newsFragment == null) {
newsFragment = NewsFragment.newInstance(null, null);
}
toSwitch = nf;
toSwitch = newsFragment;
break;
case R.id.nav_chat:
if (cf == null) {
cf = ChatFragment.newInstance(null, null);
if (chatFragment == null) {
chatFragment = ChatFragment.newInstance(null, null);
}
toSwitch = cf;
toSwitch = chatFragment;
break;
case R.id.nav_report_tag:
break;
Expand All @@ -79,6 +80,10 @@ public void switchToTab(int id) {
startActivity(i);
break;
case R.id.nav_my_code:
if (myCodeFragment == null) {
myCodeFragment = MyCodeFragment.newInstance();
}
toSwitch = myCodeFragment;
break;
case R.id.nav_settings:
break;
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/com/hvzhub/app/LoginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ public void onResponse(Call<Session> call, Response<Session> response) {

Log.i("Response", s.uuid + " : " + s.createdOn);
} else {
showProgress(false);
APIError apiError = ErrorUtils.parseError(response);
String err = apiError.error.toLowerCase();
if (err.contains("email")) {
Expand Down
148 changes: 148 additions & 0 deletions app/src/main/java/com/hvzhub/app/MyCodeFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package com.hvzhub.app;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hvzhub.app.API.API;
import com.hvzhub.app.API.ErrorUtils;
import com.hvzhub.app.API.HvZHubClient;
import com.hvzhub.app.API.NetworkUtils;
import com.hvzhub.app.API.model.APIError;
import com.hvzhub.app.API.model.Code;
import com.hvzhub.app.API.model.Uuid;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyCodeFragment extends Fragment {
TextView myCode;
ProgressBar progressBar;
LinearLayout myCodeContainer;
LinearLayout errorView;

public MyCodeFragment() {
// Required empty public constructor
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*/
// TODO: Rename and change types and number of parameters
public static MyCodeFragment newInstance() {
MyCodeFragment fragment = new MyCodeFragment();
return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getActivity().setTitle(getActivity().getString(R.string.my_code));
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_my_code, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

myCode = (TextView) view.findViewById(R.id.my_code);
myCodeContainer = (LinearLayout) view.findViewById(R.id.my_code_container);
progressBar = (ProgressBar) view.findViewById(R.id.progress_bar);
errorView = (LinearLayout) view.findViewById(R.id.error_view);
updateMyCode();
}

private void updateMyCode() {
showProgress(true);
showErrorView(false);
showContentView(false);

if (!NetworkUtils.networkIsAvailable(getActivity().getApplicationContext())) {
setError(getString(R.string.network_not_available), getString(R.string.network_not_available_hint));
showProgress(false);
showErrorView(true);
showContentView(false);
return;
}
int gameId = getActivity().getSharedPreferences(API.PREFS_API, Context.MODE_PRIVATE).getInt(API.PREFS_GAME_ID, -1);
if (gameId == -1) {
myCode.setText(R.string.empty_code);
} else {
HvZHubClient client = API.getInstance(getActivity().getApplicationContext()).getHvZHubClient();
String uuid = getActivity().getSharedPreferences(API.PREFS_API, Context.MODE_PRIVATE).getString(API.PREFS_SESSION_ID, null);
Call<Code> call = client.getMyCode(gameId, new Uuid(uuid));
call.enqueue(new Callback<Code>() {
@Override
public void onResponse(Call<Code> call, Response<Code> response) {
if (response.isSuccessful()) {
showProgress(false);
showErrorView(false);
showContentView(true);
myCode.setText(response.body().code);

} else {
APIError apiError = ErrorUtils.parseError(response);
String err = apiError.error.toLowerCase();
if (err.contains(getString(R.string.invalid_session_id))) {
// Logout and finish this activity
// Don't bother stopping the loading animation
API.getInstance(getActivity().getApplicationContext()).logout(getActivity(), true);
} else {
setError(getString(R.string.unexpected_response), getString(R.string.unexpected_response_hint));
showProgress(false);
showContentView(false);
showErrorView(true);
}

}
}

@Override
public void onFailure(Call<Code> call, Throwable t) {
setError(getString(R.string.generic_connection_error), getString(R.string.generic_connection_error_hint));
showProgress(false);
showContentView(false);
showErrorView(true);
}
});
}

}

private void showContentView(final boolean show) {
myCodeContainer.setVisibility(show ? View.VISIBLE : View.GONE);
}

private void showErrorView(final boolean show) {
errorView.setVisibility(show ? View.VISIBLE : View.GONE);
}

private void setError(String msg, String hint) {
errorView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateMyCode();
// TODO: Give feedback when the user presses the button
}
});

TextView errorMsg = (TextView) errorView.findViewById(R.id.error_msg);
errorMsg.setText(String.format("%s %s", msg, getString(R.string.tap_to_retry)));
TextView errorHint = (TextView) errorView.findViewById(R.id.error_hint);
errorHint.setText(hint);
}

private void showProgress(final boolean showProgress) {
progressBar.setVisibility(showProgress ? View.VISIBLE : View.GONE);
}
}
Binary file added app/src/main/res/drawable-hdpi/ic_refresh.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-ldpi/ic_refresh.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-mdpi/ic_refresh.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-tvdpi/ic_refresh.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xhdpi/ic_refresh.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xxhdpi/ic_refresh.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xxxhdpi/ic_refresh.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion app/src/main/res/layout/error_template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
android:layout_height="match_parent">

<ImageButton
android:id="@+id/retry_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_refresh"
android:background="?android:selectableItemBackground"
android:layout_gravity="center"/>
android:layout_gravity="center"
android:contentDescription="@string/tap_to_retry"/>

<TextView
android:id="@+id/error_msg"
Expand Down
Loading

0 comments on commit 434479f

Please sign in to comment.