From 7f2bc01180c284154fb69976122016c4638e9973 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 7 Aug 2019 10:54:47 +0800 Subject: Preparing to support multi user. Use the database to store accounts' info. LoginActivity is successfully refactored. Any other features are unavailable for now. --- app/src/main/java/Account/Account.java | 72 ++++++++++++++++++++++ app/src/main/java/Account/AccountDao.java | 22 +++++++ app/src/main/java/Account/AccountRepository.java | 40 ++++++++++++ app/src/main/java/Account/AccountRoomDatabase.java | 27 ++++++++ app/src/main/java/Account/AccountViewModel.java | 47 ++++++++++++++ 5 files changed, 208 insertions(+) create mode 100644 app/src/main/java/Account/Account.java create mode 100644 app/src/main/java/Account/AccountDao.java create mode 100644 app/src/main/java/Account/AccountRepository.java create mode 100644 app/src/main/java/Account/AccountRoomDatabase.java create mode 100644 app/src/main/java/Account/AccountViewModel.java (limited to 'app/src/main/java/Account') diff --git a/app/src/main/java/Account/Account.java b/app/src/main/java/Account/Account.java new file mode 100644 index 00000000..95873c61 --- /dev/null +++ b/app/src/main/java/Account/Account.java @@ -0,0 +1,72 @@ +package Account; + +import androidx.annotation.NonNull; +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.PrimaryKey; + +@Entity(tableName = "accounts") +public class Account { + @PrimaryKey + @NonNull + @ColumnInfo(name = "username") + private String username; + @ColumnInfo(name = "profile_image_url") + private String profileImageUrl; + @ColumnInfo(name = "banner_image_url") + private String bannerImageUrl; + @ColumnInfo(name = "karma") + private int karma; + @ColumnInfo(name = "access_token") + private String accessToken; + @ColumnInfo(name = "refresh_token") + private String refreshToken; + @ColumnInfo(name = "code") + private String code; + @ColumnInfo(name = "is_current_user") + private boolean isCurrentUser; + + public Account(@NonNull String username, String accessToken, String refreshToken, String code, + String profileImageUrl, String bannerImageUrl, int karma, boolean isCurrentUser) { + this.username = username; + this.accessToken = accessToken; + this.refreshToken = refreshToken; + this.code = code; + this.profileImageUrl = profileImageUrl; + this.bannerImageUrl = bannerImageUrl; + this.karma = karma; + this.isCurrentUser = isCurrentUser; + } + + public String getUsername() { + return username; + } + + public String getProfileImageUrl() { + return profileImageUrl; + } + + public String getBannerImageUrl() { + return bannerImageUrl; + } + + public int getKarma() { + return karma; + } + + public String getAccessToken() { + return accessToken; + } + + public String getRefreshToken() { + return refreshToken; + } + + public String getCode() { + return code; + } + + public boolean isCurrentUser() { + return isCurrentUser; + } +} diff --git a/app/src/main/java/Account/AccountDao.java b/app/src/main/java/Account/AccountDao.java new file mode 100644 index 00000000..bd53b92b --- /dev/null +++ b/app/src/main/java/Account/AccountDao.java @@ -0,0 +1,22 @@ +package Account; + +import androidx.lifecycle.LiveData; +import androidx.room.Dao; +import androidx.room.Insert; +import androidx.room.OnConflictStrategy; +import androidx.room.Query; + +@Dao +public interface AccountDao { + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insert(Account account); + + @Query("DELETE FROM accounts") + void deleteAllAccounts(); + + @Query("SELECT * FROM accounts WHERE username = :userName COLLATE NOCASE LIMIT 1") + LiveData getAccountLiveData(String userName); + + @Query("SELECT * FROM accounts WHERE username = :userName COLLATE NOCASE LIMIT 1") + Account getAccountData(String userName); +} diff --git a/app/src/main/java/Account/AccountRepository.java b/app/src/main/java/Account/AccountRepository.java new file mode 100644 index 00000000..49bdfacf --- /dev/null +++ b/app/src/main/java/Account/AccountRepository.java @@ -0,0 +1,40 @@ +package Account; + +import android.app.Application; +import android.os.AsyncTask; + +import androidx.lifecycle.LiveData; + +public class AccountRepository { + private AccountDao mAccountDao; + private LiveData mAccountLiveData; + + AccountRepository(Application application, String username) { + mAccountDao = AccountRoomDatabase.getDatabase(application).accountDao(); + + mAccountLiveData = mAccountDao.getAccountLiveData(username); + } + + LiveData getAccountLiveData() { + return mAccountLiveData; + } + + public void insert(Account Account) { + new InsertAsyncTask(mAccountDao).execute(Account); + } + + private static class InsertAsyncTask extends AsyncTask { + + private AccountDao mAsyncTaskDao; + + InsertAsyncTask(AccountDao dao) { + mAsyncTaskDao = dao; + } + + @Override + protected Void doInBackground(final Account... params) { + mAsyncTaskDao.insert(params[0]); + return null; + } + } +} diff --git a/app/src/main/java/Account/AccountRoomDatabase.java b/app/src/main/java/Account/AccountRoomDatabase.java new file mode 100644 index 00000000..2f5d0120 --- /dev/null +++ b/app/src/main/java/Account/AccountRoomDatabase.java @@ -0,0 +1,27 @@ +package Account; + +import android.content.Context; + +import androidx.room.Database; +import androidx.room.Room; +import androidx.room.RoomDatabase; + +@Database(entities = {Account.class}, version = 1) +public abstract class AccountRoomDatabase extends RoomDatabase { + private static AccountRoomDatabase INSTANCE; + + public abstract AccountDao accountDao(); + + public static AccountRoomDatabase getDatabase(final Context context) { + if(INSTANCE == null) { + synchronized (AccountRoomDatabase.class) { + if(INSTANCE == null) { + INSTANCE = Room.databaseBuilder(context.getApplicationContext(), + AccountRoomDatabase.class, "accounts") + .build(); + } + } + } + return INSTANCE; + } +} diff --git a/app/src/main/java/Account/AccountViewModel.java b/app/src/main/java/Account/AccountViewModel.java new file mode 100644 index 00000000..b649d9a7 --- /dev/null +++ b/app/src/main/java/Account/AccountViewModel.java @@ -0,0 +1,47 @@ +package Account; + +import android.app.Application; + +import androidx.annotation.NonNull; +import androidx.lifecycle.AndroidViewModel; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.ViewModel; +import androidx.lifecycle.ViewModelProvider; + +public class AccountViewModel extends AndroidViewModel { + private AccountRepository mAccountRepository; + private LiveData mAccountLiveData; + + public AccountViewModel(Application application, String id) { + super(application); + mAccountRepository = new AccountRepository(application, id); + mAccountLiveData = mAccountRepository.getAccountLiveData(); + } + + public LiveData getAccountLiveData() { + return mAccountLiveData; + } + + public void insert(Account userData) { + mAccountRepository.insert(userData); + } + + public static class Factory extends ViewModelProvider.NewInstanceFactory { + + @NonNull + private final Application mApplication; + + private final String userName; + + public Factory(@NonNull Application application, String userName) { + mApplication = application; + this.userName = userName; + } + + @Override + public T create(Class modelClass) { + //noinspection unchecked + return (T) new AccountViewModel(mApplication, userName); + } + } +} -- cgit v1.2.3