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.v4.app.Fragment;
27 import android.util.Log;
28 import android.view.LayoutInflater;
29 import android.view.Menu;
30 import android.view.MenuInflater;
31 import android.view.MenuItem;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.widget.LinearLayout;
35 import android.widget.Switch;
36 import android.widget.TextView;
38 import net.ktnx.mobileledger.R;
39 import net.ktnx.mobileledger.model.Data;
40 import net.ktnx.mobileledger.model.MobileLedgerProfile;
41 import net.ktnx.mobileledger.ui.activity.ProfileListActivity;
44 * A fragment representing a single Profile detail screen.
45 * This fragment is either contained in a {@link ProfileListActivity}
46 * in two-pane mode (on tablets) or a {@link ProfileDetailActivity}
49 public class ProfileDetailFragment extends Fragment {
51 * The fragment argument representing the item ID that this fragment
54 public static final String ARG_ITEM_ID = "item_id";
57 * The dummy content this fragment is presenting.
59 private MobileLedgerProfile mProfile;
61 private LinearLayout authParams;
62 private Switch useAuthentication;
63 private TextView userName;
64 private TextView password;
65 private FloatingActionButton fab;
66 private TextView profileName;
69 * Mandatory empty constructor for the fragment manager to instantiate the
70 * fragment (e.g. upon screen orientation changes).
72 public ProfileDetailFragment() {
75 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
76 Log.d("profiles", "[fragment] Creating profile details options menu");
77 super.onCreateOptionsMenu(menu, inflater);
78 inflater.inflate(R.menu.profile_details, menu);
79 final MenuItem menuDeleteProfile = menu.findItem(R.id.menuDelete);
80 menuDeleteProfile.setOnMenuItemClickListener(item -> {
81 Log.d("profiles", String.format("[fragment] removing profile %s", mProfile.getUuid()));
82 mProfile.removeFromDB();
83 Data.profiles.remove(mProfile);
84 if (Data.profile.get().equals(mProfile)) {
85 Log.d("profiles", "[fragment] setting current profile to 0");
86 Data.setCurrentProfile(Data.profiles.get(0));
90 menuDeleteProfile.setVisible((mProfile != null) && (Data.profiles.size() > 1));
93 public void onCreate(Bundle savedInstanceState) {
94 super.onCreate(savedInstanceState);
96 if ((getArguments() != null) && getArguments().containsKey(ARG_ITEM_ID)) {
97 // Load the dummy content specified by the fragment
98 // arguments. In a real-world scenario, use a Loader
99 // to load content from a content provider.
100 int index = getArguments().getInt(ARG_ITEM_ID, -1);
101 if (index != -1) mProfile = Data.profiles.get(index);
103 Activity activity = this.getActivity();
104 if (activity == null) throw new AssertionError();
105 CollapsingToolbarLayout appBarLayout = activity.findViewById(R.id.toolbar_layout);
106 if (appBarLayout != null) {
107 if (mProfile != null) appBarLayout.setTitle(mProfile.getName());
108 else appBarLayout.setTitle(getResources().getString(R.string.new_profile_title));
113 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
114 super.onActivityCreated(savedInstanceState);
115 Activity context = getActivity();
116 if (context == null) return;
118 fab = context.findViewById(R.id.fab);
119 fab.setOnClickListener(v -> {
120 if (mProfile != null) {
121 mProfile.setName(profileName.getText());
122 mProfile.setUrl(url.getText());
123 mProfile.setAuthEnabled(useAuthentication.isChecked());
124 mProfile.setAuthUserName(userName.getText());
125 mProfile.setAuthPassword(password.getText());
126 mProfile.storeInDB();
127 Log.d("profiles", "profile stored in DB");
128 Data.profiles.triggerItemChangedNotification(mProfile);
131 if (mProfile.getUuid().equals(Data.profile.get().getUuid())) {
132 // dummy update to notify the observers of the possibly new name/URL
133 Data.profile.set(mProfile);
137 mProfile = new MobileLedgerProfile(profileName.getText(), url.getText(),
138 useAuthentication.isChecked(), userName.getText(), password.getText());
139 mProfile.storeInDB();
140 Data.profiles.add(mProfile);
141 MobileLedgerProfile.storeProfilesOrder();
144 Activity activity = getActivity();
145 if (activity != null) activity.finish();
149 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
150 Bundle savedInstanceState) {
151 View rootView = inflater.inflate(R.layout.profile_detail, container, false);
153 profileName = rootView.findViewById(R.id.profile_name);
154 url = rootView.findViewById(R.id.url);
155 authParams = rootView.findViewById(R.id.auth_params);
156 useAuthentication = rootView.findViewById(R.id.enable_http_auth);
157 userName = rootView.findViewById(R.id.auth_user_name);
158 password = rootView.findViewById(R.id.password);
160 useAuthentication.setOnCheckedChangeListener((buttonView, isChecked) -> {
161 Log.d("profiles", isChecked ? "auth enabled " : "auth disabled");
162 authParams.setVisibility(isChecked ? View.VISIBLE : View.GONE);
163 if (isChecked) userName.requestFocus();
166 if (mProfile != null) {
167 profileName.setText(mProfile.getName());
168 url.setText(mProfile.getUrl());
169 useAuthentication.setChecked(mProfile.isAuthEnabled());
170 authParams.setVisibility(mProfile.isAuthEnabled() ? View.VISIBLE : View.GONE);
171 userName.setText(mProfile.isAuthEnabled() ? mProfile.getAuthUserName() : "");
172 password.setText(mProfile.isAuthEnabled() ? mProfile.getAuthPassword() : "");
175 profileName.setText("");
177 useAuthentication.setChecked(false);
178 authParams.setVisibility(View.GONE);
179 userName.setText("");
180 password.setText("");