Replace progress dialogs with bars

For CaldavCalendarSettingsActivity
pull/795/head
Alex Baker 6 years ago
parent 6c11e07938
commit 6469fc8bea

@ -0,0 +1,12 @@
package org.tasks.activities;
import org.tasks.caldav.CaldavClient;
import org.tasks.data.CaldavAccount;
import org.tasks.security.Encryption;
import org.tasks.ui.CompletableViewModel;
public class CreateCalendarViewModel extends CompletableViewModel<String> {
public void createCalendar(CaldavAccount caldavAccount, Encryption encryption, String name) {
run(() -> new CaldavClient(caldavAccount, encryption).makeCollection(name));
}
}

@ -0,0 +1,14 @@
package org.tasks.activities;
import org.tasks.caldav.CaldavClient;
import org.tasks.data.CaldavAccount;
import org.tasks.data.CaldavCalendar;
import org.tasks.security.Encryption;
import org.tasks.ui.ActionViewModel;
public class DeleteCalendarViewModel extends ActionViewModel {
public void deleteCalendar(
CaldavAccount caldavAccount, Encryption encryption, CaldavCalendar calendar) {
run(() -> new CaldavClient(caldavAccount, calendar, encryption).deleteCollection());
}
}

@ -2,7 +2,6 @@ package org.tasks.caldav;
import static android.text.TextUtils.isEmpty; import static android.text.TextUtils.isEmpty;
import android.app.ProgressDialog;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
@ -14,6 +13,7 @@ import android.widget.LinearLayout;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import androidx.appcompat.widget.Toolbar.OnMenuItemClickListener; import androidx.appcompat.widget.Toolbar.OnMenuItemClickListener;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProviders;
import at.bitfire.dav4android.exception.HttpException; import at.bitfire.dav4android.exception.HttpException;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
@ -23,6 +23,7 @@ import butterknife.OnTextChanged;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.textfield.TextInputEditText; import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout; import com.google.android.material.textfield.TextInputLayout;
import com.rey.material.widget.ProgressView;
import com.todoroo.astrid.activity.MainActivity; import com.todoroo.astrid.activity.MainActivity;
import com.todoroo.astrid.activity.TaskListFragment; import com.todoroo.astrid.activity.TaskListFragment;
import com.todoroo.astrid.api.CaldavFilter; import com.todoroo.astrid.api.CaldavFilter;
@ -32,6 +33,8 @@ import java.net.ConnectException;
import javax.inject.Inject; import javax.inject.Inject;
import org.tasks.R; import org.tasks.R;
import org.tasks.activities.ColorPickerActivity; import org.tasks.activities.ColorPickerActivity;
import org.tasks.activities.CreateCalendarViewModel;
import org.tasks.activities.DeleteCalendarViewModel;
import org.tasks.analytics.Tracker; import org.tasks.analytics.Tracker;
import org.tasks.analytics.Tracking; import org.tasks.analytics.Tracking;
import org.tasks.analytics.Tracking.Events; import org.tasks.analytics.Tracking.Events;
@ -81,9 +84,14 @@ public class CaldavCalendarSettingsActivity extends ThemedInjectingAppCompatActi
@BindView(R.id.toolbar) @BindView(R.id.toolbar)
Toolbar toolbar; Toolbar toolbar;
@BindView(R.id.progress_bar)
ProgressView progressView;
private CaldavCalendar caldavCalendar; private CaldavCalendar caldavCalendar;
private CaldavAccount caldavAccount; private CaldavAccount caldavAccount;
private int selectedTheme = -1; private int selectedTheme = -1;
private CreateCalendarViewModel createCalendarViewModel;
private DeleteCalendarViewModel deleteCalendarViewModel;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -93,6 +101,9 @@ public class CaldavCalendarSettingsActivity extends ThemedInjectingAppCompatActi
ButterKnife.bind(this); ButterKnife.bind(this);
createCalendarViewModel = ViewModelProviders.of(this).get(CreateCalendarViewModel.class);
deleteCalendarViewModel = ViewModelProviders.of(this).get(DeleteCalendarViewModel.class);
Intent intent = getIntent(); Intent intent = getIntent();
caldavCalendar = intent.getParcelableExtra(EXTRA_CALDAV_CALENDAR); caldavCalendar = intent.getParcelableExtra(EXTRA_CALDAV_CALENDAR);
if (caldavCalendar == null) { if (caldavCalendar == null) {
@ -147,6 +158,11 @@ public class CaldavCalendarSettingsActivity extends ThemedInjectingAppCompatActi
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(name, InputMethodManager.SHOW_IMPLICIT); imm.showSoftInput(name, InputMethodManager.SHOW_IMPLICIT);
} }
createCalendarViewModel.getData().observe(this, this::createSuccessful);
deleteCalendarViewModel.getData().observe(this, this::onDeleted);
createCalendarViewModel.getError().observe(this, this::requestFailed);
deleteCalendarViewModel.getError().observe(this, this::requestFailed);
} }
@Override @Override
@ -184,6 +200,10 @@ public class CaldavCalendarSettingsActivity extends ThemedInjectingAppCompatActi
} }
private void save() { private void save() {
if (requestInProgress()) {
return;
}
String name = getNewName(); String name = getNewName();
boolean failed = false; boolean failed = false;
@ -204,13 +224,8 @@ public class CaldavCalendarSettingsActivity extends ThemedInjectingAppCompatActi
} }
if (caldavCalendar == null) { if (caldavCalendar == null) {
CaldavClient client = new CaldavClient(caldavAccount, encryption); showProgressIndicator();
ProgressDialog dialog = dialogBuilder.newProgressDialog(R.string.contacting_server); createCalendarViewModel.createCalendar(caldavAccount, encryption, name);
dialog.show();
client
.makeCollection(name)
.doAfterTerminate(dialog::dismiss)
.subscribe(this::createSuccessful, this::requestFailed);
} else if (hasChanges()) { } else if (hasChanges()) {
updateAccount(); updateAccount();
} else { } else {
@ -218,7 +233,21 @@ public class CaldavCalendarSettingsActivity extends ThemedInjectingAppCompatActi
} }
} }
private void showProgressIndicator() {
progressView.setVisibility(View.VISIBLE);
}
private void hideProgressIndicator() {
progressView.setVisibility(View.GONE);
}
private boolean requestInProgress() {
return progressView.getVisibility() == View.VISIBLE;
}
private void requestFailed(Throwable t) { private void requestFailed(Throwable t) {
hideProgressIndicator();
if (t instanceof HttpException) { if (t instanceof HttpException) {
showSnackbar(t.getMessage()); showSnackbar(t.getMessage());
} else if (t instanceof DisplayableException) { } else if (t instanceof DisplayableException) {
@ -317,6 +346,10 @@ public class CaldavCalendarSettingsActivity extends ThemedInjectingAppCompatActi
} }
private void discard() { private void discard() {
if (requestInProgress()) {
return;
}
if (!hasChanges()) { if (!hasChanges()) {
finish(); finish();
} else { } else {
@ -352,29 +385,28 @@ public class CaldavCalendarSettingsActivity extends ThemedInjectingAppCompatActi
} }
private void deleteCollection() { private void deleteCollection() {
if (requestInProgress()) {
return;
}
dialogBuilder dialogBuilder
.newMessageDialog(R.string.delete_tag_confirmation, caldavCalendar.getName()) .newMessageDialog(R.string.delete_tag_confirmation, caldavCalendar.getName())
.setPositiveButton( .setPositiveButton(
R.string.delete, R.string.delete,
(dialog, which) -> { (dialog, which) -> {
CaldavClient caldavClient = showProgressIndicator();
new CaldavClient(caldavAccount, caldavCalendar, encryption); deleteCalendarViewModel.deleteCalendar(caldavAccount, encryption, caldavCalendar);
ProgressDialog progressDialog =
dialogBuilder.newProgressDialog(R.string.contacting_server);
progressDialog.show();
caldavClient
.deleteCollection()
.doAfterTerminate(progressDialog::dismiss)
.subscribe(this::onDeleted, this::requestFailed);
}) })
.setNegativeButton(android.R.string.cancel, null) .setNegativeButton(android.R.string.cancel, null)
.show(); .show();
} }
private void onDeleted() { private void onDeleted(boolean deleted) {
if (deleted) {
taskDeleter.delete(caldavCalendar); taskDeleter.delete(caldavCalendar);
tracker.reportEvent(Events.CALDAV_LIST_DELETED); tracker.reportEvent(Events.CALDAV_LIST_DELETED);
setResult(RESULT_OK, new Intent(TaskListFragment.ACTION_DELETED)); setResult(RESULT_OK, new Intent(TaskListFragment.ACTION_DELETED));
finish(); finish();
} }
} }
}

@ -19,7 +19,6 @@ import at.bitfire.dav4android.property.GetCTag;
import at.bitfire.dav4android.property.ResourceType; import at.bitfire.dav4android.property.ResourceType;
import at.bitfire.dav4android.property.SupportedCalendarComponentSet; import at.bitfire.dav4android.property.SupportedCalendarComponentSet;
import com.todoroo.astrid.helper.UUIDHelper; import com.todoroo.astrid.helper.UUIDHelper;
import io.reactivex.Completable;
import io.reactivex.Single; import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
@ -40,19 +39,20 @@ import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer; import org.xmlpull.v1.XmlSerializer;
import timber.log.Timber; import timber.log.Timber;
class CaldavClient { public class CaldavClient {
private final OkHttpClient httpClient; private final OkHttpClient httpClient;
private final HttpUrl httpUrl; private final HttpUrl httpUrl;
CaldavClient(CaldavAccount caldavAccount, Encryption encryption) { public CaldavClient(CaldavAccount caldavAccount, Encryption encryption) {
this( this(
caldavAccount.getUrl(), caldavAccount.getUrl(),
caldavAccount.getUsername(), caldavAccount.getUsername(),
encryption.decrypt(caldavAccount.getPassword())); encryption.decrypt(caldavAccount.getPassword()));
} }
CaldavClient(CaldavAccount caldavAccount, CaldavCalendar caldavCalendar, Encryption encryption) { public CaldavClient(
CaldavAccount caldavAccount, CaldavCalendar caldavCalendar, Encryption encryption) {
this( this(
caldavCalendar.getUrl(), caldavCalendar.getUrl(),
caldavAccount.getUsername(), caldavAccount.getUsername(),
@ -162,23 +162,17 @@ class CaldavClient {
return urls; return urls;
} }
Completable deleteCollection() { public void deleteCollection() throws IOException, HttpException {
return Completable.fromAction(() -> new DavResource(httpClient, httpUrl).delete(null)) new DavResource(httpClient, httpUrl).delete(null);
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
} }
Single<String> makeCollection(String displayName) { public String makeCollection(String displayName)
return Single.fromCallable( throws IOException, XmlPullParserException, HttpException {
() -> {
DavResource davResource = DavResource davResource =
new DavResource(httpClient, httpUrl.resolve(UUIDHelper.newUUID() + "/")); new DavResource(httpClient, httpUrl.resolve(UUIDHelper.newUUID() + "/"));
String mkcolString = getMkcolString(displayName); String mkcolString = getMkcolString(displayName);
davResource.mkCol(mkcolString); davResource.mkCol(mkcolString);
return davResource.getLocation().toString(); return davResource.getLocation().toString();
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
} }
private String getMkcolString(String displayName) throws IOException, XmlPullParserException { private String getMkcolString(String displayName) throws IOException, XmlPullParserException {

@ -10,6 +10,8 @@
<include layout="@layout/toolbar"/> <include layout="@layout/toolbar"/>
<include layout="@layout/progress_view"/>
<ScrollView <ScrollView
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content">

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="?attr/asContentBackground" android:background="?attr/asContentBackground"
@ -8,15 +7,7 @@
<include layout="@layout/toolbar"/> <include layout="@layout/toolbar"/>
<com.rey.material.widget.ProgressView <include layout="@layout/progress_view"/>
android:id="@+id/progress_bar"
android:layout_width="fill_parent"
android:layout_height="4dp"
android:visibility="gone"
app:pv_autostart="true"
app:pv_circular="false"
app:pv_progressMode="indeterminate"
app:pv_progressStyle="@style/ProgressViewStyle"/>
<ScrollView <ScrollView
android:layout_width="fill_parent" android:layout_width="fill_parent"

@ -0,0 +1,11 @@
<com.rey.material.widget.ProgressView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/progress_bar"
android:layout_width="fill_parent"
android:layout_height="4dp"
android:visibility="gone"
app:pv_autostart="true"
app:pv_circular="false"
app:pv_progressMode="indeterminate"
app:pv_progressStyle="@style/ProgressViewStyle"/>
Loading…
Cancel
Save