mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2025-10-06 13:59:49 +02:00
Preparing to support multi user. Use the database to store accounts' info. LoginActivity is successfully refactored. Any other features are unavailable for now.
This commit is contained in:
72
app/src/main/java/Account/Account.java
Normal file
72
app/src/main/java/Account/Account.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
22
app/src/main/java/Account/AccountDao.java
Normal file
22
app/src/main/java/Account/AccountDao.java
Normal file
@@ -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);
|
||||
}
|
40
app/src/main/java/Account/AccountRepository.java
Normal file
40
app/src/main/java/Account/AccountRepository.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
27
app/src/main/java/Account/AccountRoomDatabase.java
Normal file
27
app/src/main/java/Account/AccountRoomDatabase.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
47
app/src/main/java/Account/AccountViewModel.java
Normal file
47
app/src/main/java/Account/AccountViewModel.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user