Use dialog fragment to select account

This commit is contained in:
M66B 2019-07-01 16:46:40 +02:00
parent 53a71bd632
commit ba242ac0b6
2 changed files with 65 additions and 45 deletions

View File

@ -10,6 +10,8 @@ import androidx.fragment.app.Fragment;
import static android.app.Activity.RESULT_CANCELED; import static android.app.Activity.RESULT_CANCELED;
public class DialogFragmentEx extends DialogFragment { public class DialogFragmentEx extends DialogFragment {
private boolean once = false;
@Override @Override
public void onDismiss(@NonNull DialogInterface dialog) { public void onDismiss(@NonNull DialogInterface dialog) {
super.onDismiss(dialog); super.onDismiss(dialog);
@ -17,11 +19,15 @@ public class DialogFragmentEx extends DialogFragment {
} }
protected void sendResult(int result) { protected void sendResult(int result) {
Fragment target = getTargetFragment(); if (!once) {
if (target != null) { once = true;
Intent data = new Intent(); Fragment target = getTargetFragment();
data.putExtra("args", getArguments()); Log.i("Dialog target=" + target + " result=" + result);
target.onActivityResult(getTargetRequestCode(), result, null); if (target != null) {
Intent data = new Intent();
data.putExtra("args", getArguments());
target.onActivityResult(getTargetRequestCode(), result, data);
}
} }
} }
} }

View File

@ -81,7 +81,6 @@ import androidx.appcompat.widget.PopupMenu;
import androidx.appcompat.widget.SearchView; import androidx.appcompat.widget.SearchView;
import androidx.constraintlayout.widget.Group; import androidx.constraintlayout.widget.Group;
import androidx.documentfile.provider.DocumentFile; import androidx.documentfile.provider.DocumentFile;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction; import androidx.fragment.app.FragmentTransaction;
@ -254,6 +253,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
private static final int REQUEST_MESSAGES_MOVE = 16; private static final int REQUEST_MESSAGES_MOVE = 16;
static final int REQUEST_PRINT = 17; static final int REQUEST_PRINT = 17;
private static final int REQUEST_SEARCH = 18; private static final int REQUEST_SEARCH = 18;
private static final int REQUEST_ACCOUNT = 19;
static final String ACTION_STORE_RAW = BuildConfig.APPLICATION_ID + ".STORE_RAW"; static final String ACTION_STORE_RAW = BuildConfig.APPLICATION_ID + ".STORE_RAW";
static final String ACTION_STORE_ATTACHMENT = BuildConfig.APPLICATION_ID + ".STORE_ATTACHMENT"; static final String ACTION_STORE_ATTACHMENT = BuildConfig.APPLICATION_ID + ".STORE_ATTACHMENT";
@ -2274,37 +2274,10 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
ib.setOnLongClickListener(new View.OnLongClickListener() { ib.setOnLongClickListener(new View.OnLongClickListener() {
@Override @Override
public boolean onLongClick(View v) { public boolean onLongClick(View v) {
Bundle args = new Bundle(); FragmentDialogAccount fragment = new FragmentDialogAccount();
fragment.setArguments(new Bundle());
new SimpleTask<List<EntityAccount>>() { fragment.setTargetFragment(FragmentMessages.this, REQUEST_ACCOUNT);
@Override fragment.show(getFragmentManager(), "messages:accounts");
protected List<EntityAccount> onExecute(Context context, Bundle args) {
DB db = DB.getInstance(context);
return db.account().getSynchronizingAccounts();
}
@Override
protected void onExecuted(Bundle args, List<EntityAccount> accounts) {
final ArrayAdapter<EntityAccount> adapter =
new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1, accounts);
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner())
.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
EntityAccount account = adapter.getItem(which);
onMenuFolders(account.id);
}
})
.show();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(getFragmentManager(), ex);
}
}.execute(FragmentMessages.this, args, "messages:accounts");
return true; return true;
} }
}); });
@ -3376,10 +3349,8 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
} }
break; break;
case REQUEST_PRINT: case REQUEST_PRINT:
if (resultCode == RESULT_OK && data != null) { if (resultCode == RESULT_OK && data != null)
Bundle args = data.getBundleExtra("args"); onPrint(data.getBundleExtra("args"));
onPrint(args.getLong("id"));
}
break; break;
case REQUEST_SEARCH: case REQUEST_SEARCH:
if (resultCode == RESULT_OK && data != null) { if (resultCode == RESULT_OK && data != null) {
@ -3389,6 +3360,12 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
args.getLong("folder"), true, args.getString("query")); args.getLong("folder"), true, args.getString("query"));
} }
break; break;
case REQUEST_ACCOUNT:
if (resultCode == RESULT_OK && data != null) {
Bundle args = data.getBundleExtra("args");
onMenuFolders(args.getLong("account"));
}
break;
} }
} }
@ -4118,9 +4095,9 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
private WebView printWebView = null; private WebView printWebView = null;
private void onPrint(long id) { private void onPrint(Bundle args) {
Bundle args = new Bundle(); Bundle pargs = new Bundle();
args.putLong("id", id); pargs.putLong("id", args.getLong("id"));
new SimpleTask<String[]>() { new SimpleTask<String[]>() {
@Override @Override
@ -4224,7 +4201,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
protected void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(getFragmentManager(), ex); Helper.unexpectedError(getFragmentManager(), ex);
} }
}.execute(this, args, "message:print"); }.execute(this, pargs, "message:print");
} }
static void search( static void search(
@ -4361,6 +4338,43 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
} }
} }
public static class FragmentDialogAccount extends DialogFragmentEx {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
final ArrayAdapter<EntityAccount> adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1);
new SimpleTask<List<EntityAccount>>() {
@Override
protected List<EntityAccount> onExecute(Context context, Bundle args) {
DB db = DB.getInstance(context);
return db.account().getSynchronizingAccounts();
}
@Override
protected void onExecuted(Bundle args, List<EntityAccount> accounts) {
adapter.addAll(accounts);
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(getFragmentManager(), ex);
}
}.execute(getContext(), getActivity(), new Bundle(), "messages:accounts");
return new AlertDialog.Builder(getContext())
.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EntityAccount account = adapter.getItem(which);
getArguments().putLong("account", account.id);
sendResult(RESULT_OK);
}
})
.create();
}
}
public static class FragmentDialogError extends DialogFragmentEx { public static class FragmentDialogError extends DialogFragmentEx {
@NonNull @NonNull
@Override @Override