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