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

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

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

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<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_height="match_parent"
android:background="?attr/asContentBackground"
@ -8,15 +7,7 @@
<include layout="@layout/toolbar"/>
<com.rey.material.widget.ProgressView
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"/>
<include layout="@layout/progress_view"/>
<ScrollView
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