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.os.Bundle;
23 import android.support.annotation.NonNull;
24 import android.support.v7.app.ActionBar;
25 import android.support.v7.widget.DividerItemDecoration;
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.Menu;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.widget.RadioButton;
35 import android.widget.TextView;
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;
43 import java.util.Collections;
46 * An activity representing a list of Profiles. This activity
47 * has different presentations for handset and tablet-size devices. On
48 * handsets, the activity presents a list of items, which when touched,
49 * lead to a {@link ProfileDetailActivity} representing
50 * item details. On tablets, the activity presents the list of items and
51 * item details side-by-side using two vertical panes.
53 public class ProfileListActivity extends CrashReportingActivity {
55 public static final String ARG_ACTION = "action";
56 public static final String ARG_PROFILE_INDEX = "profile_index";
57 public static final int PROFILE_INDEX_NONE = -1;
58 public static final int ACTION_EDIT_PROFILE = 1;
59 public static final int ACTION_INVALID = -1;
61 * Whether or not the activity is in two-pane mode, i.e. running on a tablet
64 private boolean mTwoPane;
65 private RecyclerView recyclerView;
68 public boolean onSupportNavigateUp() {
70 return super.onSupportNavigateUp();
73 protected void onCreate(Bundle savedInstanceState) {
74 super.onCreate(savedInstanceState);
75 setContentView(R.layout.activity_profile_list);
77 Toolbar toolbar = findViewById(R.id.toolbar);
78 setSupportActionBar(toolbar);
79 toolbar.setTitle(getTitle());
80 final ActionBar supportActionBar = getSupportActionBar();
81 if (supportActionBar != null) {
82 supportActionBar.setDisplayHomeAsUpEnabled(true);
83 supportActionBar.setDisplayShowHomeEnabled(true);
86 recyclerView = findViewById(R.id.profile_list);
87 if (recyclerView == null) throw new AssertionError();
88 setupRecyclerView(recyclerView);
90 if (findViewById(R.id.profile_detail_container) != null) {
91 // The detail container view will be present only in the
92 // large-screen layouts (res/values-w900dp).
93 // If this view is present, then the
94 // activity should be in two-pane mode.
98 int action = getIntent().getIntExtra(ARG_ACTION, ACTION_INVALID);
99 if (action == ACTION_EDIT_PROFILE) {
100 Log.d("profiles", "got edit profile action");
101 int index = getIntent().getIntExtra(ARG_PROFILE_INDEX, PROFILE_INDEX_NONE);
103 MobileLedgerProfile profile = (index >= 0) ? Data.profiles.get(index) : null;
104 ProfilesRecyclerViewAdapter adapter = (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
105 if (adapter != null) {
106 adapter.editProfile(recyclerView, profile);
108 // if invoked from the initial screen, get out so that when the new profile
109 // activity finishes the user i navigated to the main activity
110 if ((profile == null) && Data.profiles.getList().isEmpty()) finish();
114 void launchNewProfileActivity() {
115 ProfilesRecyclerViewAdapter adapter =
116 (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
117 if (adapter != null) adapter.editProfile(recyclerView, null);
119 private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
120 final ProfilesRecyclerViewAdapter adapter = new ProfilesRecyclerViewAdapter(this, mTwoPane);
121 recyclerView.setAdapter(adapter);
122 ItemTouchHelper.Callback cb = new ItemTouchHelper.Callback() {
124 public int getMovementFlags(@NonNull RecyclerView recyclerView,
125 @NonNull RecyclerView.ViewHolder viewHolder) {
126 return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0);
129 public boolean onMove(@NonNull RecyclerView recyclerView,
130 @NonNull RecyclerView.ViewHolder viewHolder,
131 @NonNull RecyclerView.ViewHolder target) {
132 Collections.swap(Data.profiles.getList(), viewHolder.getAdapterPosition(),
133 target.getAdapterPosition());
134 MobileLedgerProfile.storeProfilesOrder();
135 adapter.notifyItemMoved(viewHolder.getAdapterPosition(),
136 target.getAdapterPosition());
140 public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
144 new ItemTouchHelper(cb).attachToRecyclerView(recyclerView);
145 recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(),
146 DividerItemDecoration.VERTICAL));
149 public boolean onCreateOptionsMenu(Menu menu) {
150 getMenuInflater().inflate(R.menu.profile_list, menu);
151 menu.findItem(R.id.menu_add_profile).setOnMenuItemClickListener(item -> {
152 launchNewProfileActivity();
155 return super.onCreateOptionsMenu(menu);
157 public static class ProfilesRecyclerViewAdapter
158 extends RecyclerView.Adapter<ProfilesRecyclerViewAdapter.ProfileListViewHolder> {
160 private final ProfileListActivity mParentActivity;
161 private final boolean mTwoPane;
162 private final View.OnClickListener mOnClickListener = view -> {
163 MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag();
164 editProfile(view, profile);
166 ProfilesRecyclerViewAdapter(ProfileListActivity parent, boolean twoPane) {
167 mParentActivity = parent;
169 Data.profiles.addObserver((o, arg) -> {
170 Log.d("profiles", "profile list changed");
171 if (arg == null) notifyDataSetChanged();
172 else notifyItemChanged((int) arg);
175 private void editProfile(View view, MobileLedgerProfile profile) {
176 int index = Data.profiles.indexOf(profile);
178 Bundle arguments = new Bundle();
179 arguments.putInt(ProfileDetailFragment.ARG_ITEM_ID, index);
180 ProfileDetailFragment fragment = new ProfileDetailFragment();
181 fragment.setArguments(arguments);
182 mParentActivity.getSupportFragmentManager().beginTransaction()
183 .replace(R.id.profile_detail_container, fragment).commit();
186 Context context = view.getContext();
187 Intent intent = new Intent(context, ProfileDetailActivity.class);
188 intent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
189 if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
191 context.startActivity(intent);
196 public ProfileListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
197 View view = LayoutInflater.from(parent.getContext())
198 .inflate(R.layout.profile_list_content, parent, false);
199 ProfileListViewHolder holder = new ProfileListViewHolder(view);
201 holder.mRadioView.setOnCheckedChangeListener((buttonView, isChecked) -> {
202 if (!isChecked) return;
204 String.format("Item %d got checked", holder.getAdapterPosition()));
205 MobileLedgerProfile profile = (MobileLedgerProfile) holder.itemView.getTag();
206 if (profile != null) {
208 String.format("Setting current profile to %s", profile.getUuid()));
209 Data.setCurrentProfile(profile);
212 View.OnClickListener profileSelector = v -> holder.mRadioView.setChecked(true);
213 holder.mTitle.setOnClickListener(profileSelector);
214 holder.mSubTitle.setOnClickListener(profileSelector);
215 Data.profile.addObserver((o, arg) -> {
216 MobileLedgerProfile myProfile = (MobileLedgerProfile) holder.itemView.getTag();
217 final MobileLedgerProfile currentProfile = Data.profile.get();
218 final boolean sameProfile = currentProfile.equals(myProfile);
219 if (holder.mRadioView.isChecked() != sameProfile) {
220 holder.mRadioView.setChecked(sameProfile);
226 public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
227 final MobileLedgerProfile profile = Data.profiles.get(position);
228 final MobileLedgerProfile currentProfile = Data.profile.get();
229 Log.d("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(),
230 (currentProfile == null) ? "<NULL>" : currentProfile.getUuid()));
231 holder.itemView.setTag(profile);
232 holder.mTitle.setText(profile.getName());
233 holder.mSubTitle.setText(profile.getUrl());
234 holder.mRadioView.setChecked(profile.equals(currentProfile));
236 holder.mEditButton.setOnClickListener(mOnClickListener);
239 public int getItemCount() {
240 return Data.profiles.size();
242 class ProfileListViewHolder extends RecyclerView.ViewHolder {
243 final RadioButton mRadioView;
244 final TextView mEditButton;
245 final TextView mTitle, mSubTitle;
247 ProfileListViewHolder(View view) {
249 mRadioView = view.findViewById(R.id.profile_list_radio);
250 mEditButton = view.findViewById(R.id.profile_list_edit_button);
251 mTitle = view.findViewById(R.id.title);
252 mSubTitle = view.findViewById(R.id.subtitle);