Cleaned repetitive code

This commit is contained in:
Sagi Dayan 2016-07-09 17:38:54 +03:00
parent 089e2c6966
commit 681f40d2e7
7 changed files with 41 additions and 25 deletions

View file

@ -284,6 +284,7 @@ public class GmapFragment extends Fragment implements OnMapReadyCallback {
} }
VolleyUtilSingleton.getInstance(getActivity()).post(Utils.BASE_URL + "/note/like", jsonObj, getNotesSuccessListener, Utils.genericErrorListener); VolleyUtilSingleton.getInstance(getActivity()).post(Utils.BASE_URL + "/note/like", jsonObj, getNotesSuccessListener, Utils.genericErrorListener);
mainActivity.getUser().getLiked_notes().add(note.getId()); mainActivity.getUser().getLiked_notes().add(note.getId());
mainActivity.getUser().updateUser(mainActivity);
likes.setText("Likes: "+(note.getLikes()+1)); likes.setText("Likes: "+(note.getLikes()+1));
} }
} }

View file

@ -38,11 +38,9 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
private TextView mRegisterButton; private TextView mRegisterButton;
private Button mLoginButton; private Button mLoginButton;
private final String TAG = "[TSN/LoginActivity]"; private final String TAG = "[TSN/LoginActivity]";
private final String BASE_URL = "http://thesocialnotework-api.appspot.com/api";
private final String LOGIN_PATH = "/login"; private final String LOGIN_PATH = "/login";
private LoginActivity self; //this private LoginActivity self; //this
protected LinearLayout layout; protected LinearLayout layout;
private boolean permissionsReturend = false;
@Override @Override
@ -126,8 +124,6 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
try { try {
if (!response.isNull("user")) { if (!response.isNull("user")) {
Log.e(TAG, "onLoginSuccess => user exist"); // TODO: REMOVE console Log.e(TAG, "onLoginSuccess => user exist"); // TODO: REMOVE console
SharedPreferences sharedPref = self.getSharedPreferences(MainActivity.LOCAL_DATA_TSN, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
String id, password, email, avatar, username, likedNotes = ""; String id, password, email, avatar, username, likedNotes = "";
JSONArray likedNotes_JSON; JSONArray likedNotes_JSON;
@ -143,8 +139,7 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
likedNotes += User.LIKED_NOTES_DELIMETER; likedNotes += User.LIKED_NOTES_DELIMETER;
} }
} }
editor.putString("UserData", id + User.ATTARS_DELIMETER + username + User.ATTARS_DELIMETER + password + User.ATTARS_DELIMETER + email + User.ATTARS_DELIMETER + avatar + User.ATTARS_DELIMETER + likedNotes); Utils.updateUserSharedPref(id + User.ATTARS_DELIMETER + username + User.ATTARS_DELIMETER + password + User.ATTARS_DELIMETER + email + User.ATTARS_DELIMETER + avatar + User.ATTARS_DELIMETER + likedNotes);
editor.commit();
Intent personalSpaceActivity = new Intent(LoginActivity.this, MainActivity.class); Intent personalSpaceActivity = new Intent(LoginActivity.this, MainActivity.class);
Bundle loginUserBundle = new Bundle(); Bundle loginUserBundle = new Bundle();
loginUserBundle.putString("UserData", id + User.ATTARS_DELIMETER + username + User.ATTARS_DELIMETER + password + User.ATTARS_DELIMETER + email + User.ATTARS_DELIMETER + avatar + User.ATTARS_DELIMETER + likedNotes); loginUserBundle.putString("UserData", id + User.ATTARS_DELIMETER + username + User.ATTARS_DELIMETER + password + User.ATTARS_DELIMETER + email + User.ATTARS_DELIMETER + avatar + User.ATTARS_DELIMETER + likedNotes);

View file

@ -172,10 +172,11 @@ public class MainActivity extends AppCompatActivity
ft.commit(); ft.commit();
} else if (id == R.id.nav_logout) { } else if (id == R.id.nav_logout) {
SharedPreferences sharedPref = this.getSharedPreferences(MainActivity.LOCAL_DATA_TSN, Context.MODE_PRIVATE); try {
SharedPreferences.Editor editor = sharedPref.edit(); Utils.removeUserDataFromPrefs();
editor.remove("UserData"); } catch (Exception e) {
editor.commit(); e.printStackTrace();
}
Intent loginActivity = new Intent(MainActivity.this, LoginActivity.class); Intent loginActivity = new Intent(MainActivity.this, LoginActivity.class);
startActivity(loginActivity); startActivity(loginActivity);
finish(); finish();

View file

@ -83,9 +83,10 @@ public class SettingsFragment extends Fragment implements View.OnClickListener,
//Populate The data //Populate The data
Utils.URLtoImageView(avatarImage, user.getAvatar()); Utils.URLtoImageView(avatarImage, user.getAvatar());
this.txt_username.setText(user.getUsername()); this.txt_username.setText(""+user.getUsername());
this.txt_password.setText(user.getPassword()); this.txt_password.setText(""+user.getPassword());
this.txt_email.setText(user.getEmail()); Log.d(TAG, "onCreateView: The FUCKING EMAIL IS --------------->>>" + user.getEmail());
this.txt_email.setText(""+user.getEmail());
this.lbl_num_of_notes.setText(""+user.getNumber_of_notes()); //TODO this.lbl_num_of_notes.setText(""+user.getNumber_of_notes()); //TODO
this.lbl_num_of_liked.setText(""+user.getLiked_notes().size()); this.lbl_num_of_liked.setText(""+user.getLiked_notes().size());

View file

@ -1,16 +1,11 @@
package com.android_app.matan.ara.sagi.thesocialnotework; package com.android_app.matan.ara.sagi.thesocialnotework;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.graphics.drawable.DrawableWrapper;
import android.util.Log;
import android.view.Window; import android.view.Window;
import android.view.WindowManager; import android.view.WindowManager;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout;
import java.util.Random; import java.util.Random;
@ -18,7 +13,6 @@ public class SplashActivity extends AppCompatActivity {
private ImageView background; private ImageView background;
private int timerDelay = 3500; private int timerDelay = 3500;
private final String TAG = "Splash Screen Activity";
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -33,8 +27,8 @@ public class SplashActivity extends AppCompatActivity {
} else { } else {
background.setImageDrawable( getResources().getDrawable(rand_splash())); background.setImageDrawable( getResources().getDrawable(rand_splash()));
} }
SharedPreferences sharedPref = this.getSharedPreferences(MainActivity.LOCAL_DATA_TSN, Context.MODE_PRIVATE);
final String userData = sharedPref.getString("UserData", null); final String userData = Utils.getUserFromSharedPrefs(this);
Thread timerThread = new Thread(){ Thread timerThread = new Thread(){
public void run(){ public void run(){

View file

@ -140,7 +140,7 @@ public class User {
for (int i = 0; i < liked_notes.size(); i++) { for (int i = 0; i < liked_notes.size(); i++) {
result += liked_notes.get(i); result += liked_notes.get(i);
if(i != liked_notes.size() - 1){ if(i != liked_notes.size() - 1){
result+=";"; result+=User.LIKED_NOTES_DELIMETER;
} }
} }
return result; return result;
@ -159,13 +159,13 @@ public class User {
if(response.get("status").equals("OK")){ if(response.get("status").equals("OK")){
Log.d(TAG, "onResponse: In OKOKOKOK"); Log.d(TAG, "onResponse: In OKOKOKOK");
activity.updateNavAvatar(); activity.updateNavAvatar();
SharedPreferences sharedPref = activity.getSharedPreferences(MainActivity.LOCAL_DATA_TSN, Context.MODE_PRIVATE); Utils.updateUserSharedPref(self.Serialise());
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("UserData", self.Serialise());
activity.updateNavAvatar(); activity.updateNavAvatar();
} }
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} }
} }
}, Utils.genericErrorListener); }, Utils.genericErrorListener);

