Log in other reddit accounts are available. Add an account switcher in the navigation drawer in MainActivity.

This commit is contained in:
Alex Ning
2019-08-08 20:05:45 +08:00
parent 5b5da3d3fd
commit e542ac5138
21 changed files with 451 additions and 135 deletions

View File

@@ -34,4 +34,7 @@ public interface AccountDao {
@Query("UPDATE accounts SET profile_image_url = :profileImageUrl, banner_image_url = :bannerImageUrl, " +
"karma = :karma WHERE username = :username")
void updateAccountInfo(String username, String profileImageUrl, String bannerImageUrl, int karma);
@Query("SELECT * FROM accounts WHERE username != :username")
LiveData<List<Account>> getAccountsExceptCurrentAccountLiveData(String username);
}

View File

@@ -4,21 +4,29 @@ import android.os.AsyncTask;
import androidx.lifecycle.LiveData;
import java.util.List;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
public class AccountRepository {
private AccountDao mAccountDao;
private LiveData<Account> mAccountLiveData;
private LiveData<List<Account>> mAccountsExceptCurrentAccountLiveData;
AccountRepository(RedditDataRoomDatabase redditDataRoomDatabase, String username) {
mAccountDao = redditDataRoomDatabase.accountDao();
mAccountLiveData = mAccountDao.getAccountLiveData(username);
mAccountsExceptCurrentAccountLiveData = mAccountDao.getAccountsExceptCurrentAccountLiveData(username);
}
LiveData<Account> getAccountLiveData() {
return mAccountLiveData;
}
public LiveData<List<Account>> getAccountsExceptCurrentAccountLiveData() {
return mAccountsExceptCurrentAccountLiveData;
}
public void insert(Account Account) {
new InsertAsyncTask(mAccountDao).execute(Account);
}

View File

@@ -8,22 +8,30 @@ import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import java.util.List;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
public class AccountViewModel extends AndroidViewModel {
private AccountRepository mAccountRepository;
private LiveData<Account> mAccountLiveData;
private LiveData<List<Account>> mAccountsExceptCurrentAccountLiveData;
public AccountViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String id) {
super(application);
mAccountRepository = new AccountRepository(redditDataRoomDatabase, id);
mAccountLiveData = mAccountRepository.getAccountLiveData();
mAccountsExceptCurrentAccountLiveData = mAccountRepository.getAccountsExceptCurrentAccountLiveData();
}
public LiveData<Account> getAccountLiveData() {
return mAccountLiveData;
}
public LiveData<List<Account>> getAccountsExceptCurrentAccountLiveData() {
return mAccountsExceptCurrentAccountLiveData;
}
public void insert(Account userData) {
mAccountRepository.insert(userData);
}