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);
mainActivity.getUser().getLiked_notes().add(note.getId());
mainActivity.getUser().updateUser(mainActivity);
likes.setText("Likes: "+(note.getLikes()+1));
}
}

View file

@ -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);

View file

@ -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();

View file

@ -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());

View file

@ -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(){

View file

@ -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);

View file

@ -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();
}
}