]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java
persistent profile list order
[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                 MobileLedgerProfile.storeProfilesOrder();
131                 adapter.notifyItemMoved(viewHolder.getAdapterPosition(),
132                         target.getAdapterPosition());
133                 return true;
134             }
135             @Override
136             public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
137
138             }
139         };
140         new ItemTouchHelper(cb).attachToRecyclerView(recyclerView);
141     }
142
143     public static class ProfilesRecyclerViewAdapter
144             extends RecyclerView.Adapter<ProfilesRecyclerViewAdapter.ProfileListViewHolder> {
145
146         private final ProfileListActivity mParentActivity;
147         private final boolean mTwoPane;
148         private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
149             @Override
150             public void onClick(View view) {
151                 MobileLedgerProfile item = (MobileLedgerProfile) ((View) view.getParent()).getTag();
152                 editProfile(view, item);
153             }
154         };
155         ProfilesRecyclerViewAdapter(ProfileListActivity parent, boolean twoPane) {
156             mParentActivity = parent;
157             mTwoPane = twoPane;
158             Data.profiles.addObserver((o, arg) -> {
159                 Log.d("profiles", "profile list changed");
160                 notifyDataSetChanged();
161             });
162         }
163         private void editProfile(View view, MobileLedgerProfile item) {
164             if (mTwoPane) {
165                 Bundle arguments = new Bundle();
166                 arguments.putString(ProfileDetailFragment.ARG_ITEM_ID, item.getUuid());
167                 ProfileDetailFragment fragment = new ProfileDetailFragment();
168                 fragment.setArguments(arguments);
169                 mParentActivity.getSupportFragmentManager().beginTransaction()
170                         .replace(R.id.profile_detail_container, fragment).commit();
171             }
172             else {
173                 Context context = view.getContext();
174                 Intent intent = new Intent(context, ProfileDetailActivity.class);
175                 intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID,
176                         (item == null) ? null : item.getUuid());
177
178                 context.startActivity(intent);
179             }
180         }
181         @NonNull
182         @Override
183         public ProfileListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
184             View view = LayoutInflater.from(parent.getContext())
185                     .inflate(R.layout.profile_list_content, parent, false);
186             ProfileListViewHolder holder = new ProfileListViewHolder(view);
187             Data.profile.addObserver(new Observer() {
188                 @Override
189                 public void update(Observable o, Object arg) {
190                     MobileLedgerProfile newProfile = Data.profile.get();
191                     MobileLedgerProfile profile = (MobileLedgerProfile) holder.itemView.getTag();
192                     holder.mRadioView.setChecked(
193                             newProfile != null && newProfile.getUuid().equals(profile.getUuid()));
194                 }
195             });
196             return holder;
197         }
198         @Override
199         public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
200             final MobileLedgerProfile profile = Data.profiles.get(position);
201             final MobileLedgerProfile currentProfile = Data.profile.get();
202             Log.d("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(),
203                     currentProfile.getUuid()));
204             holder.mRadioView.setText(profile.getName());
205             holder.mRadioView.setChecked(profile.getUuid().equals(currentProfile.getUuid()));
206             holder.mRadioView
207                     .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
208                         @Override
209                         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
210                             if (!isChecked) return;
211                             MLDB.set_option_value(MLDB.OPT_PROFILE_UUID, profile.getUuid());
212                             Data.profile.set(profile);
213                         }
214                     });
215
216             holder.itemView.setTag(profile);
217             holder.mEditButton.setOnClickListener(mOnClickListener);
218
219         }
220         @Override
221         public int getItemCount() {
222             return Data.profiles.size();
223         }
224         class ProfileListViewHolder extends RecyclerView.ViewHolder {
225             final RadioButton mRadioView;
226             final TextView mEditButton;
227
228             ProfileListViewHolder(View view) {
229                 super(view);
230                 mRadioView = view.findViewById(R.id.profile_list_radio);
231                 mEditButton = view.findViewById(R.id.profile_list_edit_button);
232             }
233         }
234     }
235 }