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