somewhat complete profile implementation
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / ProfileListActivity.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.activity;
19
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.support.annotation.NonNull;
24 import android.support.design.widget.FloatingActionButton;
25 import android.support.v7.app.AppCompatActivity;
26 import android.support.v7.widget.RecyclerView;
27 import android.support.v7.widget.Toolbar;
28 import android.util.Log;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.CompoundButton;
33 import android.widget.RadioButton;
34 import android.widget.TextView;
35
36 import net.ktnx.mobileledger.R;
37 import net.ktnx.mobileledger.model.Data;
38 import net.ktnx.mobileledger.model.MobileLedgerProfile;
39 import net.ktnx.mobileledger.ui.profiles.ProfileDetailActivity;
40 import net.ktnx.mobileledger.ui.profiles.ProfileDetailFragment;
41 import net.ktnx.mobileledger.utils.MLDB;
42
43 import java.util.List;
44 import java.util.Observable;
45 import java.util.Observer;
46
47 /**
48  * An activity representing a list of Profiles. This activity
49  * has different presentations for handset and tablet-size devices. On
50  * handsets, the activity presents a list of items, which when touched,
51  * lead to a {@link ProfileDetailActivity} representing
52  * item details. On tablets, the activity presents the list of items and
53  * item details side-by-side using two vertical panes.
54  */
55 public class ProfileListActivity extends AppCompatActivity {
56
57     public static final String ARG_ACTION = "action";
58     public static final String ARG_PROFILE_INDEX = "profile_uuid";
59     public static final int ACTION_EDIT_PROFILE = 1;
60     public static final int ACTION_INVALID = -1;
61     /**
62      * Whether or not the activity is in two-pane mode, i.e. running on a tablet
63      * device.
64      */
65     private boolean mTwoPane;
66
67     @Override
68     protected void onCreate(Bundle savedInstanceState) {
69         super.onCreate(savedInstanceState);
70         setContentView(R.layout.activity_profile_list);
71
72         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
73         setSupportActionBar(toolbar);
74         toolbar.setTitle(getTitle());
75
76         RecyclerView recyclerView = findViewById(R.id.profile_list);
77         if (recyclerView == null) throw new AssertionError();
78         setupRecyclerView(recyclerView);
79
80         FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
81         fab.setOnClickListener(new View.OnClickListener() {
82             @Override
83             public void onClick(View view) {
84                 ProfilesRecyclerViewAdapter adapter =
85                         (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
86                 if (adapter != null) adapter.editProfile(recyclerView, null);
87             }
88         });
89
90         if (findViewById(R.id.profile_detail_container) != null) {
91             // The detail container view will be present only in the
92             // large-screen layouts (res/values-w900dp).
93             // If this view is present, then the
94             // activity should be in two-pane mode.
95             mTwoPane = true;
96         }
97
98         int action = getIntent().getIntExtra(ARG_ACTION, ACTION_INVALID);
99         if (action == ACTION_EDIT_PROFILE) {
100             Log.d("profiles", "got edit profile action");
101             int index = getIntent().getIntExtra(ARG_PROFILE_INDEX, -1);
102             if (index >= 0) {
103                 List<MobileLedgerProfile> list = MobileLedgerProfile.loadAllFromDB();
104                 if (index < list.size()) {
105                     ProfilesRecyclerViewAdapter adapter =
106                             (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
107                     if (adapter != null) adapter.editProfile(recyclerView, list.get(index));
108                 }
109             }
110         }
111     }
112
113     private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
114         List<MobileLedgerProfile> list = MobileLedgerProfile.loadAllFromDB();
115         recyclerView.setAdapter(new ProfilesRecyclerViewAdapter(this, list, mTwoPane));
116     }
117
118     public static class ProfilesRecyclerViewAdapter
119             extends RecyclerView.Adapter<ProfilesRecyclerViewAdapter.ProfileListViewHolder> {
120
121         private final ProfileListActivity mParentActivity;
122         private final List<MobileLedgerProfile> mValues;
123         private final boolean mTwoPane;
124         private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
125             @Override
126             public void onClick(View view) {
127                 MobileLedgerProfile item = (MobileLedgerProfile) ((View) view.getParent()).getTag();
128                 editProfile(view, item);
129             }
130         };
131         ProfilesRecyclerViewAdapter(ProfileListActivity parent, List<MobileLedgerProfile> items,
132                                     boolean twoPane) {
133             mValues = items;
134             mParentActivity = parent;
135             mTwoPane = twoPane;
136         }
137         private void editProfile(View view, MobileLedgerProfile item) {
138             if (mTwoPane) {
139                 Bundle arguments = new Bundle();
140                 arguments.putString(ProfileDetailFragment.ARG_ITEM_ID, item.getUuid());
141                 ProfileDetailFragment fragment = new ProfileDetailFragment();
142                 fragment.setArguments(arguments);
143                 mParentActivity.getSupportFragmentManager().beginTransaction()
144                         .replace(R.id.profile_detail_container, fragment).commit();
145             }
146             else {
147                 Context context = view.getContext();
148                 Intent intent = new Intent(context, ProfileDetailActivity.class);
149                 intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID,
150                         (item == null) ? null : item.getUuid());
151
152                 context.startActivity(intent);
153             }
154         }
155         @NonNull
156         @Override
157         public ProfileListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
158             View view = LayoutInflater.from(parent.getContext())
159                     .inflate(R.layout.profile_list_content, parent, false);
160             ProfileListViewHolder holder = new ProfileListViewHolder(view);
161             Data.profile.addObserver(new Observer() {
162                 @Override
163                 public void update(Observable o, Object arg) {
164                     MobileLedgerProfile newProfile = Data.profile.get();
165                     MobileLedgerProfile profile = (MobileLedgerProfile) holder.itemView.getTag();
166                     holder.mRadioView.setChecked(
167                             newProfile != null && newProfile.getUuid().equals(profile.getUuid()));
168                 }
169             });
170             return holder;
171         }
172         @Override
173         public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
174             final MobileLedgerProfile profile = mValues.get(position);
175             final MobileLedgerProfile currentProfile = Data.profile.get();
176             Log.d("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(),
177                     currentProfile.getUuid()));
178             holder.mRadioView.setText(profile.getName());
179             holder.mRadioView.setChecked(profile.getUuid().equals(currentProfile.getUuid()));
180             holder.mRadioView
181                     .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
182                         @Override
183                         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
184                             if (!isChecked) return;
185                             MLDB.set_option_value(MLDB.OPT_PROFILE_UUID, profile.getUuid());
186                             Data.profile.set(profile);
187                         }
188                     });
189
190             holder.itemView.setTag(profile);
191             holder.mEditButton.setOnClickListener(mOnClickListener);
192
193         }
194         @Override
195         public int getItemCount() {
196             return mValues.size();
197         }
198         class ProfileListViewHolder extends RecyclerView.ViewHolder {
199             final RadioButton mRadioView;
200             final TextView mEditButton;
201
202             ProfileListViewHolder(View view) {
203                 super(view);
204                 mRadioView = view.findViewById(R.id.profile_list_radio);
205                 mEditButton = view.findViewById(R.id.profile_list_edit_button);
206             }
207         }
208     }
209 }