Advertisement
Guest User

Rekt

a guest
Mar 23rd, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. package com.lukewaugh.paradiddles.authClasses;
  2.  
  3.  
  4. import android.content.Context;
  5. import android.content.SharedPreferences;
  6. /*
  7. Java Class for manipulating user details to
  8. a file stored on the phone:
  9. 'SharedPreferences'.
  10. */
  11. public class UserLocal {
  12.     /*
  13.     Step 1:
  14.     Create the variable of the file where details
  15.     will be stored.
  16.     */
  17.     public static final String SP_NAME = "userDetails";
  18.     /*
  19.     Step 2:
  20.     Instantiate the SharedPreferences Class
  21.     for accessing, modifying and adding data on the users phone.
  22.     */
  23.     private SharedPreferences userDB;
  24.     /*
  25.     Step 3:
  26.     Get the context from the Activity which will be using this
  27.     Class.
  28.     */
  29.     public UserLocal(Context context) {
  30.         userDB = context.getSharedPreferences(SP_NAME, 0);
  31.  
  32.     }
  33.     /*
  34.     Step 4:
  35.     Generate methods to retrieve and set the user data
  36.     from the local database.
  37.     =================================================
  38.     4.1: Create a method to store the User data
  39.     by instantiating a variable type of
  40.     object SharedPreferences.
  41.     Modifications MUST go through an
  42.     SharedPreferences.Editor object to maintain
  43.     consistent state and control when they are
  44.     committed to the local storage.
  45.     */
  46.     public void storeUserData(User user) {
  47.         SharedPreferences.Editor editor = userDB.edit();
  48.  
  49.         editor.putString("username", user.username);
  50.         editor.putString("password", user.password);
  51.         editor.putInt("age", user.age);
  52.  
  53.         editor.apply();
  54.     }
  55.     /*
  56.     =================================================
  57.     4.2: Create a method to return the a User
  58.     who is logged in to the database by
  59.     getting the attributes from the
  60.     SharedPreferences object.
  61.     */
  62.     public User getLoggedInUser() {
  63.         String username = userDB.getString("username", "");
  64.         String email = userDB.getString("email", "");
  65.         String password = userDB.getString("password", "");
  66.         int age = userDB.getInt("age", -1);
  67.  
  68.         User storedUser = new User(username, email, password, age);
  69.  
  70.         return storedUser;
  71.  
  72.     }
  73.     /*
  74.     4.3: Create a method to set whether the User
  75.     is logged in or out, use a boolean to return
  76.     true or false.
  77.     */
  78.     public void setLoggedInUser(boolean loggedIn) {
  79.         SharedPreferences.Editor editor = userDB.edit();
  80.         editor.putBoolean("loggedIn", loggedIn);
  81.  
  82.         editor.apply();
  83.     }
  84.     /*
  85.     4.4: Create a method to clear the user data
  86.     from the SharedPreferences object
  87.     */
  88.     public void clearData() {
  89.         SharedPreferences.Editor editor = userDB.edit();
  90.         editor.clear();
  91.         editor.apply();
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement