فهرست منبع

Add SharedPreferencesHelperClass

felocsss 1 سال پیش
والد
کامیت
fa729b2d16

+ 45 - 0
app/src/main/java/com/sharix/sportsmanfriend/SharedPreferencesHelperClass.java

@@ -0,0 +1,45 @@
+package com.sharix.sportsmanfriend;
+
+import static android.content.Context.MODE_PRIVATE;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+
+public class SharedPreferencesHelperClass {
+
+    private static SharedPreferences sharedPreferences;
+    private static SharedPreferences.Editor editor;
+
+    public static boolean getBoolVar(Context context, String name){
+        setVars(context);
+        return sharedPreferences.getBoolean(name, false);
+    }
+
+    public static void setBoolVar(Context context, String name, boolean value){
+        setVars(context);
+        editor.putBoolean(name, value);
+        editor.apply();
+    }
+
+    public static String getStringVar(Context context, String name){
+        setVars(context);
+        return sharedPreferences.getString(name, "Error");
+    }
+
+    public static void setStringVar(Context context, String name, String value){
+        setVars(context);
+        editor.putString(name, value);
+        editor.apply();
+    }
+
+    public static void clearSharedPreferences(Context context){
+        setVars(context);
+        editor = sharedPreferences.edit();
+        editor.clear().commit();
+    }
+
+    private static void setVars(Context context){
+        sharedPreferences = context.getSharedPreferences("Data", MODE_PRIVATE);
+        editor = sharedPreferences.edit();
+    }
+}

+ 2 - 7
app/src/main/java/com/sharix/sportsmanfriend/view/LauncherActivity.java

@@ -1,17 +1,15 @@
 package com.sharix.sportsmanfriend.view;
 
 import android.content.Intent;
-import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.os.Handler;
 
 import androidx.appcompat.app.AppCompatActivity;
 
 import com.sharix.sportsmanfriend.R;
+import com.sharix.sportsmanfriend.SharedPreferencesHelperClass;
 
 public class LauncherActivity extends AppCompatActivity {
-
-    private SharedPreferences sharedPreferences;
     boolean userIsLoggedIn;
 
     @Override
@@ -19,10 +17,7 @@ public class LauncherActivity extends AppCompatActivity {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_launcher);
 
-        sharedPreferences = getSharedPreferences("Data", MODE_PRIVATE);
-        userIsLoggedIn = sharedPreferences.getBoolean("userIsLoggedIn", false);
-
-        if(userIsLoggedIn){
+        if(SharedPreferencesHelperClass.getBoolVar(getApplicationContext(), "userIsLoggedIn")){
             new Handler().postDelayed(() -> {
                 Intent intent = new Intent(LauncherActivity.this, MainActivity.class);
                 startActivity(intent);

+ 3 - 8
app/src/main/java/com/sharix/sportsmanfriend/view/SingInSingUpActivity.java

@@ -1,7 +1,6 @@
 package com.sharix.sportsmanfriend.view;
 
 import android.content.Intent;
-import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.widget.Button;
 import android.widget.EditText;
@@ -10,6 +9,7 @@ import android.widget.Toast;
 import androidx.appcompat.app.AppCompatActivity;
 
 import com.sharix.sportsmanfriend.R;
+import com.sharix.sportsmanfriend.SharedPreferencesHelperClass;
 import com.sharix.sportsmanfriend.requests.HTTPRequest;
 
 import java.util.Objects;
@@ -18,8 +18,6 @@ public class SingInSingUpActivity extends AppCompatActivity {
 
     private Button btnSingIn;
     private EditText EDPhone, EDPassword;
-    private SharedPreferences sharedPreferences;
-    private SharedPreferences.Editor editor;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -30,8 +28,6 @@ public class SingInSingUpActivity extends AppCompatActivity {
         EDPhone = findViewById(R.id.phoneED);
         EDPassword = findViewById(R.id.passwordED);
 
-        sharedPreferences = getSharedPreferences("Data", MODE_PRIVATE);
-
         EDPhone.setText("99999999999");
         EDPassword.setText("password");
 
@@ -41,9 +37,8 @@ public class SingInSingUpActivity extends AppCompatActivity {
             if (Objects.equals(response, "Ошибка")){
                 Toast.makeText(getApplicationContext(), "Не правильный логин или пароль", Toast.LENGTH_LONG).show();
             } else {
-                editor = sharedPreferences.edit();
-                editor.putBoolean("userIsLoggedIn", true);
-                editor.apply();
+                SharedPreferencesHelperClass.setBoolVar(getApplicationContext(), "userIsLoggedIn", true);
+                SharedPreferencesHelperClass.setStringVar(getApplicationContext(), "token", response);
                 Intent intent = new Intent(SingInSingUpActivity.this, MainActivity.class);
                 startActivity(intent);
                 finish();

+ 4 - 8
app/src/main/java/com/sharix/sportsmanfriend/view/fragments/MoreFragment.java

@@ -1,7 +1,6 @@
 package com.sharix.sportsmanfriend.view.fragments;
 
 import android.content.Intent;
-import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.View;
@@ -13,14 +12,13 @@ import androidx.fragment.app.FragmentManager;
 import androidx.fragment.app.FragmentTransaction;
 
 import com.sharix.sportsmanfriend.R;
-import com.sharix.sportsmanfriend.view.MainActivity;
+import com.sharix.sportsmanfriend.SharedPreferencesHelperClass;
+import com.sharix.sportsmanfriend.view.SingInSingUpActivity;
 
 public class MoreFragment extends Fragment {
 
     private View view;
     private ConstraintLayout settingsBlock, profileBlock, logOutBlock;
-    private SharedPreferences sharedPreferences;
-    private SharedPreferences.Editor editor;
 
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
@@ -58,11 +56,9 @@ public class MoreFragment extends Fragment {
         });
 
         logOutBlock.setOnClickListener(v -> {
-            editor = sharedPreferences.edit();
-            editor.clear().commit();
-            Intent intent = new Intent(requireActivity(), MainActivity.class);
+            SharedPreferencesHelperClass.clearSharedPreferences(requireActivity());
+            Intent intent = new Intent(requireActivity(), SingInSingUpActivity.class);
             startActivity(intent);
-            requireActivity().finish();
         });
     }
 }