mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2025-10-06 13:59:49 +02:00
Refactored all the other classes to support multi user. Clearing the app data is required before launching the app.
This commit is contained in:
@@ -58,6 +58,10 @@ public class Account {
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
public void setAccessToken(String accessToken) {
|
||||
this.accessToken = accessToken;
|
||||
}
|
||||
|
||||
public String getRefreshToken() {
|
||||
return refreshToken;
|
||||
}
|
||||
|
@@ -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();
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user