From a9fc9799fc9ad46b2c877c70778195c58705a6a6 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Tue, 23 Jul 2019 21:54:43 +0200 Subject: [PATCH] Add account deletion action --- .../activities/AccountSettingsActivity.java | 36 +++++++ .../readrops/app/activities/MainActivity.java | 98 +++++++++---------- .../readrops/app/database/dao/AccountDao.java | 4 + .../app/database/entities/Account.java | 25 ++++- .../com/readrops/app/utils/DrawerManager.java | 26 ++++- .../app/viewmodels/AccountViewModel.java | 7 ++ .../app/viewmodels/MainViewModel.java | 9 ++ .../res/layout/activity_account_settings.xml | 25 ++++- app/src/main/res/values-fr-rFR/strings.xml | 2 + app/src/main/res/values/strings.xml | 2 + 10 files changed, 177 insertions(+), 57 deletions(-) diff --git a/app/src/main/java/com/readrops/app/activities/AccountSettingsActivity.java b/app/src/main/java/com/readrops/app/activities/AccountSettingsActivity.java index 3a36bf7d..1d6cc8f7 100644 --- a/app/src/main/java/com/readrops/app/activities/AccountSettingsActivity.java +++ b/app/src/main/java/com/readrops/app/activities/AccountSettingsActivity.java @@ -3,19 +3,28 @@ package com.readrops.app.activities; import android.content.Intent; import android.os.Bundle; import android.view.MenuItem; +import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import androidx.databinding.DataBindingUtil; +import androidx.lifecycle.ViewModelProviders; +import com.afollestad.materialdialogs.MaterialDialog; import com.readrops.app.R; import com.readrops.app.database.entities.Account; import com.readrops.app.databinding.ActivityAccountSettingsBinding; +import com.readrops.app.viewmodels.AccountViewModel; + +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.observers.DisposableCompletableObserver; +import io.reactivex.schedulers.Schedulers; public class AccountSettingsActivity extends AppCompatActivity { public static final String ACCOUNT = "ACCOUNT"; private ActivityAccountSettingsBinding binding; + private AccountViewModel viewModel; @Override protected void onCreate(Bundle savedInstanceState) { @@ -26,6 +35,8 @@ public class AccountSettingsActivity extends AppCompatActivity { setTitle(account.getAccountName()); + viewModel = ViewModelProviders.of(this).get(AccountViewModel.class); + binding.accountSettingsFeeds.setOnClickListener(v -> { Intent intent = new Intent(getApplication(), ManageFeedsFoldersActivity.class); intent.putExtra(ManageFeedsFoldersActivity.ACCOUNT, account); @@ -40,6 +51,31 @@ public class AccountSettingsActivity extends AppCompatActivity { } }); + + binding.accountSettingsDeleteAccount.setOnClickListener(v -> deleteAccount(account)); + } + + + private void deleteAccount(Account account) { + new MaterialDialog.Builder(this) + .title(R.string.delete_account_question) + .positiveText(R.string.validate) + .negativeText(R.string.cancel) + .onPositive(((dialog, which) -> viewModel.delete(account) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(new DisposableCompletableObserver() { + @Override + public void onComplete() { + finish(); + } + + @Override + public void onError(Throwable e) { + Toast.makeText(AccountSettingsActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show(); + } + }))) + .show(); } @Override diff --git a/app/src/main/java/com/readrops/app/activities/MainActivity.java b/app/src/main/java/com/readrops/app/activities/MainActivity.java index 93c73995..40ac422d 100644 --- a/app/src/main/java/com/readrops/app/activities/MainActivity.java +++ b/app/src/main/java/com/readrops/app/activities/MainActivity.java @@ -46,6 +46,7 @@ import com.readrops.app.utils.SharedPreferencesManager; import com.readrops.app.viewmodels.MainViewModel; import com.readrops.app.views.MainItemListAdapter; +import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -137,58 +138,58 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou }); drawerManager.setHeaderListener((view, profile, current) -> { - if (!current) { - int id = (int) profile.getIdentifier(); + if (!current) { + int id = (int) profile.getIdentifier(); - switch (id) { - case DrawerManager.ADD_ACCOUNT_ID: - Intent intent = new Intent(this, AccountTypeListActivity.class); - intent.putExtra("fromMainActivity", true); - startActivityForResult(intent, ADD_ACCOUNT_REQUEST); - break; - case DrawerManager.ACCOUNT_SETTINGS_ID: - break; - default: - if (!updating) { - viewModel.setCurrentAccount(id); - updateDrawerFeeds(); - } - break; - } - } else { - Intent intent = new Intent(this, AccountSettingsActivity.class); - intent.putExtra(AccountSettingsActivity.ACCOUNT, viewModel.getCurrentAccount()); - startActivity(intent); + switch (id) { + case DrawerManager.ADD_ACCOUNT_ID: + Intent intent = new Intent(this, AccountTypeListActivity.class); + intent.putExtra("fromMainActivity", true); + startActivityForResult(intent, ADD_ACCOUNT_REQUEST); + break; + case DrawerManager.ACCOUNT_SETTINGS_ID: + break; + default: + if (!updating) { + viewModel.setCurrentAccount(id); + updateDrawerFeeds(); + } + break; } - - return true; - }); - - Account currentAccount = getIntent().getParcelableExtra(ACCOUNT_KEY); - - if (currentAccount != null) { // first account created - List accounts = new ArrayList<>(); - accounts.add(currentAccount); - - viewModel.setAccounts(accounts); - - drawer = drawerManager.buildDrawer(accounts); - - if (!viewModel.isAccountLocal()) { - refreshLayout.setRefreshing(true); - onRefresh(); + } else { + Intent intent = new Intent(this, AccountSettingsActivity.class); + intent.putExtra(AccountSettingsActivity.ACCOUNT, viewModel.getCurrentAccount()); + startActivity(intent); } - } else { // last current account - viewModel.getAllAccounts().observe(this, accounts -> { - if (viewModel.getCurrentAccount() == null) { - viewModel.setAccounts(accounts); + return true; + }); - drawer = drawerManager.buildDrawer(accounts); - updateDrawerFeeds(); - } - }); - } + Account currentAccount = getIntent().getParcelableExtra(MainActivity.ACCOUNT_KEY); + WeakReference accountWeakReference = new WeakReference<>(currentAccount); + + viewModel.getAllAccounts().observe(this, accounts -> { + viewModel.setAccounts(accounts); + + if (drawer == null) { + drawer = drawerManager.buildDrawer(accounts); + updateDrawerFeeds(); + } else if (accounts.size() < drawerManager.getNumberOfProfiles() && accounts.size() > 0) { + drawerManager.updateHeader(accounts); + updateDrawerFeeds(); + } else if (accounts.size() == 0) { + Intent intent = new Intent(this, AccountTypeListActivity.class); + startActivity(intent); + finish(); + } + + if (accountWeakReference.get() != null && !(accountWeakReference.get().getAccountType() == Account.AccountType.LOCAL)) { + refreshLayout.setRefreshing(true); + onRefresh(); + accountWeakReference.clear(); + } + + }); } private void handleDrawerClick(IDrawerItem drawerItem) { @@ -502,7 +503,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou } drawerManager.resetItems(); - drawerManager.addAccount(newAccount); + drawerManager.addAccount(newAccount, true); } } @@ -576,7 +577,6 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou } - @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.item_list_menu, menu); diff --git a/app/src/main/java/com/readrops/app/database/dao/AccountDao.java b/app/src/main/java/com/readrops/app/database/dao/AccountDao.java index 0e814f1a..f6f4d62d 100644 --- a/app/src/main/java/com/readrops/app/database/dao/AccountDao.java +++ b/app/src/main/java/com/readrops/app/database/dao/AccountDao.java @@ -2,6 +2,7 @@ package com.readrops.app.database.dao; import androidx.lifecycle.LiveData; import androidx.room.Dao; +import androidx.room.Delete; import androidx.room.Insert; import androidx.room.Query; import androidx.room.Update; @@ -28,6 +29,9 @@ public interface AccountDao { @Update void update(Account account); + @Delete + void delete(Account account); + @Query("Update Account set last_modified = :lastModified Where id = :accountId") void updateLastModified(int accountId, long lastModified); diff --git a/app/src/main/java/com/readrops/app/database/entities/Account.java b/app/src/main/java/com/readrops/app/database/entities/Account.java index e76554e2..0a10c062 100644 --- a/app/src/main/java/com/readrops/app/database/entities/Account.java +++ b/app/src/main/java/com/readrops/app/database/entities/Account.java @@ -4,6 +4,7 @@ import android.os.Parcel; import android.os.Parcelable; import androidx.annotation.DrawableRes; +import androidx.annotation.Nullable; import androidx.annotation.StringRes; import androidx.room.ColumnInfo; import androidx.room.Entity; @@ -149,7 +150,7 @@ public class Account implements Parcelable { } public String getLoginKey() { - return accountType.name() + "_login_" + id ; + return accountType.name() + "_login_" + id; } public String getPasswordKey() { @@ -180,8 +181,10 @@ public class Account implements Parcelable { FEEDLY(R.drawable.ic_feedly, R.string.feedly), FRESHRSS(R.drawable.ic_freshrss, R.string.freshrss); - private @DrawableRes int iconRes; - private @StringRes int name; + private @DrawableRes + int iconRes; + private @StringRes + int name; @Override public void writeToParcel(Parcel dest, int flags) { @@ -205,11 +208,13 @@ public class Account implements Parcelable { } }; - public @DrawableRes int getIconRes() { + public @DrawableRes + int getIconRes() { return iconRes; } - public @StringRes int getName() { + public @StringRes + int getName() { return name; } @@ -222,4 +227,14 @@ public class Account implements Parcelable { public Credentials toCredentials() { return new Credentials(login, password, url); } + + @Override + public boolean equals(@Nullable Object obj) { + if (obj == null) + return false; + else if (!(obj instanceof Account)) + return false; + else + return this.id == ((Account) obj).getId(); + } } diff --git a/app/src/main/java/com/readrops/app/utils/DrawerManager.java b/app/src/main/java/com/readrops/app/utils/DrawerManager.java index e11eb16b..9c5cc0b8 100644 --- a/app/src/main/java/com/readrops/app/utils/DrawerManager.java +++ b/app/src/main/java/com/readrops/app/utils/DrawerManager.java @@ -212,11 +212,33 @@ public class DrawerManager { drawer.addItem(new DividerDrawerItem()); } - public void addAccount(Account account) { + public void addAccount(Account account, boolean currentProfile) { ProfileDrawerItem profileItem = createProfileItem(account); header.addProfiles(profileItem); - header.setActiveProfile(profileItem.getIdentifier()); + + if (currentProfile) + header.setActiveProfile(profileItem.getIdentifier()); + } + + public void updateHeader(List accounts) { + header.clear(); + + for (Account account : accounts) { + addAccount(account, account.isCurrentAccount()); + } + } + + public int getNumberOfProfiles() { + List profiles = header.getProfiles(); + + int number = 0; + for (IProfile profile : profiles) { + if (profile instanceof ProfileDrawerItem) + number++; + } + + return number; } public void resetItems() { diff --git a/app/src/main/java/com/readrops/app/viewmodels/AccountViewModel.java b/app/src/main/java/com/readrops/app/viewmodels/AccountViewModel.java index a13d611a..96e3a051 100644 --- a/app/src/main/java/com/readrops/app/viewmodels/AccountViewModel.java +++ b/app/src/main/java/com/readrops/app/viewmodels/AccountViewModel.java @@ -44,6 +44,13 @@ public class AccountViewModel extends AndroidViewModel { }); } + public Completable delete(Account account) { + return Completable.create(emitter -> { + database.accountDao().delete(account); + emitter.onComplete(); + }); + } + public Single getAccountCountByAccountType(int accountTypeCode) { return Single.create(emitter -> emitter.onSuccess(database.accountDao().getAccountCountByType(accountTypeCode))); } diff --git a/app/src/main/java/com/readrops/app/viewmodels/MainViewModel.java b/app/src/main/java/com/readrops/app/viewmodels/MainViewModel.java index 90aaef86..729ac527 100644 --- a/app/src/main/java/com/readrops/app/viewmodels/MainViewModel.java +++ b/app/src/main/java/com/readrops/app/viewmodels/MainViewModel.java @@ -218,15 +218,24 @@ public class MainViewModel extends AndroidViewModel { public void setAccounts(List accounts) { this.accounts = accounts; + boolean currentAccountExists = false; + for (Account account1 : accounts) { if (account1.isCurrentAccount()) { currentAccount = account1; + currentAccountExists = true; + setRepository(currentAccount.getAccountType()); queryBuilder.setAccountId(currentAccount.getId()); buildPagedList(); break; } } + + if (!currentAccountExists && accounts.size() > 0) { + setCurrentAccount(accounts.get(0)); + accounts.get(0).setCurrentAccount(true); + } } public boolean isAccountLocal() { diff --git a/app/src/main/res/layout/activity_account_settings.xml b/app/src/main/res/layout/activity_account_settings.xml index dbfe6b87..f88892f6 100644 --- a/app/src/main/res/layout/activity_account_settings.xml +++ b/app/src/main/res/layout/activity_account_settings.xml @@ -28,7 +28,7 @@ @@ -49,6 +49,29 @@ android:text="@string/account" /> + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-fr-rFR/strings.xml b/app/src/main/res/values-fr-rFR/strings.xml index 0f4e2fda..4ad8150b 100644 --- a/app/src/main/res/values-fr-rFR/strings.xml +++ b/app/src/main/res/values-fr-rFR/strings.xml @@ -66,5 +66,7 @@ Flux Modifier le dossier Supprimer le dossier ? + Supprimer le compte + Supprimer le compte ? \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f30b8166..36a43800 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -71,4 +71,6 @@ Feeds Edit folder Delete folder ? + Delete account + Delete account ?