Skip to content

Commit

Permalink
some improvements to user session management
Browse files Browse the repository at this point in the history
  • Loading branch information
github-user committed Jul 27, 2016
1 parent 12bfa0e commit ef43277
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.annimon.stream.function.Function;
import com.apptopus.progressive.Progressive;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
Expand Down Expand Up @@ -82,7 +84,7 @@ public class PokeMapsActivity extends AppCompatActivity implements GoogleApiClie

View mRootView;

View mSignin;
Button mSignin;
View mSignout;

TextView mHeadLine;
Expand Down Expand Up @@ -150,7 +152,7 @@ public void onDrawerClosed(View view) {
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle(R.string.profile);
updateProfile();
// updateProfile();
}
};

Expand All @@ -170,7 +172,7 @@ public void onDrawerOpened(View drawerView) {
NavigationView navigationView = (NavigationView) findViewById(R.id.left_drawer);
View headerLayout = navigationView.inflateHeaderView(R.layout.signed_in_layout);

mSignin = headerLayout.findViewById(R.id.sign_in);
mSignin = (Button) headerLayout.findViewById(R.id.sign_in);
mSignin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Expand Down Expand Up @@ -250,7 +252,7 @@ public void run() {
transaction.replace(R.id.main_container, mMapWrapperFragment)
.commit();

initPokemonGOApi();
initPokemonGOApi(new ProfileUpdateFunction());

}

Expand Down Expand Up @@ -362,7 +364,8 @@ private void refreshToken(){

}

private void initPokemonGOApi(){

private void initPokemonGOApi(final Function<Object, Object> callback){
final String authJson = pref.getString("auth", "");

if(TextUtils.isEmpty(authJson)){
Expand All @@ -371,6 +374,14 @@ private void initPokemonGOApi(){
}

AsyncTask at = new AsyncTask() {

@Override
protected void onPreExecute() {
super.onPreExecute();
// mSignin.setEnabled(false);
mSignin.setText(R.string.loading_);
}

@Override
protected Object doInBackground(Object[] objects) {

Expand All @@ -386,20 +397,22 @@ protected Object doInBackground(Object[] objects) {
}
}catch (Exception e){
e.printStackTrace();
final String error = e.getMessage();
runOnUiThread(new Runnable() {
@Override
public void run() {
showMessage(error);
}
});
// final String error = e.getMessage();
// runOnUiThread(new Runnable() {
// @Override
// public void run() {
// showMessage(error);
// }
// });
}
return null;
}

@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
// mSignin.setEnabled(false);
mSignin.setText(R.string.sign_in);
if(o instanceof PlayerProfile){
PlayerProfile playerProfile = (PlayerProfile)o;
Map<String, String> stats = new HashMap<>();
Expand All @@ -411,6 +424,9 @@ protected void onPostExecute(Object o) {
setProfile("", "", Collections.<String, String>emptyMap());
}

if(callback != null){
callback.apply(o);
}

}
};
Expand All @@ -419,7 +435,25 @@ protected void onPostExecute(Object o) {

@MainThread
private void login() {
startActivityForResult(new Intent(this, AuthUiActivity.class), 1);
final String authJson = pref.getString("auth", "");

if(!TextUtils.isEmpty(authJson)){
initPokemonGOApi(new ProfileUpdateFunction(){
@Override
public Object apply(Object o) {
if(o instanceof PlayerProfile){
return super.apply(o);
}else{
startActivityForResult(new Intent(PokeMapsActivity.this, AuthUiActivity.class), 1);
}
return Void.TYPE;
}
});
return;
}



}

@Override
Expand All @@ -430,7 +464,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
showMessage(R.string.loggedin_message,
Snackbar.LENGTH_LONG);
initPokemonGOApi();
initPokemonGOApi(new ProfileUpdateFunction());
return;
}
super.onActivityResult(requestCode, resultCode, data);
Expand Down Expand Up @@ -553,7 +587,7 @@ public Location requestLocation() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if("auth".equals(key)){
initPokemonGOApi();
initPokemonGOApi(new ProfileUpdateFunction());
}
}

Expand All @@ -564,6 +598,24 @@ protected void onResume() {
mTracker.send(new HitBuilders.ScreenViewBuilder().build());
}

private class ProfileUpdateFunction implements Function<Object, Object>{

@Override
@MainThread
public Object apply(Object o) {
if(o instanceof PlayerProfile){
PlayerProfile playerProfile = (PlayerProfile)o;
Map<String, String> stats = new HashMap<>();
stats.put("Level", String.valueOf(playerProfile.getStats().getLevel()));
stats.put("XP", String.valueOf(playerProfile.getStats().getExperience()));
setProfile(playerProfile.getUsername(), playerProfile.getTeam().name(), stats);
}else if(o != null){
showMessage(String.valueOf(o));
}
return Void.TYPE;
}
}

private AtomicBoolean isUpdatingProfile = new AtomicBoolean(false);
@MainThread
public void updateProfile(){
Expand Down Expand Up @@ -601,16 +653,7 @@ protected void onPostExecute(Object o) {
if(navigationView != null){
Progressive.hideProgress(navigationView);
}
if(o instanceof PlayerProfile){
PlayerProfile playerProfile = (PlayerProfile)o;
Map<String, String> stats = new HashMap<>();
stats.put("Level", String.valueOf(playerProfile.getStats().getLevel()));
stats.put("XP", String.valueOf(playerProfile.getStats().getExperience()));

setProfile(playerProfile.getUsername(), playerProfile.getTeam().name(), stats);
}else{
showMessage(String.valueOf(o));
}
new ProfileUpdateFunction().apply(o);
}
};
at.execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.MainThread;
import android.support.annotation.StringRes;
import android.support.design.widget.Snackbar;
import android.support.v4.content.SharedPreferencesCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
Expand Down Expand Up @@ -163,4 +166,25 @@ protected void onResume() {
mTracker.setScreenName("Login");
mTracker.send(new HitBuilders.ScreenViewBuilder().build());
}

@Override
protected void onStop() {
super.onStop();
if(!TextUtils.isEmpty(mUsername.getText())){
SharedPreferencesCompat.EditorCompat.getInstance().apply(
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("last_username", String.valueOf(mUsername.getText())));
}
}

@Override
protected void onStart() {
super.onStart();
if(TextUtils.isEmpty(mUsername.getText())){
String lastUserName = PreferenceManager.getDefaultSharedPreferences(this).getString("last_username", "");
if(!TextUtils.isEmpty(lastUserName)){
mUsername.setText(lastUserName);
mPassword.requestFocus();
}
}
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@
<string name="support_summary">Support the starving developer, get invite to supporter only community (and maybe some other goodies).</string>
<string name="developer">Developer</string>
<string name="developer_summary">Find me on xda-developers, the best forum in the world!</string>
<string name="loading_">Loading..</string>
</resources>

0 comments on commit ef43277

Please sign in to comment.