diff options
Diffstat (limited to 'app/src/main/java/Account')
-rw-r--r-- | app/src/main/java/Account/Account.java | 72 | ||||
-rw-r--r-- | app/src/main/java/Account/AccountDao.java | 22 | ||||
-rw-r--r-- | app/src/main/java/Account/AccountRepository.java | 40 | ||||
-rw-r--r-- | app/src/main/java/Account/AccountRoomDatabase.java | 27 | ||||
-rw-r--r-- | app/src/main/java/Account/AccountViewModel.java | 47 |
5 files changed, 208 insertions, 0 deletions
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<Account> 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<Account> mAccountLiveData; + + AccountRepository(Application application, String username) { + mAccountDao = AccountRoomDatabase.getDatabase(application).accountDao(); + + mAccountLiveData = mAccountDao.getAccountLiveData(username); + } + + LiveData<Account> getAccountLiveData() { + return mAccountLiveData; + } + + public void insert(Account Account) { + new InsertAsyncTask(mAccountDao).execute(Account); + } + + private static class InsertAsyncTask extends AsyncTask<Account, Void, Void> { + + 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<Account> mAccountLiveData; + + public AccountViewModel(Application application, String id) { + super(application); + mAccountRepository = new AccountRepository(application, id); + mAccountLiveData = mAccountRepository.getAccountLiveData(); + } + + public LiveData<Account> 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 extends ViewModel> T create(Class<T> modelClass) { + //noinspection unchecked + return (T) new AccountViewModel(mApplication, userName); + } + } +} |