Add account deletion action

This commit is contained in:
Shinokuni 2019-07-23 21:54:43 +02:00
parent db0d689f10
commit a9fc9799fc
10 changed files with 177 additions and 57 deletions

View File

@ -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

View File

@ -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<Account> 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<Account> 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);

View File

@ -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);

View File

@ -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();
}
}

View File

@ -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<Account> accounts) {
header.clear();
for (Account account : accounts) {
addAccount(account, account.isCurrentAccount());
}
}
public int getNumberOfProfiles() {
List<IProfile> profiles = header.getProfiles();
int number = 0;
for (IProfile profile : profiles) {
if (profile instanceof ProfileDrawerItem)
number++;
}
return number;
}
public void resetItems() {

View File

@ -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<Integer> getAccountCountByAccountType(int accountTypeCode) {
return Single.create(emitter -> emitter.onSuccess(database.accountDao().getAccountCountByType(accountTypeCode)));
}

View File

@ -218,15 +218,24 @@ public class MainViewModel extends AndroidViewModel {
public void setAccounts(List<Account> 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() {

View File

@ -28,7 +28,7 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_height="0.5dp"
android:background="@drawable/cell_separator"
app:layout_constraintBottom_toTopOf="@id/account_settings_account"
app:layout_constraintTop_toBottomOf="@id/account_settings_feeds" />
@ -49,6 +49,29 @@
android:text="@string/account" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@drawable/cell_separator"
app:layout_constraintBottom_toTopOf="@id/account_settings_delete_account"
app:layout_constraintTop_toBottomOf="@id/account_settings_account" />
<RelativeLayout
android:id="@+id/account_settings_delete_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:padding="10dp"
app:layout_constraintTop_toBottomOf="@id/account_settings_account">
<TextView
style="@style/TextAppearance.AppCompat.Subhead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="@string/delete_account" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

View File

@ -66,5 +66,7 @@
<string name="feeds">Flux</string>
<string name="edit_folder">Modifier le dossier</string>
<string name="delete_folder">Supprimer le dossier ?</string>
<string name="delete_account">Supprimer le compte</string>
<string name="delete_account_question">Supprimer le compte ?</string>
</resources>

View File

@ -71,4 +71,6 @@
<string name="feeds">Feeds</string>
<string name="edit_folder">Edit folder</string>
<string name="delete_folder">Delete folder ?</string>
<string name="delete_account">Delete account</string>
<string name="delete_account_question">Delete account ?</string>
</resources>