]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java
profile color control
[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 MoLe.
4  * MoLe 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  * MoLe 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 MoLe. 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.graphics.Color;
23 import android.os.Bundle;
24 import android.support.annotation.NonNull;
25 import android.support.v7.app.ActionBar;
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.Menu;
33 import android.view.View;
34 import android.view.ViewGroup;
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.ProfileDetailFragment;
42 import net.ktnx.mobileledger.utils.Colors;
43
44 import java.util.Collections;
45
46 /**
47  * An activity representing a list of Profiles. This activity
48  * has different presentations for handset and tablet-size devices. On
49  * handsets, the activity presents a list of items, which when touched,
50  * lead to a {@link ProfileDetailActivity} representing
51  * item details. On tablets, the activity presents the list of items and
52  * item details side-by-side using two vertical panes.
53  */
54 public class ProfileListActivity extends CrashReportingActivity {
55
56     public static final String ARG_ACTION = "action";
57     public static final String ARG_PROFILE_INDEX = "profile_index";
58     public static final int PROFILE_INDEX_NONE = -1;
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     private RecyclerView recyclerView;
67
68     @Override
69     public boolean onSupportNavigateUp() {
70         onBackPressed();
71         return super.onSupportNavigateUp();
72     }
73     @Override
74     protected void onCreate(Bundle savedInstanceState) {
75         super.onCreate(savedInstanceState);
76         setContentView(R.layout.activity_profile_list);
77
78         Toolbar toolbar = findViewById(R.id.toolbar);
79         setSupportActionBar(toolbar);
80         toolbar.setTitle(getTitle());
81         final ActionBar supportActionBar = getSupportActionBar();
82         if (supportActionBar != null) {
83             supportActionBar.setDisplayHomeAsUpEnabled(true);
84             supportActionBar.setDisplayShowHomeEnabled(true);
85         }
86
87         recyclerView = findViewById(R.id.profile_list);
88         if (recyclerView == null) throw new AssertionError();
89         setupRecyclerView(recyclerView);
90
91         if (findViewById(R.id.profile_detail_container) != null) {
92             // The detail container view will be present only in the
93             // large-screen layouts (res/values-w900dp).
94             // If this view is present, then the
95             // activity should be in two-pane mode.
96             mTwoPane = true;
97         }
98
99         int action = getIntent().getIntExtra(ARG_ACTION, ACTION_INVALID);
100         if (action == ACTION_EDIT_PROFILE) {
101             Log.d("profiles", "got edit profile action");
102             int index = getIntent().getIntExtra(ARG_PROFILE_INDEX, PROFILE_INDEX_NONE);
103
104             MobileLedgerProfile profile = (index >= 0) ? Data.profiles.get(index) : null;
105             ProfilesRecyclerViewAdapter adapter =
106                     (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
107             if (adapter != null) {
108                 adapter.editProfile(recyclerView, profile);
109
110                 // if invoked from the initial screen, get out so that when the new profile
111                 // activity finishes the user i navigated to the main activity
112                 if ((profile == null) && Data.profiles.getList().isEmpty()) finish();
113             }
114         }
115     }
116     void launchNewProfileActivity() {
117         ProfilesRecyclerViewAdapter adapter =
118                 (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
119         if (adapter != null) adapter.editProfile(recyclerView, null);
120     }
121     private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
122         final ProfilesRecyclerViewAdapter adapter = new ProfilesRecyclerViewAdapter(this, mTwoPane);
123         recyclerView.setAdapter(adapter);
124         ItemTouchHelper.Callback cb = new ItemTouchHelper.Callback() {
125             @Override
126             public int getMovementFlags(@NonNull RecyclerView recyclerView,
127                                         @NonNull RecyclerView.ViewHolder viewHolder) {
128                 return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0);
129             }
130             @Override
131             public boolean onMove(@NonNull RecyclerView recyclerView,
132                                   @NonNull RecyclerView.ViewHolder viewHolder,
133                                   @NonNull RecyclerView.ViewHolder target) {
134                 Collections.swap(Data.profiles.getList(), viewHolder.getAdapterPosition(),
135                         target.getAdapterPosition());
136                 MobileLedgerProfile.storeProfilesOrder();
137                 adapter.notifyItemMoved(viewHolder.getAdapterPosition(),
138                         target.getAdapterPosition());
139                 return true;
140             }
141             @Override
142             public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
143
144             }
145         };
146         new ItemTouchHelper(cb).attachToRecyclerView(recyclerView);
147         recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(),
148                 DividerItemDecoration.VERTICAL));
149     }
150     @Override
151     public boolean onCreateOptionsMenu(Menu menu) {
152         getMenuInflater().inflate(R.menu.profile_list, menu);
153         menu.findItem(R.id.menu_add_profile).setOnMenuItemClickListener(item -> {
154             launchNewProfileActivity();
155             return true;
156         });
157         return super.onCreateOptionsMenu(menu);
158     }
159     public static class ProfilesRecyclerViewAdapter
160             extends RecyclerView.Adapter<ProfilesRecyclerViewAdapter.ProfileListViewHolder> {
161
162         private final ProfileListActivity mParentActivity;
163         private final boolean mTwoPane;
164         private final View.OnClickListener mOnClickListener = view -> {
165             MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag();
166             editProfile(view, profile);
167         };
168         ProfilesRecyclerViewAdapter(ProfileListActivity parent, boolean twoPane) {
169             mParentActivity = parent;
170             mTwoPane = twoPane;
171             Data.profiles.addObserver((o, arg) -> {
172                 Log.d("profiles", "profile list changed");
173                 if (arg == null) notifyDataSetChanged();
174                 else notifyItemChanged((int) arg);
175             });
176         }
177         private void editProfile(View view, MobileLedgerProfile profile) {
178             int index = Data.profiles.indexOf(profile);
179             if (mTwoPane) {
180                 Bundle arguments = new Bundle();
181                 arguments.putInt(ProfileDetailFragment.ARG_ITEM_ID, index);
182                 ProfileDetailFragment fragment = new ProfileDetailFragment();
183                 fragment.setArguments(arguments);
184                 mParentActivity.getSupportFragmentManager().beginTransaction()
185                         .replace(R.id.profile_detail_container, fragment).commit();
186             }
187             else {
188                 Context context = view.getContext();
189                 Intent intent = new Intent(context, ProfileDetailActivity.class);
190                 intent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
191                 if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
192
193                 context.startActivity(intent);
194             }
195         }
196         @NonNull
197         @Override
198         public ProfileListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
199             View view = LayoutInflater.from(parent.getContext())
200                     .inflate(R.layout.profile_list_content, parent, false);
201             ProfileListViewHolder holder = new ProfileListViewHolder(view);
202
203             holder.mRadioView.setOnCheckedChangeListener((buttonView, isChecked) -> {
204                 if (!isChecked) return;
205                 Log.d("profiles",
206                         String.format("Item %d got checked", holder.getAdapterPosition()));
207                 MobileLedgerProfile profile = (MobileLedgerProfile) holder.itemView.getTag();
208                 if (profile != null) {
209                     Log.d("profiles",
210                             String.format("Setting current profile to %s", profile.getUuid()));
211                     Data.setCurrentProfile(profile);
212                 }
213             });
214             View.OnClickListener profileSelector = v -> holder.mRadioView.setChecked(true);
215             holder.mTitle.setOnClickListener(profileSelector);
216             holder.mSubTitle.setOnClickListener(profileSelector);
217             Data.profile.addObserver((o, arg) -> {
218                 MobileLedgerProfile myProfile = (MobileLedgerProfile) holder.itemView.getTag();
219                 final MobileLedgerProfile currentProfile = Data.profile.get();
220                 final boolean sameProfile = currentProfile.equals(myProfile);
221                 if (holder.mRadioView.isChecked() != sameProfile) {
222                     holder.mRadioView.setChecked(sameProfile);
223                 }
224             });
225             return holder;
226         }
227         @Override
228         public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
229             final MobileLedgerProfile profile = Data.profiles.get(position);
230             final MobileLedgerProfile currentProfile = Data.profile.get();
231             Log.d("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(),
232                     (currentProfile == null) ? "<NULL>" : currentProfile.getUuid()));
233             holder.itemView.setTag(profile);
234
235             int hue = profile.getThemeId();
236             if (hue == -1) holder.mColorTag.setBackgroundColor(Color.TRANSPARENT);
237             else holder.mColorTag.setBackgroundColor(Colors.getPrimaryColorForHue(hue));
238
239             holder.mTitle.setText(profile.getName());
240             holder.mSubTitle.setText(profile.getUrl());
241             holder.mRadioView.setChecked(profile.equals(currentProfile));
242
243             holder.mEditButton.setOnClickListener(mOnClickListener);
244         }
245         @Override
246         public int getItemCount() {
247             return Data.profiles.size();
248         }
249         class ProfileListViewHolder extends RecyclerView.ViewHolder {
250             final RadioButton mRadioView;
251             final TextView mEditButton;
252             final TextView mTitle, mSubTitle, mColorTag;
253
254             ProfileListViewHolder(View view) {
255                 super(view);
256                 mRadioView = view.findViewById(R.id.profile_list_radio);
257                 mEditButton = view.findViewById(R.id.profile_list_edit_button);
258                 mTitle = view.findViewById(R.id.title);
259                 mSubTitle = view.findViewById(R.id.subtitle);
260                 mColorTag = view.findViewById(R.id.colorTag);
261             }
262         }
263     }
264 }