Skip to content

Commit

Permalink
Added Chapter Selection Activity. Join does not work yet.
Browse files Browse the repository at this point in the history
  • Loading branch information
steenburgh committed Mar 13, 2016
1 parent 7ffb35b commit fa4907e
Show file tree
Hide file tree
Showing 14 changed files with 303 additions and 65 deletions.
8 changes: 6 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@
android:label="@string/title_activity_login" />
<activity
android:name=".ReportTagActivity"
android:label="@string/title_activity_report_tag"></activity>
android:label="@string/title_activity_report_tag"/>

<activity android:name=".ChapterSelectionActivity"
android:label="@string/select_a_chapter"/>

<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Expand All @@ -51,7 +55,7 @@

<activity
android:name=".HeatMapActivity"
android:label="@string/title_activity_heat_map"></activity>
android:label="@string/title_activity_heat_map"/>
</application>

</manifest>
232 changes: 232 additions & 0 deletions app/src/main/java/com/hvzhub/app/ChapterSelectionActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
package com.hvzhub.app;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
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;
import android.widget.ArrayAdapter;
import android.widget.ListView;
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;
import com.hvzhub.app.API.model.Uuid;

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

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

public class ChapterSelectionActivity extends AppCompatActivity {

ListView listView;
ArrayAdapter<Chapter> adapter;
List<Chapter> chapterList;

ProgressBar progressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chapter_selection);

listView = (ListView) findViewById(R.id.list_view);
chapterList = new ArrayList<>();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, chapterList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO: Remove this
Intent i = new Intent(ChapterSelectionActivity.this, GameActivity.class);
startActivity(i);
finish();

// TODO: Fix this method
//joinChapter(chapterList.get(position));
}
});

progressBar = (ProgressBar) findViewById(R.id.progress);

// Show the close button
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close_white);
}

getChapterList();
}

