From 681f40d2e7f8d48bdf4a1bd4cfa7aa085b8eac5c Mon Sep 17 00:00:00 2001 From: Sagi Dayan Date: Sat, 9 Jul 2016 17:38:54 +0300 Subject: [PATCH] Cleaned repetitive code --- .../sagi/thesocialnotework/GmapFragment.java | 1 + .../sagi/thesocialnotework/LoginActivity.java | 7 +----- .../sagi/thesocialnotework/MainActivity.java | 9 +++---- .../thesocialnotework/SettingsFragment.java | 7 +++--- .../thesocialnotework/SplashActivity.java | 10 ++------ .../ara/sagi/thesocialnotework/User.java | 8 +++---- .../ara/sagi/thesocialnotework/Utils.java | 24 +++++++++++++++++++ 7 files changed, 41 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/GmapFragment.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/GmapFragment.java index 2ec294e..247726b 100644 --- a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/GmapFragment.java +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/GmapFragment.java @@ -284,6 +284,7 @@ public class GmapFragment extends Fragment implements OnMapReadyCallback { } VolleyUtilSingleton.getInstance(getActivity()).post(Utils.BASE_URL + "/note/like", jsonObj, getNotesSuccessListener, Utils.genericErrorListener); mainActivity.getUser().getLiked_notes().add(note.getId()); + mainActivity.getUser().updateUser(mainActivity); likes.setText("Likes: "+(note.getLikes()+1)); } } diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/LoginActivity.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/LoginActivity.java index 5ff7851..01b8bd9 100644 --- a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/LoginActivity.java +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/LoginActivity.java @@ -38,11 +38,9 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList private TextView mRegisterButton; private Button mLoginButton; private final String TAG = "[TSN/LoginActivity]"; - private final String BASE_URL = "http://thesocialnotework-api.appspot.com/api"; private final String LOGIN_PATH = "/login"; private LoginActivity self; //this protected LinearLayout layout; - private boolean permissionsReturend = false; @Override @@ -126,8 +124,6 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList try { if (!response.isNull("user")) { 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 = ""; JSONArray likedNotes_JSON; @@ -143,8 +139,7 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList 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); - editor.commit(); + Utils.updateUserSharedPref(id + User.ATTARS_DELIMETER + username + User.ATTARS_DELIMETER + password + User.ATTARS_DELIMETER + email + User.ATTARS_DELIMETER + avatar + User.ATTARS_DELIMETER + likedNotes); Intent personalSpaceActivity = new Intent(LoginActivity.this, MainActivity.class); 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); diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/MainActivity.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/MainActivity.java index f39a303..449c4c0 100644 --- a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/MainActivity.java +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/MainActivity.java @@ -172,10 +172,11 @@ public class MainActivity extends AppCompatActivity ft.commit(); } else if (id == R.id.nav_logout) { - SharedPreferences sharedPref = this.getSharedPreferences(MainActivity.LOCAL_DATA_TSN, Context.MODE_PRIVATE); - SharedPreferences.Editor editor = sharedPref.edit(); - editor.remove("UserData"); - editor.commit(); + try { + Utils.removeUserDataFromPrefs(); + } catch (Exception e) { + e.printStackTrace(); + } Intent loginActivity = new Intent(MainActivity.this, LoginActivity.class); startActivity(loginActivity); finish(); diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/SettingsFragment.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/SettingsFragment.java index ad84c83..ebbbcf7 100644 --- a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/SettingsFragment.java +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/SettingsFragment.java @@ -83,9 +83,10 @@ public class SettingsFragment extends Fragment implements View.OnClickListener, //Populate The data Utils.URLtoImageView(avatarImage, user.getAvatar()); - this.txt_username.setText(user.getUsername()); - this.txt_password.setText(user.getPassword()); - this.txt_email.setText(user.getEmail()); + this.txt_username.setText(""+user.getUsername()); + this.txt_password.setText(""+user.getPassword()); + 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_liked.setText(""+user.getLiked_notes().size()); diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/SplashActivity.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/SplashActivity.java index d8b6e40..480db6f 100644 --- a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/SplashActivity.java +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/SplashActivity.java @@ -1,16 +1,11 @@ package com.android_app.matan.ara.sagi.thesocialnotework; -import android.content.Context; import android.content.Intent; -import android.content.SharedPreferences; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; -import android.support.v7.graphics.drawable.DrawableWrapper; -import android.util.Log; import android.view.Window; import android.view.WindowManager; import android.widget.ImageView; -import android.widget.LinearLayout; import java.util.Random; @@ -18,7 +13,6 @@ public class SplashActivity extends AppCompatActivity { private ImageView background; private int timerDelay = 3500; - private final String TAG = "Splash Screen Activity"; @Override protected void onCreate(Bundle savedInstanceState) { @@ -33,8 +27,8 @@ public class SplashActivity extends AppCompatActivity { } else { 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(){ public void run(){ diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/User.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/User.java index f83b84a..c295897 100644 --- a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/User.java +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/User.java @@ -140,7 +140,7 @@ public class User { for (int i = 0; i < liked_notes.size(); i++) { result += liked_notes.get(i); if(i != liked_notes.size() - 1){ - result+=";"; + result+=User.LIKED_NOTES_DELIMETER; } } return result; @@ -159,13 +159,13 @@ public class User { if(response.get("status").equals("OK")){ Log.d(TAG, "onResponse: In OKOKOKOK"); activity.updateNavAvatar(); - SharedPreferences sharedPref = activity.getSharedPreferences(MainActivity.LOCAL_DATA_TSN, Context.MODE_PRIVATE); - SharedPreferences.Editor editor = sharedPref.edit(); - editor.putString("UserData", self.Serialise()); + Utils.updateUserSharedPref(self.Serialise()); activity.updateNavAvatar(); } } catch (JSONException e) { e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); } } }, Utils.genericErrorListener); diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/Utils.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/Utils.java index b5a07c7..c6a0369 100644 --- a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/Utils.java +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/Utils.java @@ -2,6 +2,7 @@ package com.android_app.matan.ara.sagi.thesocialnotework; import android.app.ProgressDialog; import android.content.Context; +import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; @@ -45,6 +46,7 @@ public class Utils { private static boolean mLocationPermission = false; private static boolean mCameraPermission = false; + private static SharedPreferences prefs; public static Bitmap getBitmapFromURL(String url) { @@ -208,6 +210,28 @@ public class Utils { 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(); + } + + }