View file

@ -2,6 +2,7 @@ package com.android_app.matan.ara.sagi.thesocialnotework;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.Canvas; import android.graphics.Canvas;
@ -45,6 +46,7 @@ public class Utils {
private static boolean mLocationPermission = false; private static boolean mLocationPermission = false;
private static boolean mCameraPermission = false; private static boolean mCameraPermission = false;
private static SharedPreferences prefs;
public static Bitmap getBitmapFromURL(String url) { public static Bitmap getBitmapFromURL(String url) {
@ -208,6 +210,28 @@ public class Utils {
return mLocationPermission; return mLocationPermission;
} }
public static String getUserFromSharedPrefs(Context contexst){
if(prefs == null){
prefs = contexst.getSharedPreferences(MainActivity.LOCAL_DATA_TSN, Context.MODE_PRIVATE);
}
return prefs.getString("UserData", null);
};
public static void updateUserSharedPref(String data) throws Exception {
if(prefs == null) throw new Exception("Prefs are not available");
SharedPreferences.Editor editor = prefs.edit();
editor.putString("UserData", data);
editor.commit();
}
public static void removeUserDataFromPrefs() throws Exception{
if(prefs == null) throw new Exception("Prefs are not available");
SharedPreferences.Editor editor = prefs.edit();
editor.remove("UserData");
editor.commit();
}
} }