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 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.setAuthEnabled(useAuthentication.isChecked());
131 mProfile.setAuthUserName(userName.getText());
132 mProfile.setAuthPassword(password.getText());
133 mProfile.storeInDB();
134 Log.d("profiles", "profile stored in DB");
135 Data.profiles.triggerItemChangedNotification(mProfile);
138 if (mProfile.getUuid().equals(Data.profile.get().getUuid())) {
139 // dummy update to notify the observers of the possibly new name/URL
140 Data.profile.set(mProfile);
144 mProfile = new MobileLedgerProfile(profileName.getText(), postingPermitted.isChecked(),
145 url.getText(), useAuthentication.isChecked(), userName.getText(),
147 mProfile.storeInDB();
148 Data.profiles.add(mProfile);
149 MobileLedgerProfile.storeProfilesOrder();
151 // first profile ever?
152 if (Data.profiles.getList().size() == 1) Data.profile.set(mProfile);
155 Activity activity = getActivity();
156 if (activity != null) activity.finish();
159 profileName.requestFocus();
162 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
163 Bundle savedInstanceState) {
164 View rootView = inflater.inflate(R.layout.profile_detail, container, false);
166 profileName = rootView.findViewById(R.id.profile_name);
167 profileNameLayout = rootView.findViewById(R.id.profile_name_layout);
168 url = rootView.findViewById(R.id.url);
169 urlLayout = rootView.findViewById(R.id.url_layout);
170 postingPermitted = rootView.findViewById(R.id.profile_permit_posting);
171 authParams = rootView.findViewById(R.id.auth_params);
172 useAuthentication = rootView.findViewById(R.id.enable_http_auth);
173 userName = rootView.findViewById(R.id.auth_user_name);
174 userNameLayout = rootView.findViewById(R.id.auth_user_name_layout);
175 password = rootView.findViewById(R.id.password);
176 passwordLayout = rootView.findViewById(R.id.password_layout);
178 useAuthentication.setOnCheckedChangeListener((buttonView, isChecked) -> {
179 Log.d("profiles", isChecked ? "auth enabled " : "auth disabled");
180 authParams.setVisibility(isChecked ? View.VISIBLE : View.GONE);
181 if (isChecked) userName.requestFocus();
184 hookClearErrorOnFocusListener(profileName, profileNameLayout);
185 hookClearErrorOnFocusListener(url, urlLayout);
186 hookClearErrorOnFocusListener(userName, userNameLayout);
187 hookClearErrorOnFocusListener(password, passwordLayout);
189 if (mProfile != null) {
190 profileName.setText(mProfile.getName());
191 postingPermitted.setChecked(mProfile.isPostingPermitted());
192 url.setText(mProfile.getUrl());
193 useAuthentication.setChecked(mProfile.isAuthEnabled());
194 authParams.setVisibility(mProfile.isAuthEnabled() ? View.VISIBLE : View.GONE);
195 userName.setText(mProfile.isAuthEnabled() ? mProfile.getAuthUserName() : "");
196 password.setText(mProfile.isAuthEnabled() ? mProfile.getAuthPassword() : "");
199 profileName.setText("");
201 postingPermitted.setChecked(true);
202 useAuthentication.setChecked(false);
203 authParams.setVisibility(View.GONE);
204 userName.setText("");
205 password.setText("");
210 private void hookClearErrorOnFocusListener(TextView view, TextInputLayout layout) {
211 view.setOnFocusChangeListener((v, hasFocus) -> {
212 if (hasFocus) layout.setError(null);
214 view.addTextChangedListener(new TextWatcher() {
216 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
219 public void onTextChanged(CharSequence s, int start, int before, int count) {
220 layout.setError(null);
223 public void afterTextChanged(Editable s) {
227 boolean checkValidity() {
228 boolean valid = true;
230 String val = String.valueOf(profileName.getText());
231 if (val.trim().isEmpty()) {
233 profileNameLayout.setError(getResources().getText(R.string.err_profile_name_empty));
236 val = String.valueOf(url.getText());
237 if (val.trim().isEmpty()) {
239 urlLayout.setError(getResources().getText(R.string.err_profile_url_empty));
241 if (useAuthentication.isChecked()) {
242 val = String.valueOf(userName.getText());
243 if (val.trim().isEmpty()) {
246 .setError(getResources().getText(R.string.err_profile_user_name_empty));
249 val = String.valueOf(password.getText());
250 if (val.trim().isEmpty()) {
253 .setError(getResources().getText(R.string.err_profile_password_empty));