mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2024-12-29 20:37:12 +01:00
Update karma in the navigation drawer in MainActivity.
This commit is contained in:
parent
64d56f930d
commit
d6d4e230a9
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user