]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java
somewhat complete profile implementation
[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.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;
34
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;
39
40 /**
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}
44  * on handsets.
45  */
46 public class ProfileDetailFragment extends Fragment {
47     /**
48      * The fragment argument representing the item ID that this fragment
49      * represents.
50      */
51     public static final String ARG_ITEM_ID = "item_id";
52
53     /**
54      * The dummy content this fragment is presenting.
55      */
56     private MobileLedgerProfile mItem;
57     private TextView url;
58     private LinearLayout authParams;
59     private Switch useAuthentication;
60     private TextView userName;
61     private TextView password;
62     private FloatingActionButton fab;
63     private TextView profileName;
64
65     /**
66      * Mandatory empty constructor for the fragment manager to instantiate the
67      * fragment (e.g. upon screen orientation changes).
68      */
69     public ProfileDetailFragment() {
70     }
71
72     @Override
73     public void onCreate(Bundle savedInstanceState) {
74         super.onCreate(savedInstanceState);
75
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);
81             if (uuid != null)
82                 mItem = MobileLedgerProfile.loadUUIDFromDB(getArguments().getString(ARG_ITEM_ID));
83
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));
90             }
91         }
92     }
93
94     @Override
95     public void onAttach(Context context) {
96         super.onAttach(context);
97         fab = ((Activity) context).findViewById(R.id.fab);
98         fab.setOnClickListener(v -> {
99             if (mItem != null) {
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());
105                 mItem.storeInDB();
106
107
108                 if (mItem.getUuid().equals(Data.profile.get().getUuid())) {
109                     Data.profile.set(mItem);
110                 }
111             }
112             else {
113                 mItem = new MobileLedgerProfile(profileName.getText(), url.getText(),
114                         useAuthentication.isChecked(), userName.getText(), password.getText());
115                 mItem.storeInDB();
116             }
117
118             Activity activity = getActivity();
119             if (activity != null) activity.finish();
120         });
121     }
122     @Override
123     public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
124                              Bundle savedInstanceState) {
125         View rootView = inflater.inflate(R.layout.profile_detail, container, false);
126
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);
133
134         useAuthentication.setOnCheckedChangeListener((buttonView, isChecked) -> {
135             Log.d("profiles", isChecked ? "auth enabled " : "auth disabled");
136             authParams.setVisibility(isChecked ? View.VISIBLE : View.GONE);
137         });
138
139         if (mItem != null) {
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() : "");
146         }
147         else {
148             profileName.setText("");
149             url.setText("");
150             useAuthentication.setChecked(false);
151             authParams.setVisibility(View.GONE);
152             userName.setText("");
153             password.setText("");
154         }
155
156         return rootView;
157     }
158 }