2 * Copyright © 2019 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.ui.profiles;
20 import android.app.Activity;
21 import android.app.AlertDialog;
22 import android.os.Bundle;
23 import android.text.Editable;
24 import android.text.TextWatcher;
25 import android.view.LayoutInflater;
26 import android.view.Menu;
27 import android.view.MenuInflater;
28 import android.view.MenuItem;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.widget.LinearLayout;
32 import android.widget.PopupMenu;
33 import android.widget.Switch;
34 import android.widget.TextView;
36 import androidx.annotation.NonNull;
37 import androidx.annotation.Nullable;
38 import androidx.fragment.app.Fragment;
39 import androidx.fragment.app.FragmentActivity;
41 import com.google.android.material.appbar.CollapsingToolbarLayout;
42 import com.google.android.material.floatingactionbutton.FloatingActionButton;
43 import com.google.android.material.textfield.TextInputLayout;
45 import net.ktnx.mobileledger.BuildConfig;
46 import net.ktnx.mobileledger.R;
47 import net.ktnx.mobileledger.model.Data;
48 import net.ktnx.mobileledger.model.MobileLedgerProfile;
49 import net.ktnx.mobileledger.ui.HueRingDialog;
50 import net.ktnx.mobileledger.ui.activity.ProfileDetailActivity;
51 import net.ktnx.mobileledger.utils.Colors;
53 import org.jetbrains.annotations.NonNls;
54 import org.jetbrains.annotations.NotNull;
56 import java.net.MalformedURLException;
58 import java.util.ArrayList;
59 import java.util.Objects;
61 import static net.ktnx.mobileledger.utils.Logger.debug;
64 * A fragment representing a single Profile detail screen.
65 * a {@link ProfileDetailActivity}
68 public class ProfileDetailFragment extends Fragment implements HueRingDialog.HueSelectedListener {
70 * The fragment argument representing the item ID that this fragment
73 public static final String ARG_ITEM_ID = "item_id";
75 private static final String HTTPS_URL_START = "https://";
78 * The dummy content this fragment is presenting.
80 private MobileLedgerProfile mProfile;
82 private Switch postingPermitted;
83 private TextInputLayout urlLayout;
84 private LinearLayout authParams;
85 private Switch useAuthentication;
86 private TextView userName;
87 private TextInputLayout userNameLayout;
88 private TextView password;
89 private TextInputLayout passwordLayout;
90 private TextView profileName;
91 private TextInputLayout profileNameLayout;
92 private TextView preferredAccountsFilter;
93 private TextInputLayout preferredAccountsFilterLayout;
94 private View huePickerView;
95 private View insecureWarningText;
96 private TextView futureDatesText;
97 private MobileLedgerProfile.FutureDates futureDates;
98 private View futureDatesLayout;
101 * Mandatory empty constructor for the fragment manager to instantiate the
102 * fragment (e.g. upon screen orientation changes).
104 public ProfileDetailFragment() {
107 public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
108 debug("profiles", "[fragment] Creating profile details options menu");
109 super.onCreateOptionsMenu(menu, inflater);
110 inflater.inflate(R.menu.profile_details, menu);
111 final MenuItem menuDeleteProfile = menu.findItem(R.id.menuDelete);
112 menuDeleteProfile.setOnMenuItemClickListener(item -> {
113 AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
114 builder.setTitle(mProfile.getName());
115 builder.setMessage(R.string.remove_profile_dialog_message);
116 builder.setPositiveButton(R.string.Remove, (dialog, which) -> {
118 String.format("[fragment] removing profile %s", mProfile.getUuid()));
119 mProfile.removeFromDB();
120 ArrayList<MobileLedgerProfile> oldList = Data.profiles.getValue();
122 throw new AssertionError();
123 ArrayList<MobileLedgerProfile> newList = new ArrayList<>(oldList);
124 newList.remove(mProfile);
125 Data.profiles.setValue(newList);
126 if (mProfile.equals(Data.profile.getValue())) {
127 debug("profiles", "[fragment] setting current profile to 0");
128 Data.setCurrentProfile(newList.get(0));
131 final FragmentActivity activity = getActivity();
132 if (activity != null)
138 final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
139 menuDeleteProfile.setVisible(
140 (mProfile != null) && (profiles != null) && (profiles.size() > 1));
142 if (BuildConfig.DEBUG) {
143 final MenuItem menuWipeProfileData = menu.findItem(R.id.menuWipeData);
144 menuWipeProfileData.setOnMenuItemClickListener(ignored -> onWipeDataMenuClicked());
145 menuWipeProfileData.setVisible(mProfile != null);
148 private boolean onWipeDataMenuClicked() {
149 // this is a development option, so no confirmation
150 mProfile.wipeAllData();
151 if (mProfile.equals(Data.profile.getValue()))
152 triggerProfileChange();
155 private void triggerProfileChange() {
156 int index = Data.getProfileIndex(mProfile);
157 MobileLedgerProfile newProfile = new MobileLedgerProfile(mProfile);
158 final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
159 if (profiles == null)
160 throw new AssertionError();
161 profiles.set(index, newProfile);
163 ProfilesRecyclerViewAdapter prva = ProfilesRecyclerViewAdapter.getInstance();
165 prva.notifyItemChanged(index);
167 if (mProfile.equals(Data.profile.getValue()))
168 Data.profile.setValue(newProfile);
171 public void onCreate(Bundle savedInstanceState) {
172 super.onCreate(savedInstanceState);
174 if ((getArguments() != null) && getArguments().containsKey(ARG_ITEM_ID)) {
175 int index = getArguments().getInt(ARG_ITEM_ID, -1);
176 ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
177 if ((profiles != null) && (index != -1) && (index < profiles.size()))
178 mProfile = profiles.get(index);
180 Activity activity = this.getActivity();
181 if (activity == null)
182 throw new AssertionError();
183 CollapsingToolbarLayout appBarLayout = activity.findViewById(R.id.toolbar_layout);
184 if (appBarLayout != null) {
185 if (mProfile != null)
186 appBarLayout.setTitle(mProfile.getName());
188 appBarLayout.setTitle(getResources().getString(R.string.new_profile_title));
193 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
194 super.onActivityCreated(savedInstanceState);
195 Activity context = getActivity();
199 FloatingActionButton fab = context.findViewById(R.id.fab);
200 fab.setOnClickListener(v -> onSaveFabClicked());
202 profileName.requestFocus();
204 private void onSaveFabClicked() {
205 if (!checkValidity())
208 if (mProfile != null) {
209 updateProfileFromUI();
210 // debug("profiles", String.format("Selected item is %d", mProfile.getThemeId()));
211 mProfile.storeInDB();
212 debug("profiles", "profile stored in DB");
213 triggerProfileChange();
216 mProfile = new MobileLedgerProfile();
217 updateProfileFromUI();
218 mProfile.storeInDB();
219 final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
220 if (profiles == null)
221 throw new AssertionError();
222 ArrayList<MobileLedgerProfile> newList = new ArrayList<>(profiles);
223 newList.add(mProfile);
224 Data.profiles.setValue(newList);
225 MobileLedgerProfile.storeProfilesOrder();
227 // first profile ever?
228 if (newList.size() == 1)
229 Data.profile.setValue(mProfile);
232 Activity activity = getActivity();
233 if (activity != null)
236 private void updateProfileFromUI() {
237 mProfile.setName(profileName.getText());
238 mProfile.setUrl(url.getText());
239 mProfile.setPostingPermitted(postingPermitted.isChecked());
240 mProfile.setPreferredAccountsFilter(preferredAccountsFilter.getText());
241 mProfile.setAuthEnabled(useAuthentication.isChecked());
242 mProfile.setAuthUserName(userName.getText());
243 mProfile.setAuthPassword(password.getText());
244 mProfile.setThemeId(huePickerView.getTag());
245 mProfile.setFutureDates(futureDates);
248 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
249 Bundle savedInstanceState) {
250 View rootView = inflater.inflate(R.layout.profile_detail, container, false);
252 profileName = rootView.findViewById(R.id.profile_name);
253 profileNameLayout = rootView.findViewById(R.id.profile_name_layout);
254 url = rootView.findViewById(R.id.url);
255 urlLayout = rootView.findViewById(R.id.url_layout);
256 postingPermitted = rootView.findViewById(R.id.profile_permit_posting);
257 futureDatesLayout = rootView.findViewById(R.id.future_dates_layout);
258 futureDatesText = rootView.findViewById(R.id.future_dates_text);
259 rootView.findViewById(R.id.future_dates_layout)
260 .setOnClickListener(v -> {
261 MenuInflater mi = new MenuInflater(getContext());
262 PopupMenu menu = new PopupMenu(getContext(), v);
263 menu.inflate(R.menu.future_dates);
264 menu.setOnMenuItemClickListener(item -> {
265 switch (item.getItemId()) {
266 case R.id.menu_future_dates_30:
267 futureDates = MobileLedgerProfile.FutureDates.OneMonth;
269 case R.id.menu_future_dates_60:
270 futureDates = MobileLedgerProfile.FutureDates.TwoMonths;
272 case R.id.menu_future_dates_90:
273 futureDates = MobileLedgerProfile.FutureDates.ThreeMonths;
275 case R.id.menu_future_dates_180:
276 futureDates = MobileLedgerProfile.FutureDates.SixMonths;
278 case R.id.menu_future_dates_365:
279 futureDates = MobileLedgerProfile.FutureDates.OneYear;
281 case R.id.menu_future_dates_all:
282 futureDates = MobileLedgerProfile.FutureDates.All;
285 futureDates = MobileLedgerProfile.FutureDates.None;
287 futureDatesText.setText(futureDates.getText(getResources()));
292 authParams = rootView.findViewById(R.id.auth_params);
293 useAuthentication = rootView.findViewById(R.id.enable_http_auth);
294 userName = rootView.findViewById(R.id.auth_user_name);
295 userNameLayout = rootView.findViewById(R.id.auth_user_name_layout);
296 password = rootView.findViewById(R.id.password);
297 passwordLayout = rootView.findViewById(R.id.password_layout);
298 huePickerView = rootView.findViewById(R.id.btn_pick_ring_color);
299 preferredAccountsFilter = rootView.findViewById(R.id.preferred_accounts_filter_filter);
300 preferredAccountsFilterLayout =
301 rootView.findViewById(R.id.preferred_accounts_accounts_filter_layout);
302 insecureWarningText = rootView.findViewById(R.id.insecure_scheme_text);
304 useAuthentication.setOnCheckedChangeListener((buttonView, isChecked) -> {
305 debug("profiles", isChecked ? "auth enabled " : "auth disabled");
306 authParams.setVisibility(isChecked ? View.VISIBLE : View.GONE);
308 userName.requestFocus();
309 checkInsecureSchemeWithAuth();
312 postingPermitted.setOnCheckedChangeListener(((buttonView, isChecked) -> {
313 preferredAccountsFilterLayout.setVisibility(isChecked ? View.VISIBLE : View.GONE);
314 futureDatesLayout.setVisibility(isChecked ? View.VISIBLE : View.GONE);
317 hookClearErrorOnFocusListener(profileName, profileNameLayout);
318 hookClearErrorOnFocusListener(url, urlLayout);
319 hookClearErrorOnFocusListener(userName, userNameLayout);
320 hookClearErrorOnFocusListener(password, passwordLayout);
323 if (mProfile != null) {
324 profileName.setText(mProfile.getName());
325 postingPermitted.setChecked(mProfile.isPostingPermitted());
326 futureDates = mProfile.getFutureDates();
327 futureDatesText.setText(futureDates.getText(getResources()));
328 url.setText(mProfile.getUrl());
329 useAuthentication.setChecked(mProfile.isAuthEnabled());
330 authParams.setVisibility(mProfile.isAuthEnabled() ? View.VISIBLE : View.GONE);
331 userName.setText(mProfile.isAuthEnabled() ? mProfile.getAuthUserName() : "");
332 password.setText(mProfile.isAuthEnabled() ? mProfile.getAuthPassword() : "");
333 preferredAccountsFilter.setText(mProfile.getPreferredAccountsFilter());
334 profileThemeId = mProfile.getThemeId();
337 profileName.setText("");
338 url.setText(HTTPS_URL_START);
339 postingPermitted.setChecked(true);
340 futureDates = MobileLedgerProfile.FutureDates.None;
341 futureDatesText.setText(futureDates.getText(getResources()));
342 useAuthentication.setChecked(false);
343 authParams.setVisibility(View.GONE);
344 userName.setText("");
345 password.setText("");
346 preferredAccountsFilter.setText(null);
350 checkInsecureSchemeWithAuth();
352 url.addTextChangedListener(new TextWatcher() {
354 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
358 public void onTextChanged(CharSequence s, int start, int before, int count) {
362 public void afterTextChanged(Editable s) {
363 checkInsecureSchemeWithAuth();
367 final int hue = (profileThemeId == -1) ? Colors.DEFAULT_HUE_DEG : profileThemeId;
368 final int profileColor = Colors.getPrimaryColorForHue(hue);
370 huePickerView.setBackgroundColor(profileColor);
371 huePickerView.setTag(profileThemeId);
372 huePickerView.setOnClickListener(v -> {
373 HueRingDialog d = new HueRingDialog(
374 Objects.requireNonNull(ProfileDetailFragment.this.getContext()), profileThemeId,
375 (Integer) v.getTag());
377 d.setColorSelectedListener(this);
381 private boolean checkUrlValidity() {
382 boolean valid = true;
384 String val = String.valueOf(url.getText())
388 urlLayout.setError(getResources().getText(R.string.err_profile_url_empty));
391 URL url = new URL(val);
392 String host = url.getHost();
393 if (host == null || host.isEmpty())
394 throw new MalformedURLException("Missing host");
395 String protocol = url.getProtocol()
397 if (!protocol.equals("HTTP") && !protocol.equals("HTTPS")) {
399 urlLayout.setError(getResources().getText(R.string.err_invalid_url));
402 catch (MalformedURLException e) {
404 urlLayout.setError(getResources().getText(R.string.err_invalid_url));
409 private void checkInsecureSchemeWithAuth() {
410 boolean showWarning = false;
412 if (useAuthentication.isChecked()) {
413 String urlText = url.getText()
415 if (urlText.startsWith("http") && !urlText.startsWith("https"))
420 insecureWarningText.setVisibility(View.VISIBLE);
422 insecureWarningText.setVisibility(View.GONE);
424 private void hookClearErrorOnFocusListener(TextView view, TextInputLayout layout) {
425 view.setOnFocusChangeListener((v, hasFocus) -> {
427 layout.setError(null);
429 view.addTextChangedListener(new TextWatcher() {
431 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
434 public void onTextChanged(CharSequence s, int start, int before, int count) {
435 layout.setError(null);
438 public void afterTextChanged(Editable s) {
442 private boolean checkValidity() {
443 boolean valid = true;
445 String val = String.valueOf(profileName.getText());
450 profileNameLayout.setError(getResources().getText(R.string.err_profile_name_empty));
453 if (!checkUrlValidity())
456 if (useAuthentication.isChecked()) {
457 val = String.valueOf(userName.getText());
462 userNameLayout.setError(
463 getResources().getText(R.string.err_profile_user_name_empty));
466 val = String.valueOf(password.getText());
471 passwordLayout.setError(
472 getResources().getText(R.string.err_profile_password_empty));
479 public void onHueSelected(int hue) {
480 huePickerView.setBackgroundColor(Colors.getPrimaryColorForHue(hue));
481 huePickerView.setTag(hue);