]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java
improvement in the color selection UI
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / profiles / ProfileDetailFragment.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.ui.profiles;
19
20 import android.app.Activity;
21 import android.app.AlertDialog;
22 import android.content.DialogInterface;
23 import android.os.Bundle;
24 import android.text.Editable;
25 import android.text.TextWatcher;
26 import android.util.Log;
27 import android.view.LayoutInflater;
28 import android.view.Menu;
29 import android.view.MenuInflater;
30 import android.view.MenuItem;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.widget.LinearLayout;
34 import android.widget.Switch;
35 import android.widget.TextView;
36
37 import com.google.android.material.appbar.CollapsingToolbarLayout;
38 import com.google.android.material.floatingactionbutton.FloatingActionButton;
39 import com.google.android.material.textfield.TextInputLayout;
40
41 import net.ktnx.mobileledger.BuildConfig;
42 import net.ktnx.mobileledger.R;
43 import net.ktnx.mobileledger.model.Data;
44 import net.ktnx.mobileledger.model.MobileLedgerProfile;
45 import net.ktnx.mobileledger.ui.HueRingDialog;
46 import net.ktnx.mobileledger.ui.activity.ProfileDetailActivity;
47 import net.ktnx.mobileledger.utils.Colors;
48
49 import java.util.Objects;
50
51 import androidx.annotation.NonNull;
52 import androidx.annotation.Nullable;
53 import androidx.fragment.app.Fragment;
54
55 /**
56  * A fragment representing a single Profile detail screen.
57  * a {@link ProfileDetailActivity}
58  * on handsets.
59  */
60 public class ProfileDetailFragment extends Fragment implements HueRingDialog.HueSelectedListener {
61     /**
62      * The fragment argument representing the item ID that this fragment
63      * represents.
64      */
65     public static final String ARG_ITEM_ID = "item_id";
66
67     /**
68      * The dummy content this fragment is presenting.
69      */
70     private MobileLedgerProfile mProfile;
71     private TextView url;
72     private Switch postingPermitted;
73     private TextInputLayout urlLayout;
74     private LinearLayout authParams;
75     private Switch useAuthentication;
76     private TextView userName;
77     private TextInputLayout userNameLayout;
78     private TextView password;
79     private TextInputLayout passwordLayout;
80     private TextView profileName;
81     private TextInputLayout profileNameLayout;
82     private TextView preferredAccountsFilter;
83     private TextInputLayout preferredAccountsFilterLayout;
84     private View huePickerView;
85
86     /**
87      * Mandatory empty constructor for the fragment manager to instantiate the
88      * fragment (e.g. upon screen orientation changes).
89      */
90     public ProfileDetailFragment() {
91     }
92     @Override
93     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
94         Log.d("profiles", "[fragment] Creating profile details options menu");
95         super.onCreateOptionsMenu(menu, inflater);
96         inflater.inflate(R.menu.profile_details, menu);
97         final MenuItem menuDeleteProfile = menu.findItem(R.id.menuDelete);
98         menuDeleteProfile.setOnMenuItemClickListener(item -> {
99             AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
100             builder.setTitle(mProfile.getName());
101             builder.setMessage(R.string.remove_profile_dialog_message);
102             builder.setPositiveButton(R.string.Remove, new DialogInterface.OnClickListener() {
103                 @Override
104                 public void onClick(DialogInterface dialog, int which) {
105                     Log.d("profiles",
106                             String.format("[fragment] removing profile %s", mProfile.getUuid()));
107                     mProfile.removeFromDB();
108                     Data.profiles.remove(mProfile);
109                     if (Data.profile.get().equals(mProfile)) {
110                         Log.d("profiles", "[fragment] setting current profile to 0");
111                         Data.setCurrentProfile(Data.profiles.get(0));
112                     }
113                     getActivity().finish();
114                 }
115             });
116             builder.show();
117             return false;
118         });
119         menuDeleteProfile.setVisible((mProfile != null) && (Data.profiles.size() > 1));
120
121         if (BuildConfig.DEBUG) {
122             final MenuItem menuWipeProfileData = menu.findItem(R.id.menuWipeData);
123             menuWipeProfileData.setOnMenuItemClickListener(this::onWipeDataMenuClicked);
124             menuWipeProfileData.setVisible(mProfile != null);
125         }
126     }
127     private boolean onWipeDataMenuClicked(MenuItem item) {
128         // this is a development option, so no confirmation
129         mProfile.wipeAllData();
130         Data.profile.forceNotifyObservers();
131         return true;
132     }
133     @Override
134     public void onCreate(Bundle savedInstanceState) {
135         super.onCreate(savedInstanceState);
136
137         if ((getArguments() != null) && getArguments().containsKey(ARG_ITEM_ID)) {
138             int index = getArguments().getInt(ARG_ITEM_ID, -1);
139             if (index != -1) mProfile = Data.profiles.get(index);
140
141             Activity activity = this.getActivity();
142             if (activity == null) throw new AssertionError();
143             CollapsingToolbarLayout appBarLayout = activity.findViewById(R.id.toolbar_layout);
144             if (appBarLayout != null) {
145                 if (mProfile != null) appBarLayout.setTitle(mProfile.getName());
146                 else appBarLayout.setTitle(getResources().getString(R.string.new_profile_title));
147             }
148         }
149     }
150     @Override
151     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
152         super.onActivityCreated(savedInstanceState);
153         Activity context = getActivity();
154         if (context == null) return;
155
156         FloatingActionButton fab = context.findViewById(R.id.fab);
157         fab.setOnClickListener(v -> {
158             if (!checkValidity()) return;
159
160             if (mProfile != null) {
161                 updateProfileFromUI();
162 //                Log.d("profiles", String.format("Selected item is %d", mProfile.getThemeId()));
163                 mProfile.storeInDB();
164                 Log.d("profiles", "profile stored in DB");
165                 Data.profiles.triggerItemChangedNotification(mProfile);
166
167
168                 if (mProfile.getUuid().equals(Data.profile.get().getUuid())) {
169                     // dummy update to notify the observers of the possibly new name/URL
170                     Data.profile.set(mProfile);
171                 }
172             }
173             else {
174                 mProfile = new MobileLedgerProfile();
175                 updateProfileFromUI();
176                 mProfile.storeInDB();
177                 Data.profiles.add(mProfile);
178                 MobileLedgerProfile.storeProfilesOrder();
179
180                 // first profile ever?
181                 if (Data.profiles.size() == 1) Data.profile.set(mProfile);
182             }
183
184             Activity activity = getActivity();
185             if (activity != null) activity.finish();
186         });
187
188         profileName.requestFocus();
189     }
190     private void updateProfileFromUI() {
191         mProfile.setName(profileName.getText());
192         mProfile.setUrl(url.getText());
193         mProfile.setPostingPermitted(postingPermitted.isChecked());
194         mProfile.setPreferredAccountsFilter(preferredAccountsFilter.getText());
195         mProfile.setAuthEnabled(useAuthentication.isChecked());
196         mProfile.setAuthUserName(userName.getText());
197         mProfile.setAuthPassword(password.getText());
198         mProfile.setThemeId(huePickerView.getTag());
199     }
200     @Override
201     public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
202                              Bundle savedInstanceState) {
203         View rootView = inflater.inflate(R.layout.profile_detail, container, false);
204
205         profileName = rootView.findViewById(R.id.profile_name);
206         profileNameLayout = rootView.findViewById(R.id.profile_name_layout);
207         url = rootView.findViewById(R.id.url);
208         urlLayout = rootView.findViewById(R.id.url_layout);
209         postingPermitted = rootView.findViewById(R.id.profile_permit_posting);
210         authParams = rootView.findViewById(R.id.auth_params);
211         useAuthentication = rootView.findViewById(R.id.enable_http_auth);
212         userName = rootView.findViewById(R.id.auth_user_name);
213         userNameLayout = rootView.findViewById(R.id.auth_user_name_layout);
214         password = rootView.findViewById(R.id.password);
215         passwordLayout = rootView.findViewById(R.id.password_layout);
216         huePickerView = rootView.findViewById(R.id.btn_pick_ring_color);
217         preferredAccountsFilter = rootView.findViewById(R.id.preferred_accounts_filter_filter);
218         preferredAccountsFilterLayout =
219                 rootView.findViewById(R.id.preferred_accounts_accounts_filter_layout);
220
221         useAuthentication.setOnCheckedChangeListener((buttonView, isChecked) -> {
222             Log.d("profiles", isChecked ? "auth enabled " : "auth disabled");
223             authParams.setVisibility(isChecked ? View.VISIBLE : View.GONE);
224             if (isChecked) userName.requestFocus();
225         });
226
227         postingPermitted.setOnCheckedChangeListener(((buttonView, isChecked) -> {
228             preferredAccountsFilterLayout.setVisibility(isChecked ? View.VISIBLE : View.GONE);
229         }));
230
231         hookClearErrorOnFocusListener(profileName, profileNameLayout);
232         hookClearErrorOnFocusListener(url, urlLayout);
233         hookClearErrorOnFocusListener(userName, userNameLayout);
234         hookClearErrorOnFocusListener(password, passwordLayout);
235
236         int profileThemeId;
237         if (mProfile != null) {
238             profileName.setText(mProfile.getName());
239             postingPermitted.setChecked(mProfile.isPostingPermitted());
240             url.setText(mProfile.getUrl());
241             useAuthentication.setChecked(mProfile.isAuthEnabled());
242             authParams.setVisibility(mProfile.isAuthEnabled() ? View.VISIBLE : View.GONE);
243             userName.setText(mProfile.isAuthEnabled() ? mProfile.getAuthUserName() : "");
244             password.setText(mProfile.isAuthEnabled() ? mProfile.getAuthPassword() : "");
245             preferredAccountsFilter.setText(mProfile.getPreferredAccountsFilter());
246             profileThemeId = mProfile.getThemeId();
247         }
248         else {
249             profileName.setText("");
250             url.setText("https://");
251             postingPermitted.setChecked(true);
252             useAuthentication.setChecked(false);
253             authParams.setVisibility(View.GONE);
254             userName.setText("");
255             password.setText("");
256             preferredAccountsFilter.setText(null);
257             profileThemeId = -1;
258         }
259
260         final int hue = (profileThemeId == -1) ? Colors.DEFAULT_HUE_DEG : profileThemeId;
261         final int profileColor = Colors.getPrimaryColorForHue(hue);
262
263         huePickerView.setBackgroundColor(profileColor);
264         huePickerView.setTag(profileThemeId);
265         huePickerView.setOnClickListener(v -> {
266             HueRingDialog d = new HueRingDialog(
267                     Objects.requireNonNull(ProfileDetailFragment.this.getContext()),
268                     profileThemeId, (Integer) v.getTag());
269             d.show();
270             d.setColorSelectedListener(this);
271         });
272         return rootView;
273     }
274     private void hookClearErrorOnFocusListener(TextView view, TextInputLayout layout) {
275         view.setOnFocusChangeListener((v, hasFocus) -> {
276             if (hasFocus) layout.setError(null);
277         });
278         view.addTextChangedListener(new TextWatcher() {
279             @Override
280             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
281             }
282             @Override
283             public void onTextChanged(CharSequence s, int start, int before, int count) {
284                 layout.setError(null);
285             }
286             @Override
287             public void afterTextChanged(Editable s) {
288             }
289         });
290     }
291     private boolean checkValidity() {
292         boolean valid = true;
293
294         String val = String.valueOf(profileName.getText());
295         if (val.trim().isEmpty()) {
296             valid = false;
297             profileNameLayout.setError(getResources().getText(R.string.err_profile_name_empty));
298         }
299
300         val = String.valueOf(url.getText());
301         if (val.trim().isEmpty()) {
302             valid = false;
303             urlLayout.setError(getResources().getText(R.string.err_profile_url_empty));
304         }
305         if (useAuthentication.isChecked()) {
306             val = String.valueOf(userName.getText());
307             if (val.trim().isEmpty()) {
308                 valid = false;
309                 userNameLayout
310                         .setError(getResources().getText(R.string.err_profile_user_name_empty));
311             }
312
313             val = String.valueOf(password.getText());
314             if (val.trim().isEmpty()) {
315                 valid = false;
316                 passwordLayout
317                         .setError(getResources().getText(R.string.err_profile_password_empty));
318             }
319         }
320
321         return valid;
322     }
323     @Override
324     public void onHueSelected(int hue) {
325         huePickerView.setBackgroundColor(Colors.getPrimaryColorForHue(hue));
326         huePickerView.setTag(hue);
327     }
328 }