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.content.Context;
22 import android.os.Bundle;
23 import android.support.annotation.NonNull;
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.View;
30 import android.view.ViewGroup;
31 import android.widget.LinearLayout;
32 import android.widget.Switch;
33 import android.widget.TextView;
35 import net.ktnx.mobileledger.R;
36 import net.ktnx.mobileledger.model.Data;
37 import net.ktnx.mobileledger.model.MobileLedgerProfile;
38 import net.ktnx.mobileledger.ui.activity.ProfileListActivity;
41 * A fragment representing a single Profile detail screen.
42 * This fragment is either contained in a {@link ProfileListActivity}
43 * in two-pane mode (on tablets) or a {@link ProfileDetailActivity}
46 public class ProfileDetailFragment extends Fragment {
48 * The fragment argument representing the item ID that this fragment
51 public static final String ARG_ITEM_ID = "item_id";
54 * The dummy content this fragment is presenting.
56 private MobileLedgerProfile mItem;
58 private LinearLayout authParams;
59 private Switch useAuthentication;
60 private TextView userName;
61 private TextView password;
62 private FloatingActionButton fab;
63 private TextView profileName;
66 * Mandatory empty constructor for the fragment manager to instantiate the
67 * fragment (e.g. upon screen orientation changes).
69 public ProfileDetailFragment() {
73 public void onCreate(Bundle savedInstanceState) {
74 super.onCreate(savedInstanceState);
76 if ((getArguments() != null) && getArguments().containsKey(ARG_ITEM_ID)) {
77 // Load the dummy content specified by the fragment
78 // arguments. In a real-world scenario, use a Loader
79 // to load content from a content provider.
80 String uuid = getArguments().getString(ARG_ITEM_ID);
82 mItem = MobileLedgerProfile.loadUUIDFromDB(getArguments().getString(ARG_ITEM_ID));
84 Activity activity = this.getActivity();
85 if (activity == null) throw new AssertionError();
86 CollapsingToolbarLayout appBarLayout = activity.findViewById(R.id.toolbar_layout);
87 if (appBarLayout != null) {
88 if (mItem != null) appBarLayout.setTitle(mItem.getName());
89 else appBarLayout.setTitle(getResources().getString(R.string.new_profile_title));
95 public void onAttach(Context context) {
96 super.onAttach(context);
97 fab = ((Activity) context).findViewById(R.id.fab);
98 fab.setOnClickListener(v -> {
100 mItem.setName(profileName.getText());
101 mItem.setUrl(url.getText());
102 mItem.setAuthEnabled(useAuthentication.isChecked());
103 mItem.setAuthUserName(userName.getText());
104 mItem.setAuthPassword(password.getText());
108 if (mItem.getUuid().equals(Data.profile.get().getUuid())) {
109 Data.profile.set(mItem);
113 mItem = new MobileLedgerProfile(profileName.getText(), url.getText(),
114 useAuthentication.isChecked(), userName.getText(), password.getText());
118 Activity activity = getActivity();
119 if (activity != null) activity.finish();
123 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
124 Bundle savedInstanceState) {
125 View rootView = inflater.inflate(R.layout.profile_detail, container, false);
127 profileName = rootView.findViewById(R.id.profile_name);
128 url = rootView.findViewById(R.id.url);
129 authParams = rootView.findViewById(R.id.auth_params);
130 useAuthentication = rootView.findViewById(R.id.enable_http_auth);
131 userName = rootView.findViewById(R.id.auth_user_name);
132 password = rootView.findViewById(R.id.password);
134 useAuthentication.setOnCheckedChangeListener((buttonView, isChecked) -> {
135 Log.d("profiles", isChecked ? "auth enabled " : "auth disabled");
136 authParams.setVisibility(isChecked ? View.VISIBLE : View.GONE);
140 profileName.setText(mItem.getName());
141 url.setText(mItem.getUrl());
142 useAuthentication.setChecked(mItem.isAuthEnabled());
143 authParams.setVisibility(mItem.isAuthEnabled() ? View.VISIBLE : View.GONE);
144 userName.setText(mItem.isAuthEnabled() ? mItem.getAuthUserName() : "");
145 password.setText(mItem.isAuthEnabled() ? mItem.getAuthPassword() : "");
148 profileName.setText("");
150 useAuthentication.setChecked(false);
151 authParams.setVisibility(View.GONE);
152 userName.setText("");
153 password.setText("");