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.
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.
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/>.
18 package net.ktnx.mobileledger.ui.activity;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.graphics.Color;
23 import android.os.Bundle;
24 import androidx.annotation.NonNull;
25 import androidx.appcompat.app.ActionBar;
26 import androidx.recyclerview.widget.DividerItemDecoration;
27 import androidx.recyclerview.widget.RecyclerView;
28 import androidx.appcompat.widget.Toolbar;
29 import androidx.recyclerview.widget.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;
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;
44 import java.util.Collections;
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.
54 public class ProfileListActivity extends CrashReportingActivity {
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;
62 * Whether or not the activity is in two-pane mode, i.e. running on a tablet
65 private boolean mTwoPane;
66 private RecyclerView recyclerView;
69 public boolean onSupportNavigateUp() {
71 return super.onSupportNavigateUp();
74 protected void onCreate(Bundle savedInstanceState) {
75 super.onCreate(savedInstanceState);
76 setContentView(R.layout.activity_profile_list);
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);
87 recyclerView = findViewById(R.id.profile_list);
88 if (recyclerView == null) throw new AssertionError();
89 setupRecyclerView(recyclerView);
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.
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);
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);
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();
116 void launchNewProfileActivity() {
117 ProfilesRecyclerViewAdapter adapter =
118 (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
119 if (adapter != null) adapter.editProfile(recyclerView, null);
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() {
126 public int getMovementFlags(@NonNull RecyclerView recyclerView,
127 @NonNull RecyclerView.ViewHolder viewHolder) {
128 return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0);
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());
142 public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
146 new ItemTouchHelper(cb).attachToRecyclerView(recyclerView);
147 recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(),
148 DividerItemDecoration.VERTICAL));
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();
157 return super.onCreateOptionsMenu(menu);
159 public static class ProfilesRecyclerViewAdapter
160 extends RecyclerView.Adapter<ProfilesRecyclerViewAdapter.ProfileListViewHolder> {
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);
168 ProfilesRecyclerViewAdapter(ProfileListActivity parent, boolean twoPane) {
169 mParentActivity = parent;
171 Data.profiles.addObserver((o, arg) -> {
172 Log.d("profiles", "profile list changed");
173 if (arg == null) notifyDataSetChanged();
174 else notifyItemChanged((int) arg);
177 private void editProfile(View view, MobileLedgerProfile profile) {
178 int index = Data.profiles.indexOf(profile);
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();
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);
193 context.startActivity(intent);
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);
203 holder.mRadioView.setOnCheckedChangeListener((buttonView, isChecked) -> {
204 if (!isChecked) return;
206 String.format("Item %d got checked", holder.getAdapterPosition()));
207 MobileLedgerProfile profile = (MobileLedgerProfile) holder.itemView.getTag();
208 if (profile != null) {
210 String.format("Setting current profile to %s", profile.getUuid()));
211 Data.setCurrentProfile(profile);
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);
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);
235 int hue = profile.getThemeId();
236 if (hue == -1) holder.mColorTag.setBackgroundColor(Color.TRANSPARENT);
237 else holder.mColorTag.setBackgroundColor(Colors.getPrimaryColorForHue(hue));
239 holder.mTitle.setText(profile.getName());
240 holder.mSubTitle.setText(profile.getUrl());
241 holder.mRadioView.setChecked(profile.equals(currentProfile));
243 holder.mEditButton.setOnClickListener(mOnClickListener);
246 public int getItemCount() {
247 return Data.profiles.size();
249 class ProfileListViewHolder extends RecyclerView.ViewHolder {
250 final RadioButton mRadioView;
251 final TextView mEditButton;
252 final TextView mTitle, mSubTitle, mColorTag;
254 ProfileListViewHolder(View 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);