From 654a00d6479fc7e8cdc538e2b1220bd97f40fea7 Mon Sep 17 00:00:00 2001 From: Gregor Santner Date: Thu, 6 Oct 2016 05:26:54 +0200 Subject: [PATCH] PodSelectionFragment --- .../data/DiasporaPodList.java | 17 ++ .../fragment/PodSelectionFragment.java | 127 +++++------- .../ui/PodSelectionDialog.java | 195 ++++++++++++++++++ .../main/res/layout/podselection__dialog.xml | 122 +++++++++++ .../res/layout/podselection__fragment.xml | 30 +-- app/src/main/res/values/strings.xml | 4 + app/src/main/res/values/styles.xml | 4 + 7 files changed, 411 insertions(+), 88 deletions(-) create mode 100644 app/src/main/java/com/github/dfa/diaspora_android/ui/PodSelectionDialog.java create mode 100644 app/src/main/res/layout/podselection__dialog.xml diff --git a/app/src/main/java/com/github/dfa/diaspora_android/data/DiasporaPodList.java b/app/src/main/java/com/github/dfa/diaspora_android/data/DiasporaPodList.java index 194fcf38..36216206 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/data/DiasporaPodList.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/data/DiasporaPodList.java @@ -416,6 +416,23 @@ public class DiasporaPodList implements Iterable, S return json; } + /** + * Set default values for https + */ + public void setHttpsDefaults(){ + setProtocol("https"); + setPort(443); + } + + + /** + * Set default values for http + */ + public void setHttpDefaults(){ + setProtocol("http"); + setPort(80); + } + /** * Tells if the ports needs to shown */ diff --git a/app/src/main/java/com/github/dfa/diaspora_android/fragment/PodSelectionFragment.java b/app/src/main/java/com/github/dfa/diaspora_android/fragment/PodSelectionFragment.java index accdfff2..132b8cb8 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/fragment/PodSelectionFragment.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/fragment/PodSelectionFragment.java @@ -1,9 +1,7 @@ package com.github.dfa.diaspora_android.fragment; -import android.app.AlertDialog; import android.content.BroadcastReceiver; import android.content.Context; -import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.os.Build; @@ -12,8 +10,6 @@ import android.support.design.widget.Snackbar; import android.support.v4.content.LocalBroadcastManager; import android.support.v4.view.MenuItemCompat; import android.support.v7.widget.SearchView; -import android.text.SpannableString; -import android.text.util.Linkify; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -32,10 +28,10 @@ import com.github.dfa.diaspora_android.data.AppSettings; import com.github.dfa.diaspora_android.data.DiasporaPodList; import com.github.dfa.diaspora_android.data.DiasporaPodList.DiasporaPod; import com.github.dfa.diaspora_android.task.GetPodsService; +import com.github.dfa.diaspora_android.ui.PodSelectionDialog; import com.github.dfa.diaspora_android.util.AppLog; import com.github.dfa.diaspora_android.util.DiasporaUrlHelper; import com.github.dfa.diaspora_android.util.Helpers; -import com.github.dfa.diaspora_android.util.WebHelper; import org.json.JSONException; import org.json.JSONObject; @@ -44,16 +40,17 @@ import java.util.ArrayList; import butterknife.BindView; import butterknife.ButterKnife; +import butterknife.OnClick; /** * Fragment that lets the user choose a Pod * Created by vanitas on 01.10.16. */ -public class PodSelectionFragment extends CustomFragment implements SearchView.OnQueryTextListener { +public class PodSelectionFragment extends CustomFragment implements SearchView.OnQueryTextListener, PodSelectionDialog.PodSelectionDialogResultListener { public static final String TAG = "com.github.dfa.diaspora_android.PodSelectionFragment"; - @BindView(R.id.podselection__listpods) + @BindView(R.id.podselection__fragment__listpods) protected ListView listViewPod; protected App app; @@ -85,9 +82,8 @@ public class PodSelectionFragment extends CustomFragment implements SearchView.O listViewPod.setTextFilterEnabled(true); listViewPod.setOnItemClickListener(new AdapterView.OnItemClickListener() { - @Override public void onItemClick(AdapterView adapterView, View view, int i, long l) { - showPodConfirmationDialog((String) listViewPod.getAdapter().getItem(i)); + showPodSelectionDialog(podList.getPodAt(i)); } }); LocalBroadcastManager.getInstance(getContext()).registerReceiver(podListReceiver, new IntentFilter(GetPodsService.MESSAGE_PODS_RECEIVED)); @@ -104,13 +100,9 @@ public class PodSelectionFragment extends CustomFragment implements SearchView.O } } - // Called when a pod was clicked (or custom) + @OnClick(R.id.podselection__fragment__button_use_custom_pod) public void onPodButtonClicked(View v) { - //if (editFilter.getText().length() > 4 && editFilter.getText().toString().contains("")) { - showPodConfirmationDialog(filterString); - //} else { - // Snackbar.make(listViewPod, R.string.valid_pod, Snackbar.LENGTH_LONG).show(); - //} + showPodSelectionDialog(new DiasporaPod()); } @Override @@ -118,16 +110,6 @@ public class PodSelectionFragment extends CustomFragment implements SearchView.O return TAG; } - @Override - public void onCreateBottomOptionsMenu(Menu menu, MenuInflater inflater) { - /* Nothing to do */ - } - - @Override - public boolean onBackPressed() { - return false; - } - private final BroadcastReceiver podListReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { @@ -175,49 +157,9 @@ public class PodSelectionFragment extends CustomFragment implements SearchView.O listViewPodAdapter.getFilter().filter(filterString); } - private void showPodConfirmationDialog(final String selectedPod) { - // Make a clickable link - final SpannableString dialogMessage = new SpannableString(getString(R.string.confirm_pod, selectedPod)); - Linkify.addLinks(dialogMessage, Linkify.ALL); - - // Check if online - if (!WebHelper.isOnline(getContext())) { - Snackbar.make(listViewPod, R.string.no_internet, Snackbar.LENGTH_LONG).show(); - return; - } - - // Show dialog - new AlertDialog.Builder(getContext()) - .setTitle(getString(R.string.confirmation)) - .setMessage(dialogMessage) - .setPositiveButton(android.R.string.yes, - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - onPodSelectionConfirmed(selectedPod); - } - }) - .setNegativeButton(android.R.string.no, null) - .show(); - } - - private void onPodSelectionConfirmed(String selectedPod) { - app.getSettings().setPodDomain(selectedPod); - - try { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - CookieManager.getInstance().removeAllCookies(null); - CookieManager.getInstance().removeSessionCookies(null); - } else { - //noinspection deprecation - CookieManager.getInstance().removeAllCookie(); - //noinspection deprecation - CookieManager.getInstance().removeSessionCookie(); - } - } catch (Exception e) { - e.printStackTrace(); - } - - ((MainActivity) getActivity()).openDiasporaUrl(new DiasporaUrlHelper(appSettings).getPodUrl()); + private void showPodSelectionDialog(final DiasporaPod selectedPod) { + PodSelectionDialog dialog = PodSelectionDialog.newInstance(selectedPod, this); + dialog.show(getFragmentManager(), PodSelectionDialog.TAG); } @Override @@ -253,11 +195,6 @@ public class PodSelectionFragment extends CustomFragment implements SearchView.O return super.onOptionsItemSelected(item); } - @Override - public boolean onQueryTextSubmit(String query) { - return false; - } - @Override public boolean onQueryTextChange(String newText) { if (listViewPodAdapter != null) { @@ -265,4 +202,48 @@ public class PodSelectionFragment extends CustomFragment implements SearchView.O } return true; } + + @Override + public void onPodSelectionDialogResult(DiasporaPod pod, boolean accepted) { + System.out.println(accepted + ": " + pod.toString()); + if (accepted) { + //TODO: Rework for new pod url system ;) + app.getSettings().setPodDomain(pod.getPodUrls().get(0).getHost()); + + try { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + CookieManager.getInstance().removeAllCookies(null); + CookieManager.getInstance().removeSessionCookies(null); + } else { + //noinspection deprecation + CookieManager.getInstance().removeAllCookie(); + //noinspection deprecation + CookieManager.getInstance().removeSessionCookie(); + } + } catch (Exception e) { + e.printStackTrace(); + } + + ((MainActivity) getActivity()).openDiasporaUrl(new DiasporaUrlHelper(appSettings).getPodUrl()); + } + } + + + /* + * Dummy implementations + */ + + @Override + public void onCreateBottomOptionsMenu(Menu menu, MenuInflater inflater) { + } + + @Override + public boolean onQueryTextSubmit(String query) { + return false; + } + + @Override + public boolean onBackPressed() { + return false; + } } \ No newline at end of file diff --git a/app/src/main/java/com/github/dfa/diaspora_android/ui/PodSelectionDialog.java b/app/src/main/java/com/github/dfa/diaspora_android/ui/PodSelectionDialog.java new file mode 100644 index 00000000..7120f75c --- /dev/null +++ b/app/src/main/java/com/github/dfa/diaspora_android/ui/PodSelectionDialog.java @@ -0,0 +1,195 @@ +package com.github.dfa.diaspora_android.ui; + + +import android.app.Dialog; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.v7.app.AlertDialog; +import android.support.v7.app.AppCompatDialogFragment; +import android.text.TextUtils; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.EditText; +import android.widget.RadioGroup; +import android.widget.Spinner; +import android.widget.TextView; + +import com.github.dfa.diaspora_android.R; +import com.github.dfa.diaspora_android.data.DiasporaPodList.DiasporaPod; +import com.github.dfa.diaspora_android.data.DiasporaPodList.DiasporaPod.DiasporaPodUrl; + +import org.json.JSONException; + +import java.util.List; + +import butterknife.BindView; +import butterknife.ButterKnife; +import butterknife.OnClick; +import butterknife.OnItemSelected; + +/** + * Created by gsantner (https://gsantner.github.io) on 06.10.16. + */ +public class PodSelectionDialog extends AppCompatDialogFragment { + public static final String TAG = "com.github.dfa.diaspora_android.PodSelectionDialog"; + + public static interface PodSelectionDialogResultListener { + void onPodSelectionDialogResult(DiasporaPod pod, boolean accepted); + } + + public static PodSelectionDialog newInstance(PodSelectionDialogResultListener resultListener) { + return newInstance(new DiasporaPod(), resultListener); + } + + public static PodSelectionDialog newInstance(DiasporaPod pod, PodSelectionDialogResultListener resultListener) { + PodSelectionDialog dialog = new PodSelectionDialog(); + dialog.setPod(pod); + dialog.setResultListener(resultListener); + return dialog; + } + + /* + // ██████╗ ██╗ █████╗ ██╗ ██████╗ ██████╗ + // ██╔══██╗██║██╔══██╗██║ ██╔═══██╗██╔════╝ + // ██║ ██║██║███████║██║ ██║ ██║██║ ███╗ + // ██║ ██║██║██╔══██║██║ ██║ ██║██║ ██║ + // ██████╔╝██║██║ ██║███████╗╚██████╔╝╚██████╔╝ + // ╚═════╝ ╚═╝╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚═════╝ + */ + + @BindView(R.id.podselection__dialog__edit_podaddress) + EditText editPodAddress; + + @BindView(R.id.podselection__dialog__edit_podname) + EditText editPodName; + + @BindView(R.id.podselection__dialog__radiogroup_protocol) + RadioGroup radiogrpProtocol; + + @BindView(R.id.podselection__dialog__text_profile) + TextView textProfile; + + @BindView(R.id.podselection__dialog__spinner_profile) + Spinner spinnerProfile; + + private PodSelectionDialogResultListener resultListener; + private View root; + private DiasporaPod pod = new DiasporaPod(); + + @NonNull + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + LayoutInflater inflater = getActivity().getLayoutInflater(); + + // Bind UI + root = inflater.inflate(R.layout.podselection__dialog, null); + ButterKnife.bind(this, root); + editPodName.setText(pod.getName()); + List podUrls = pod.getPodUrls(); + if (podUrls.size() > 0) { + uiLoadDiasporaUrl(0); + } + if (podUrls.size() > 1) { + textProfile.setVisibility(View.VISIBLE); + spinnerProfile.setVisibility(View.VISIBLE); + String[] podUrlss = new String[podUrls.size()]; + for (int i = 0; i < podUrls.size(); podUrlss[i] = podUrls.get(i++).getBaseUrl()) ; + ArrayAdapter spinnerAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_spinner_item, podUrlss); + spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + spinnerProfile.setAdapter(spinnerAdapter); + } + + + builder.setView(root); + return builder.create(); + } + + @OnItemSelected(R.id.podselection__dialog__spinner_profile) + public void spinnerItemSelected(Spinner spinner, int position) { + uiLoadDiasporaUrl(position); + } + + public void uiLoadDiasporaUrl(int wantedPodUrlPos) { + List podUrls = pod.getPodUrls(); + if (podUrls.size() == 0) { + return; + } + wantedPodUrlPos = wantedPodUrlPos < podUrls.size() ? wantedPodUrlPos : 0; + + DiasporaPodUrl url1 = podUrls.get(wantedPodUrlPos); + editPodAddress.setText(url1.getHost()); + radiogrpProtocol.check(url1.getProtocol().equals("https") + ? R.id.podselection__dialog__radio_https : R.id.podselection__dialog__radio_http); + } + + + @OnClick({R.id.podselection__dialog__btn_ok, R.id.podselection__dialog__btn_cancel}) + public void onResultButtonClicked(View view) { + boolean POSITIVE_PRESSED = view.getId() == R.id.podselection__dialog__btn_ok; + if (POSITIVE_PRESSED) { + if (!checkInputs()) { + return; + } + DiasporaPodUrl podUrl = new DiasporaPodUrl(); + if (radiogrpProtocol.getCheckedRadioButtonId() == R.id.podselection__dialog__radio_https) { + podUrl.setHttpsDefaults(); + } else { + podUrl.setHttpDefaults(); + } + podUrl.setHost(editPodAddress.getText().toString()); + pod.setName(editPodName.getText().toString()); + pod.getPodUrls().clear(); + pod.getPodUrls().add(podUrl); + getDialog().dismiss(); + publishResult(POSITIVE_PRESSED); + } else { + getDialog().cancel(); + publishResult(POSITIVE_PRESSED); + } + } + + public boolean checkInputs() { + boolean ok = true; + String s = editPodAddress.getText().toString(); + if(TextUtils.isEmpty(s) || s.length() < 3){ + editPodAddress.setError(getString(R.string.missing_value)); + ok = false; + } + s = editPodName.getText().toString(); + if(TextUtils.isEmpty(s) || s.length() < 3){ + editPodName.setError(getString(R.string.missing_value)); + ok = false; + } + return ok; + } + + public void publishResult(boolean accepted) { + if (resultListener != null) { + resultListener.onPodSelectionDialogResult(pod, accepted); + } + } + + /* + * GETTER & SETTER + */ + public PodSelectionDialogResultListener getResultListener() { + return resultListener; + } + + public void setResultListener(PodSelectionDialogResultListener resultListener) { + this.resultListener = resultListener; + } + + public DiasporaPod getPod() { + return pod; + } + + public void setPod(DiasporaPod pod) { + try { + this.pod = new DiasporaPod().fromJson(pod.toJson()); + } catch (JSONException ignored) { + } + } +} diff --git a/app/src/main/res/layout/podselection__dialog.xml b/app/src/main/res/layout/podselection__dialog.xml new file mode 100644 index 00000000..c2f6a736 --- /dev/null +++ b/app/src/main/res/layout/podselection__dialog.xml @@ -0,0 +1,122 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +