diff options
Diffstat (limited to 'app/src/main/java/Account')
-rw-r--r-- | app/src/main/java/Account/Account.java | 4 | ||||
-rw-r--r-- | app/src/main/java/Account/AccountDao.java | 11 | ||||
-rw-r--r-- | app/src/main/java/Account/AccountRepository.java | 8 | ||||
-rw-r--r-- | app/src/main/java/Account/AccountRoomDatabase.java | 27 | ||||
-rw-r--r-- | app/src/main/java/Account/AccountViewModel.java | 17 |
5 files changed, 29 insertions, 38 deletions
diff --git a/app/src/main/java/Account/Account.java b/app/src/main/java/Account/Account.java index 95873c61..4ae15741 100644 --- a/app/src/main/java/Account/Account.java +++ b/app/src/main/java/Account/Account.java @@ -58,6 +58,10 @@ public class Account { return accessToken; } + public void setAccessToken(String accessToken) { + this.accessToken = accessToken; + } + public String getRefreshToken() { return refreshToken; } diff --git a/app/src/main/java/Account/AccountDao.java b/app/src/main/java/Account/AccountDao.java index bd53b92b..8db57683 100644 --- a/app/src/main/java/Account/AccountDao.java +++ b/app/src/main/java/Account/AccountDao.java @@ -6,11 +6,19 @@ import androidx.room.Insert; import androidx.room.OnConflictStrategy; import androidx.room.Query; +import java.util.List; + @Dao public interface AccountDao { @Insert(onConflict = OnConflictStrategy.REPLACE) void insert(Account account); + @Query("SELECT * FROM accounts WHERE is_current_user = 0") + List<Account> getAllNonCurrentAccounts(); + + @Query("UPDATE accounts SET is_current_user = 0 WHERE is_current_user = 1") + void markAllAccountsNonCurrent(); + @Query("DELETE FROM accounts") void deleteAllAccounts(); @@ -19,4 +27,7 @@ public interface AccountDao { @Query("SELECT * FROM accounts WHERE username = :userName COLLATE NOCASE LIMIT 1") Account getAccountData(String userName); + + @Query("SELECT * FROM accounts WHERE is_current_user = 1 LIMIT 1") + Account getCurrentAccount(); } diff --git a/app/src/main/java/Account/AccountRepository.java b/app/src/main/java/Account/AccountRepository.java index 49bdfacf..c495579d 100644 --- a/app/src/main/java/Account/AccountRepository.java +++ b/app/src/main/java/Account/AccountRepository.java @@ -1,17 +1,17 @@ package Account; -import android.app.Application; import android.os.AsyncTask; import androidx.lifecycle.LiveData; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + public class AccountRepository { private AccountDao mAccountDao; private LiveData<Account> mAccountLiveData; - AccountRepository(Application application, String username) { - mAccountDao = AccountRoomDatabase.getDatabase(application).accountDao(); - + AccountRepository(RedditDataRoomDatabase redditDataRoomDatabase, String username) { + mAccountDao = redditDataRoomDatabase.accountDao(); mAccountLiveData = mAccountDao.getAccountLiveData(username); } diff --git a/app/src/main/java/Account/AccountRoomDatabase.java b/app/src/main/java/Account/AccountRoomDatabase.java deleted file mode 100644 index 2f5d0120..00000000 --- a/app/src/main/java/Account/AccountRoomDatabase.java +++ /dev/null @@ -1,27 +0,0 @@ -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 index b649d9a7..ab6e9663 100644 --- a/app/src/main/java/Account/AccountViewModel.java +++ b/app/src/main/java/Account/AccountViewModel.java @@ -8,13 +8,15 @@ import androidx.lifecycle.LiveData; import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModelProvider; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + public class AccountViewModel extends AndroidViewModel { private AccountRepository mAccountRepository; private LiveData<Account> mAccountLiveData; - public AccountViewModel(Application application, String id) { + public AccountViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String id) { super(application); - mAccountRepository = new AccountRepository(application, id); + mAccountRepository = new AccountRepository(redditDataRoomDatabase, id); mAccountLiveData = mAccountRepository.getAccountLiveData(); } @@ -30,18 +32,19 @@ public class AccountViewModel extends AndroidViewModel { @NonNull private final Application mApplication; + private final RedditDataRoomDatabase mRedditDataRoomDatabase; + private final String mUsername; - private final String userName; - - public Factory(@NonNull Application application, String userName) { + public Factory(@NonNull Application application, RedditDataRoomDatabase redditDataRoomDatabase, String username) { mApplication = application; - this.userName = userName; + mRedditDataRoomDatabase = redditDataRoomDatabase; + mUsername = username; } @Override public <T extends ViewModel> T create(Class<T> modelClass) { //noinspection unchecked - return (T) new AccountViewModel(mApplication, userName); + return (T) new AccountViewModel(mApplication, mRedditDataRoomDatabase, mUsername); } } } |