private void getChapterList() {
if (!NetworkUtils.networkIsAvailable(this)) {
AlertDialog.Builder b = new AlertDialog.Builder(ChapterSelectionActivity.this);
b.setTitle(getString(R.string.network_not_available))
.setMessage(getString(R.string.network_not_available_hint))
.setPositiveButton(R.string.retry, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getChapterList();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.show();
} else {
showProgress(true);
HvZHubClient client = API.getInstance(getApplicationContext()).getHvZHubClient();

String uuid = getSharedPreferences(API.PREFS_API, MODE_PRIVATE).getString(API.PREFS_SESSION_ID, null);
Call<ChapterListContainer> call = client.chapters(new Uuid(uuid));
call.enqueue(new Callback<ChapterListContainer>() {
@Override
public void onResponse(Call<ChapterListContainer> call, Response<ChapterListContainer> response) {
showProgress(false);
if (response.isSuccessful()) {
chapterList.clear();
chapterList.addAll(response.body().chapters);
} else {
AlertDialog.Builder b = new AlertDialog.Builder(ChapterSelectionActivity.this);
b.setTitle(getString(R.string.unexpected_response))
.setMessage(getString(R.string.unexpected_response_hint))
.setPositiveButton(R.string.retry, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getChapterList();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.show();
}
}

@Override
public void onFailure(Call<ChapterListContainer> call, Throwable t) {
showProgress(false);

AlertDialog.Builder b = new AlertDialog.Builder(ChapterSelectionActivity.this);
b.setTitle(getString(R.string.generic_connection_error))
.setMessage(getString(R.string.generic_connection_error_hint))
.setPositiveButton(R.string.retry, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getChapterList();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.show();
}
});

}
}

private void joinChapter(Chapter chapter) {
showProgress(true);
HvZHubClient client = API.getInstance(getApplicationContext()).getHvZHubClient();
String uuid = getSharedPreferences(API.PREFS_API, MODE_PRIVATE).getString(API.PREFS_SESSION_ID, null);
Call<Status> call = client.join(chapter.getUrl(), new Uuid(uuid));
call.enqueue(new Callback<Status>() {
@Override
public void onResponse(Call<Status> call, Response<Status> response) {
showProgress(false);
// TODO: Do something
}

@Override
public void onFailure(Call<Status> call, Throwable t) {
showProgress(false);
// TODO: Do something
}
});
}

/**
* Shows the progress UI and hides the login form.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private void showProgress(final boolean show) {
// On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
// for very easy animations. If available, use these APIs to fade-in
// the progress spinner.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);

listView.setVisibility(show ? View.GONE : View.VISIBLE);
listView.animate().setDuration(shortAnimTime).alpha(
show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
listView.setVisibility(show ? View.GONE : View.VISIBLE);
}
});

progressBar.setVisibility(show ? View.VISIBLE : View.GONE);
progressBar.animate().setDuration(shortAnimTime).alpha(
show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
progressBar.setVisibility(show ? View.VISIBLE : View.GONE);
}
});
} else {
// The ViewPropertyAnimator APIs are not available, so simply show
// and hide the relevant UI components.
progressBar.setVisibility(show ? View.VISIBLE : View.GONE);
listView.setVisibility(show ? View.GONE : View.VISIBLE);
}
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
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);
}
// Else, close this activity
finish();
return true;
default:
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
return super.onOptionsItemSelected(item);
}
}
}
75 changes: 20 additions & 55 deletions app/src/main/java/com/hvzhub/app/LoginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;

Expand All @@ -28,14 +26,11 @@
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.Chapter;
import com.hvzhub.app.API.model.ChapterListContainer;
import com.hvzhub.app.API.model.Login.LoginRequest;
import com.hvzhub.app.API.model.Login.Session;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
Expand Down Expand Up @@ -81,7 +76,7 @@ public void onClick(View view) {
});

mLoginFormView = findViewById(R.id.login_form);
mProgressView = findViewById(R.id.login_progress);
mProgressView = findViewById(R.id.progress);

// TODO: Remove this
Button cheatButton = (Button) findViewById(R.id.cheat_button);
Expand All @@ -95,44 +90,6 @@ public void onClick(View v) {

}


// TODO: Use this
private void getChapterList() {
HvZHubClient client = API.getInstance(getApplicationContext()).getHvZHubClient();
Call<ChapterListContainer> call = client.chapters();
call.enqueue(new Callback<ChapterListContainer>() {
@Override
public void onResponse(Call<ChapterListContainer> call, Response<ChapterListContainer> response) {
if (response.isSuccessful()) {
Log.d("HTTP_GET_RESPONSE", response.raw().toString());
ChapterListContainer chapterContainer = response.body();
List<Chapter> chapters = chapterContainer.chapters;

for (Chapter chapter : chapters) {
Log.i("GET Chapters", "Id: " + chapter.id + " Name: " + chapter.name);
}
} else {
Log.d("Error", "Response was unsuccessful");
}

}

@Override
public void onFailure(Call<ChapterListContainer> call, Throwable t) {
Log.d("Error", t.getMessage());
}
});
}



public boolean networkIsAvailable() {
ConnectivityManager connectivityManager =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

/**
* Attempts to sign in or register the account specified by the login form.
* If there are form errors (invalid email, missing fields, etc.), the
Expand Down Expand Up @@ -209,8 +166,16 @@ public void onResponse(Call<Session> call, Response<Session> response) {
if (response.isSuccessful()) {
// Login successful. Start the rest of the app
finish();
Intent intent = new Intent(LoginActivity.this, GameActivity.class);
startActivity(intent);

String chapterUrl = getSharedPreferences(API.PREFS_API, MODE_PRIVATE).getString(API.PREFS_CHAPTER_URL, null);
if (chapterUrl == null) {
Intent intent = new Intent(LoginActivity.this, ChapterSelectionActivity.class);
startActivity(intent);
} else {
Intent intent = new Intent(LoginActivity.this, GameActivity.class);
startActivity(intent);
}


// Persist the uuid in sharedPrefs
SharedPreferences.Editor prefs = getSharedPreferences(API.PREFS_API, Context.MODE_PRIVATE).edit();
Expand Down Expand Up @@ -252,14 +217,14 @@ public void onFailure(Call<Session> call, Throwable t) {
showProgress(false);
AlertDialog.Builder b = new AlertDialog.Builder(LoginActivity.this);
b.setTitle(getString(R.string.generic_connection_error))
.setMessage(getString(R.string.generic_connection_error_hint))
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
})
.show();
.setMessage(getString(R.string.generic_connection_error_hint))
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
})
.show();
Log.d("Error", t.getMessage());
}
});
Expand Down
Loading

0 comments on commit fa4907e

Please sign in to comment.