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