From d6d4e230a9697da259a638df56615bc5f4f312d3 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 14 Oct 2020 07:39:34 +0800 Subject: Update karma in the navigation drawer in MainActivity. --- .../infinityforreddit/Account/AccountRepository.java | 6 ++++++ .../infinityforreddit/Account/AccountViewModel.java | 6 ++++++ .../docilealligator/infinityforreddit/Activity/MainActivity.java | 8 ++++++++ .../Adapter/NavigationDrawerRecyclerViewAdapter.java | 5 +++++ 4 files changed, 25 insertions(+) (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Account/AccountRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Account/AccountRepository.java index 69ad4088..70fccd02 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Account/AccountRepository.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Account/AccountRepository.java @@ -11,16 +11,22 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; public class AccountRepository { private AccountDao mAccountDao; private LiveData> mAccountsExceptCurrentAccountLiveData; + private LiveData mCurrentAccountLiveData; AccountRepository(RedditDataRoomDatabase redditDataRoomDatabase, String username) { mAccountDao = redditDataRoomDatabase.accountDao(); mAccountsExceptCurrentAccountLiveData = mAccountDao.getAccountsExceptCurrentAccountLiveData(); + mCurrentAccountLiveData = mAccountDao.getCurrentAccountLiveData(); } public LiveData> getAccountsExceptCurrentAccountLiveData() { return mAccountsExceptCurrentAccountLiveData; } + public LiveData getCurrentAccountLiveData() { + return mCurrentAccountLiveData; + } + public void insert(Account Account) { new InsertAsyncTask(mAccountDao).execute(Account); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Account/AccountViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Account/AccountViewModel.java index 55e805e8..0eab1570 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Account/AccountViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Account/AccountViewModel.java @@ -15,17 +15,23 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; public class AccountViewModel extends AndroidViewModel { private AccountRepository mAccountRepository; private LiveData> mAccountsExceptCurrentAccountLiveData; + private LiveData mCurrentAccountLiveData; public AccountViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String id) { super(application); mAccountRepository = new AccountRepository(redditDataRoomDatabase, id); mAccountsExceptCurrentAccountLiveData = mAccountRepository.getAccountsExceptCurrentAccountLiveData(); + mCurrentAccountLiveData = mAccountRepository.getCurrentAccountLiveData(); } public LiveData> getAccountsExceptCurrentAccountLiveData() { return mAccountsExceptCurrentAccountLiveData; } + public LiveData getCurrentAccountLiveData() { + return mCurrentAccountLiveData; + } + public void insert(Account userData) { mAccountRepository.insert(userData); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java index 90302bea..4d42621e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java @@ -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() { + @Override + public void onChanged(Account account) { + adapter.updateKarma(account.getKarma()); + } + }); if (getIntent().hasExtra(EXTRA_POST_TYPE)) { String type = getIntent().getStringExtra(EXTRA_POST_TYPE); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/NavigationDrawerRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/NavigationDrawerRecyclerViewAdapter.java index d8147480..cdb73759 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/NavigationDrawerRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/NavigationDrawerRecyclerViewAdapter.java @@ -546,6 +546,11 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter