]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java
drop unused instantiation of TransactionListViewModel
[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 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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.ui.profiles;
19
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;
40
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;
45
46 /**
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}
50  * on handsets.
51  */
52 public class ProfileDetailFragment extends Fragment {
53     /**
54      * The fragment argument representing the item ID that this fragment
55      * represents.
56      */
57     public static final String ARG_ITEM_ID = "item_id";
58
59     /**
60      * The dummy content this fragment is presenting.
61      */
62     private MobileLedgerProfile mProfile;
63     private TextView url;
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;
75
76     /**
77      * Mandatory empty constructor for the fragment manager to instantiate the
78      * fragment (e.g. upon screen orientation changes).
79      */
80     public ProfileDetailFragment() {
81     }
82     @Override
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));
95             }
96             return false;
97         });
98         menuDeleteProfile.setVisible((mProfile != null) && (Data.profiles.size() > 1));
99     }
100     @Override
101     public void onCreate(Bundle savedInstanceState) {
102         super.onCreate(savedInstanceState);
103
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);
107
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));
114             }
115         }
116     }
117     @Override
118     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
119         super.onActivityCreated(savedInstanceState);
120         Activity context = getActivity();
121         if (context == null) return;
122
123         fab = context.findViewById(R.id.fab);
124         fab.setOnClickListener(v -> {
125             if (!checkValidity()) return;
126
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);
137
138
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);
142                 }
143             }
144             else {
145                 mProfile = new MobileLedgerProfile(profileName.getText(), postingPermitted.isChecked(),
146                         url.getText(), useAuthentication.isChecked(), userName.getText(),
147                         password.getText());
148                 mProfile.storeInDB();
149                 Data.profiles.add(mProfile);
150                 MobileLedgerProfile.storeProfilesOrder();
151
152                 // first profile ever?
153                 if (Data.profiles.getList().size() == 1) Data.profile.set(mProfile);
154             }
155
156             Activity activity = getActivity();
157             if (activity != null) activity.finish();
158         });
159
160         profileName.requestFocus();
161     }
162     @Override
163     public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
164                              Bundle savedInstanceState) {
165         View rootView = inflater.inflate(R.layout.profile_detail, container, false);
166
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);
178
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();
183         });
184
185         hookClearErrorOnFocusListener(profileName, profileNameLayout);
186         hookClearErrorOnFocusListener(url, urlLayout);
187         hookClearErrorOnFocusListener(userName, userNameLayout);
188         hookClearErrorOnFocusListener(password, passwordLayout);
189
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() : "");
198         }
199         else {
200             profileName.setText("");
201             url.setText("");
202             postingPermitted.setChecked(true);
203             useAuthentication.setChecked(false);
204             authParams.setVisibility(View.GONE);
205             userName.setText("");
206             password.setText("");
207         }
208
209         return rootView;
210     }
211     private void hookClearErrorOnFocusListener(TextView view, TextInputLayout layout) {
212         view.setOnFocusChangeListener((v, hasFocus) -> {
213             if (hasFocus) layout.setError(null);
214         });
215         view.addTextChangedListener(new TextWatcher() {
216             @Override
217             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
218             }
219             @Override
220             public void onTextChanged(CharSequence s, int start, int before, int count) {
221                 layout.setError(null);
222             }
223             @Override
224             public void afterTextChanged(Editable s) {
225             }
226         });
227     }
228     boolean checkValidity() {
229         boolean valid = true;
230
231         String val = String.valueOf(profileName.getText());
232         if (val.trim().isEmpty()) {
233             valid = false;
234             profileNameLayout.setError(getResources().getText(R.string.err_profile_name_empty));
235         }
236
237         val = String.valueOf(url.getText());
238         if (val.trim().isEmpty()) {
239             valid = false;
240             urlLayout.setError(getResources().getText(R.string.err_profile_url_empty));
241         }
242         if (useAuthentication.isChecked()) {
243             val = String.valueOf(userName.getText());
244             if (val.trim().isEmpty()) {
245                 valid = false;
246                 userNameLayout
247                         .setError(getResources().getText(R.string.err_profile_user_name_empty));
248             }
249
250             val = String.valueOf(password.getText());
251             if (val.trim().isEmpty()) {
252                 valid = false;
253                 passwordLayout
254                         .setError(getResources().getText(R.string.err_profile_password_empty));
255             }
256         }
257
258         return valid;
259     }
260 }