Update karma in the navigation drawer in MainActivity.

This commit is contained in:
Alex Ning 2020-10-14 07:39:34 +08:00
parent 64d56f930d
commit d6d4e230a9
4 changed files with 25 additions and 0 deletions

View File

@ -11,16 +11,22 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
public class AccountRepository {
private AccountDao mAccountDao;
private LiveData<List<Account>> mAccountsExceptCurrentAccountLiveData;
private LiveData<Account> mCurrentAccountLiveData;
AccountRepository(RedditDataRoomDatabase redditDataRoomDatabase, String username) {
mAccountDao = redditDataRoomDatabase.accountDao();
mAccountsExceptCurrentAccountLiveData = mAccountDao.getAccountsExceptCurrentAccountLiveData();
mCurrentAccountLiveData = mAccountDao.getCurrentAccountLiveData();
}
public LiveData<List<Account>> getAccountsExceptCurrentAccountLiveData() {
return mAccountsExceptCurrentAccountLiveData;
}
public LiveData<Account> getCurrentAccountLiveData() {
return mCurrentAccountLiveData;
}
public void insert(Account Account) {
new InsertAsyncTask(mAccountDao).execute(Account);
}

View File

@ -15,17 +15,23 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
public class AccountViewModel extends AndroidViewModel {
private AccountRepository mAccountRepository;
private LiveData<List<Account>> mAccountsExceptCurrentAccountLiveData;
private LiveData<Account> mCurrentAccountLiveData;
public AccountViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String id) {
super(application);
mAccountRepository = new AccountRepository(redditDataRoomDatabase, id);
mAccountsExceptCurrentAccountLiveData = mAccountRepository.getAccountsExceptCurrentAccountLiveData();
mCurrentAccountLiveData = mAccountRepository.getCurrentAccountLiveData();
}
public LiveData<List<Account>> getAccountsExceptCurrentAccountLiveData() {
return mAccountsExceptCurrentAccountLiveData;
}
public LiveData<Account> getCurrentAccountLiveData() {
return mCurrentAccountLiveData;
}
public void insert(Account userData) {
mAccountRepository.insert(userData);
}

View File

@ -31,6 +31,7 @@ import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
@ -63,6 +64,7 @@ import javax.inject.Named;
import butterknife.BindView;
import butterknife.ButterKnife;
import ml.docilealligator.infinityforreddit.Account.Account;
import ml.docilealligator.infinityforreddit.Account.AccountViewModel;
import ml.docilealligator.infinityforreddit.ActivityToolbarInterface;
import ml.docilealligator.infinityforreddit.Adapter.NavigationDrawerRecyclerViewAdapter;
@ -833,6 +835,12 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
accountViewModel = new ViewModelProvider(this,
new AccountViewModel.Factory(getApplication(), mRedditDataRoomDatabase, mAccountName)).get(AccountViewModel.class);
accountViewModel.getAccountsExceptCurrentAccountLiveData().observe(this, adapter::changeAccountsDataset);
accountViewModel.getCurrentAccountLiveData().observe(this, new Observer<Account>() {
@Override
public void onChanged(Account account) {
adapter.updateKarma(account.getKarma());
}
});
if (getIntent().hasExtra(EXTRA_POST_TYPE)) {
String type = getIntent().getStringExtra(EXTRA_POST_TYPE);

View File

@ -546,6 +546,11 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re
}
}
public void updateKarma(int karma) {
this.karma = karma;
notifyItemChanged(0);
}
public void setNSFWEnabled(boolean isNSFWEnabled) {
this.isNSFWEnabled = isNSFWEnabled;
if (isInMainPage) {