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.os.Bundle;
22 import android.support.annotation.NonNull;
23 import android.support.annotation.Nullable;
24 import android.support.design.widget.CollapsingToolbarLayout;
25 import android.support.design.widget.FloatingActionButton;
26 import android.support.design.widget.TextInputLayout;
27 import android.support.v4.app.Fragment;
28 import android.text.Editable;
29 import android.text.TextWatcher;
30 import android.util.Log;
31 import android.view.LayoutInflater;
32 import android.view.Menu;
33 import android.view.MenuInflater;
34 import android.view.MenuItem;
35 import android.view.View;
36 import android.view.ViewGroup;
37 import android.widget.LinearLayout;
38 import android.widget.Switch;
39 import android.widget.TextView;
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.activity.ProfileListActivity;
47 * A fragment representing a single Profile detail screen.
48 * This fragment is either contained in a {@link ProfileListActivity}
49 * in two-pane mode (on tablets) or a {@link ProfileDetailActivity}
52 public class ProfileDetailFragment extends Fragment {
54 * The fragment argument representing the item ID that this fragment
57 public static final String ARG_ITEM_ID = "item_id";
60 * The dummy content this fragment is presenting.
62 private MobileLedgerProfile mProfile;
64 private Switch postingPermitted;
65 private TextInputLayout urlLayout;
66 private LinearLayout authParams;
67 private Switch useAuthentication;
68 private TextView userName;
69 private TextInputLayout userNameLayout;
70 private TextView password;
71 private TextInputLayout passwordLayout;
72 private TextView profileName;
73 private TextInputLayout profileNameLayout;
74 private FloatingActionButton fab;
77 * Mandatory empty constructor for the fragment manager to instantiate the
78 * fragment (e.g. upon screen orientation changes).
80 public ProfileDetailFragment() {
83 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
84 Log.d("profiles", "[fragment] Creating profile details options menu");
85 super.onCreateOptionsMenu(menu, inflater);
86 inflater.inflate(R.menu.profile_details, menu);
87 final MenuItem menuDeleteProfile = menu.findItem(R.id.menuDelete);
88 menuDeleteProfile.setOnMenuItemClickListener(item -> {
89 Log.d("profiles", String.format("[fragment] removing profile %s", mProfile.getUuid()));
90 mProfile.removeFromDB();
91 Data.profiles.remove(mProfile);
92 if (Data.profile.get().equals(mProfile)) {
93 Log.d("profiles", "[fragment] setting current profile to 0");
94 Data.setCurrentProfile(Data.profiles.get(0));
98 menuDeleteProfile.setVisible((mProfile != null) && (Data.profiles.size() > 1));
101 public void onCreate(Bundle savedInstanceState) {
102 super.onCreate(savedInstanceState);
104 if ((getArguments() != null) && getArguments().containsKey(ARG_ITEM_ID)) {
105 int index = getArguments().getInt(ARG_ITEM_ID, -1);
106 if (index != -1) mProfile = Data.profiles.get(index);
108 Activity activity = this.getActivity();
109 if (activity == null) throw new AssertionError();
110 CollapsingToolbarLayout appBarLayout = activity.findViewById(R.id.toolbar_layout);
111 if (appBarLayout != null) {
112 if (mProfile != null) appBarLayout.setTitle(mProfile.getName());
113 else appBarLayout.setTitle(getResources().getString(R.string.new_profile_title));
118 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
119 super.onActivityCreated(savedInstanceState);
120 Activity context = getActivity();
121 if (context == null) return;
123 fab = context.findViewById(R.id.fab);
124 fab.setOnClickListener(v -> {
125 if (!checkValidity()) return;
127 if (mProfile != null) {
128 mProfile.setName(profileName.getText());
129 mProfile.setUrl(url.getText());
130 mProfile.setPostingPermitted(postingPermitted.isChecked());
131 mProfile.setAuthEnabled(useAuthentication.isChecked());
132 mProfile.setAuthUserName(userName.getText());
133 mProfile.setAuthPassword(password.getText());
134 mProfile.storeInDB();
135 Log.d("profiles", "profile stored in DB");
136 Data.profiles.triggerItemChangedNotification(mProfile);
139 if (mProfile.getUuid().equals(Data.profile.get().getUuid())) {
140 // dummy update to notify the observers of the possibly new name/URL
141 Data.profile.set(mProfile);
145 mProfile = new MobileLedgerProfile(profileName.getText(), postingPermitted.isChecked(),
146 url.getText(), useAuthentication.isChecked(), userName.getText(),
148 mProfile.storeInDB();
149 Data.profiles.add(mProfile);
150 MobileLedgerProfile.storeProfilesOrder();
152 // first profile ever?
153 if (Data.profiles.getList().size() == 1) Data.profile.set(mProfile);
156 Activity activity = getActivity();
157 if (activity != null) activity.finish();
160 profileName.requestFocus();
163 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
164 Bundle savedInstanceState) {
165 View rootView = inflater.inflate(R.layout.profile_detail, container, false);
167 profileName = rootView.findViewById(R.id.profile_name);
168 profileNameLayout = rootView.findViewById(R.id.profile_name_layout);
169 url = rootView.findViewById(R.id.url);
170 urlLayout = rootView.findViewById(R.id.url_layout);
171 postingPermitted = rootView.findViewById(R.id.profile_permit_posting);
172 authParams = rootView.findViewById(R.id.auth_params);
173 useAuthentication = rootView.findViewById(R.id.enable_http_auth);
174 userName = rootView.findViewById(R.id.auth_user_name);
175 userNameLayout = rootView.findViewById(R.id.auth_user_name_layout);
176 password = rootView.findViewById(R.id.password);
177 passwordLayout = rootView.findViewById(R.id.password_layout);
179 useAuthentication.setOnCheckedChangeListener((buttonView, isChecked) -> {
180 Log.d("profiles", isChecked ? "auth enabled " : "auth disabled");
181 authParams.setVisibility(isChecked ? View.VISIBLE : View.GONE);
182 if (isChecked) userName.requestFocus();
185 hookClearErrorOnFocusListener(profileName, profileNameLayout);
186 hookClearErrorOnFocusListener(url, urlLayout);
187 hookClearErrorOnFocusListener(userName, userNameLayout);
188 hookClearErrorOnFocusListener(password, passwordLayout);
190 if (mProfile != null) {
191 profileName.setText(mProfile.getName());
192 postingPermitted.setChecked(mProfile.isPostingPermitted());
193 url.setText(mProfile.getUrl());
194 useAuthentication.setChecked(mProfile.isAuthEnabled());
195 authParams.setVisibility(mProfile.isAuthEnabled() ? View.VISIBLE : View.GONE);
196 userName.setText(mProfile.isAuthEnabled() ? mProfile.getAuthUserName() : "");
197 password.setText(mProfile.isAuthEnabled() ? mProfile.getAuthPassword() : "");
200 profileName.setText("");
202 postingPermitted.setChecked(true);
203 useAuthentication.setChecked(false);
204 authParams.setVisibility(View.GONE);
205 userName.setText("");
206 password.setText("");
211 private void hookClearErrorOnFocusListener(TextView view, TextInputLayout layout) {
212 view.setOnFocusChangeListener((v, hasFocus) -> {
213 if (hasFocus) layout.setError(null);
215 view.addTextChangedListener(new TextWatcher() {
217 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
220 public void onTextChanged(CharSequence s, int start, int before, int count) {
221 layout.setError(null);
224 public void afterTextChanged(Editable s) {
228 boolean checkValidity() {
229 boolean valid = true;
231 String val = String.valueOf(profileName.getText());
232 if (val.trim().isEmpty()) {
234 profileNameLayout.setError(getResources().getText(R.string.err_profile_name_empty));
237 val = String.valueOf(url.getText());
238 if (val.trim().isEmpty()) {
240 urlLayout.setError(getResources().getText(R.string.err_profile_url_empty));
242 if (useAuthentication.isChecked()) {
243 val = String.valueOf(userName.getText());
244 if (val.trim().isEmpty()) {
247 .setError(getResources().getText(R.string.err_profile_user_name_empty));
250 val = String.valueOf(password.getText());
251 if (val.trim().isEmpty()) {
254 .setError(getResources().getText(R.string.err_profile_password_empty));