2 * Copyright © 2019 Damyan Ivanov.
3 * This file is part of Mobile-Ledger.
4 * Mobile-Ledger 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 * Mobile-Ledger 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 Mobile-Ledger. 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 TextInputLayout urlLayout;
65 private LinearLayout authParams;
66 private Switch useAuthentication;
67 private TextView userName;
68 private TextInputLayout userNameLayout;
69 private TextView password;
70 private TextInputLayout passwordLayout;
71 private TextView profileName;
72 private TextInputLayout profileNameLayout;
73 private FloatingActionButton fab;
76 * Mandatory empty constructor for the fragment manager to instantiate the
77 * fragment (e.g. upon screen orientation changes).
79 public ProfileDetailFragment() {
82 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
83 Log.d("profiles", "[fragment] Creating profile details options menu");
84 super.onCreateOptionsMenu(menu, inflater);
85 inflater.inflate(R.menu.profile_details, menu);
86 final MenuItem menuDeleteProfile = menu.findItem(R.id.menuDelete);
87 menuDeleteProfile.setOnMenuItemClickListener(item -> {
88 Log.d("profiles", String.format("[fragment] removing profile %s", mProfile.getUuid()));
89 mProfile.removeFromDB();
90 Data.profiles.remove(mProfile);
91 if (Data.profile.get().equals(mProfile)) {
92 Log.d("profiles", "[fragment] setting current profile to 0");
93 Data.setCurrentProfile(Data.profiles.get(0));
97 menuDeleteProfile.setVisible((mProfile != null) && (Data.profiles.size() > 1));
100 public void onCreate(Bundle savedInstanceState) {
101 super.onCreate(savedInstanceState);
103 if ((getArguments() != null) && getArguments().containsKey(ARG_ITEM_ID)) {
104 int index = getArguments().getInt(ARG_ITEM_ID, -1);
105 if (index != -1) mProfile = Data.profiles.get(index);
107 Activity activity = this.getActivity();
108 if (activity == null) throw new AssertionError();
109 CollapsingToolbarLayout appBarLayout = activity.findViewById(R.id.toolbar_layout);
110 if (appBarLayout != null) {
111 if (mProfile != null) appBarLayout.setTitle(mProfile.getName());
112 else appBarLayout.setTitle(getResources().getString(R.string.new_profile_title));
117 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
118 super.onActivityCreated(savedInstanceState);
119 Activity context = getActivity();
120 if (context == null) return;
122 fab = context.findViewById(R.id.fab);
123 fab.setOnClickListener(v -> {
124 if (!checkValidity()) return;
126 if (mProfile != null) {
127 mProfile.setName(profileName.getText());
128 mProfile.setUrl(url.getText());
129 mProfile.setAuthEnabled(useAuthentication.isChecked());
130 mProfile.setAuthUserName(userName.getText());
131 mProfile.setAuthPassword(password.getText());
132 mProfile.storeInDB();
133 Log.d("profiles", "profile stored in DB");
134 Data.profiles.triggerItemChangedNotification(mProfile);
137 if (mProfile.getUuid().equals(Data.profile.get().getUuid())) {
138 // dummy update to notify the observers of the possibly new name/URL
139 Data.profile.set(mProfile);
143 mProfile = new MobileLedgerProfile(profileName.getText(), url.getText(),
144 useAuthentication.isChecked(), userName.getText(), password.getText());
145 mProfile.storeInDB();
146 Data.profiles.add(mProfile);
147 MobileLedgerProfile.storeProfilesOrder();
149 // first profile ever?
150 if (Data.profiles.getList().size() == 1) Data.profile.set(mProfile);
153 Activity activity = getActivity();
154 if (activity != null) activity.finish();
157 profileName.requestFocus();
160 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
161 Bundle savedInstanceState) {
162 View rootView = inflater.inflate(R.layout.profile_detail, container, false);
164 profileName = rootView.findViewById(R.id.profile_name);
165 profileNameLayout = rootView.findViewById(R.id.profile_name_layout);
166 url = rootView.findViewById(R.id.url);
167 urlLayout = rootView.findViewById(R.id.url_layout);
168 authParams = rootView.findViewById(R.id.auth_params);
169 useAuthentication = rootView.findViewById(R.id.enable_http_auth);
170 userName = rootView.findViewById(R.id.auth_user_name);
171 userNameLayout = rootView.findViewById(R.id.auth_user_name_layout);
172 password = rootView.findViewById(R.id.password);
173 passwordLayout = rootView.findViewById(R.id.password_layout);
175 useAuthentication.setOnCheckedChangeListener((buttonView, isChecked) -> {
176 Log.d("profiles", isChecked ? "auth enabled " : "auth disabled");
177 authParams.setVisibility(isChecked ? View.VISIBLE : View.GONE);
178 if (isChecked) userName.requestFocus();
181 hookClearErrorOnFocusListener(profileName, profileNameLayout);
182 hookClearErrorOnFocusListener(url, urlLayout);
183 hookClearErrorOnFocusListener(userName, userNameLayout);
184 hookClearErrorOnFocusListener(password, passwordLayout);
186 if (mProfile != null) {
187 profileName.setText(mProfile.getName());
188 url.setText(mProfile.getUrl());
189 useAuthentication.setChecked(mProfile.isAuthEnabled());
190 authParams.setVisibility(mProfile.isAuthEnabled() ? View.VISIBLE : View.GONE);
191 userName.setText(mProfile.isAuthEnabled() ? mProfile.getAuthUserName() : "");
192 password.setText(mProfile.isAuthEnabled() ? mProfile.getAuthPassword() : "");
195 profileName.setText("");
197 useAuthentication.setChecked(false);
198 authParams.setVisibility(View.GONE);
199 userName.setText("");
200 password.setText("");
205 private void hookClearErrorOnFocusListener(TextView view, TextInputLayout layout) {
206 view.setOnFocusChangeListener((v, hasFocus) -> {
207 if (hasFocus) layout.setError(null);
209 view.addTextChangedListener(new TextWatcher() {
211 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
214 public void onTextChanged(CharSequence s, int start, int before, int count) {
215 layout.setError(null);
218 public void afterTextChanged(Editable s) {
222 boolean checkValidity() {
223 boolean valid = true;
225 String val = String.valueOf(profileName.getText());
226 if (val.trim().isEmpty()) {
228 profileNameLayout.setError(getResources().getText(R.string.err_profile_name_empty));
231 val = String.valueOf(url.getText());
232 if (val.trim().isEmpty()) {
234 urlLayout.setError(getResources().getText(R.string.err_profile_url_empty));
236 if (useAuthentication.isChecked()) {
237 val = String.valueOf(userName.getText());
238 if (val.trim().isEmpty()) {
241 .setError(getResources().getText(R.string.err_profile_user_name_empty));
244 val = String.valueOf(password.getText());
245 if (val.trim().isEmpty()) {
248 .setError(getResources().getText(R.string.err_profile_password_empty));