improved async task dialog

This commit is contained in:
Mariotaku Lee 2017-05-16 10:55:27 +08:00
parent 68b34edecf
commit 85cafaf654
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
4 changed files with 80 additions and 105 deletions

View File

@ -1,94 +0,0 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2014 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.preference;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.support.v7.preference.Preference;
import android.util.AttributeSet;
import org.mariotaku.twidere.R;
public abstract class AsyncTaskPreference extends Preference {
private Task mTask;
public AsyncTaskPreference(final Context context) {
this(context, null);
}
public AsyncTaskPreference(final Context context, final AttributeSet attrs) {
this(context, attrs, R.attr.preferenceStyle);
}
public AsyncTaskPreference(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onClick() {
if (mTask == null || mTask.getStatus() != Status.RUNNING) {
mTask = new Task(this);
mTask.execute();
}
}
protected abstract void doInBackground();
private static class Task extends AsyncTask<Object, Object, Object> {
private final AsyncTaskPreference mPreference;
private final Context mContext;
private final ProgressDialog mProgress;
public Task(final AsyncTaskPreference preference) {
mPreference = preference;
mContext = preference.getContext();
mProgress = new ProgressDialog(mContext);
}
@Override
protected Object doInBackground(final Object... args) {
mPreference.doInBackground();
return null;
}
@Override
protected void onPostExecute(final Object result) {
if (mProgress.isShowing()) {
mProgress.dismiss();
}
}
@Override
protected void onPreExecute() {
if (mProgress.isShowing()) {
mProgress.dismiss();
}
mProgress.setMessage(mContext.getString(R.string.message_please_wait));
mProgress.setCancelable(false);
mProgress.show();
}
}
}

View File

@ -0,0 +1,76 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.preference
import android.content.Context
import android.os.AsyncTask
import android.os.AsyncTask.Status
import android.support.v7.preference.Preference
import android.util.AttributeSet
import org.mariotaku.chameleon.ChameleonUtils
import org.mariotaku.ktextension.dismissDialogFragment
import org.mariotaku.twidere.activity.iface.IBaseActivity
import org.mariotaku.twidere.fragment.ProgressDialogFragment
import java.lang.ref.WeakReference
abstract class AsyncTaskPreference(context: Context, attrs: AttributeSet? = null) :
Preference(context, attrs) {
private var task: InternalTask? = null
override fun onClick() {
if (task?.status != Status.RUNNING) {
task = InternalTask(this).apply { execute() }
}
}
protected abstract fun doInBackground()
private class InternalTask(preference: AsyncTaskPreference) : AsyncTask<Any, Any, Unit>() {
private val preferenceRef = WeakReference<AsyncTaskPreference>(preference)
override fun doInBackground(vararg args: Any) {
val preference = preferenceRef.get() ?: return
preference.doInBackground()
}
override fun onPostExecute(result: Unit) {
val context = preferenceRef.get()?.context ?: return
val activity = ChameleonUtils.getActivity(context) as? IBaseActivity<*> ?: return
activity.executeAfterFragmentResumed {
it.supportFragmentManager.dismissDialogFragment(FRAGMENT_TAG)
}
}
override fun onPreExecute() {
val context = preferenceRef.get()?.context ?: return
val activity = ChameleonUtils.getActivity(context) as? IBaseActivity<*> ?: return
activity.executeAfterFragmentResumed {
ProgressDialogFragment.show(it.supportFragmentManager, FRAGMENT_TAG)
}
}
companion object {
private const val FRAGMENT_TAG = "task_progress"
}
}
}

View File

@ -21,13 +21,9 @@ package org.mariotaku.twidere.preference
import android.content.Context
import android.util.AttributeSet
import org.mariotaku.twidere.R
class ClearCachePreference @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = R.attr.preferenceStyle
) : AsyncTaskPreference(context, attrs, defStyle) {
class ClearCachePreference(context: Context, attrs: AttributeSet? = null) :
AsyncTaskPreference(context, attrs) {
override fun doInBackground() {
val context = context ?: return

View File

@ -21,15 +21,12 @@ package org.mariotaku.twidere.preference
import android.content.Context
import android.util.AttributeSet
import org.mariotaku.twidere.R
import org.mariotaku.twidere.TwidereConstants.TIMELINE_POSITIONS_PREFERENCES_NAME
import org.mariotaku.twidere.provider.TwidereDataStore.*
import org.mariotaku.twidere.util.DataStoreUtils
class ClearDatabasesPreference(
context: Context,
attrs: AttributeSet? = null
) : AsyncTaskPreference(context, attrs, R.attr.preferenceStyle) {
class ClearDatabasesPreference(context: Context, attrs: AttributeSet? = null) :
AsyncTaskPreference(context, attrs) {
override fun doInBackground() {
val context = context ?: return