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.
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.
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/>.
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.design.widget.FloatingActionButton;
25 import android.support.v7.app.AppCompatActivity;
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.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 AppCompatActivity {
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 FloatingActionButton fab;
68 protected void onCreate(Bundle savedInstanceState) {
69 super.onCreate(savedInstanceState);
70 setContentView(R.layout.activity_profile_list);
72 Toolbar toolbar = findViewById(R.id.toolbar);
73 setSupportActionBar(toolbar);
74 toolbar.setTitle(getTitle());
76 RecyclerView recyclerView = findViewById(R.id.profile_list);
77 if (recyclerView == null) throw new AssertionError();
78 setupRecyclerView(recyclerView);
80 fab = findViewById(R.id.fab);
81 fab.setOnClickListener(view -> {
82 ProfilesRecyclerViewAdapter adapter =
83 (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
84 if (adapter != null) adapter.editProfile(recyclerView, null);
87 if (findViewById(R.id.profile_detail_container) != null) {
88 // The detail container view will be present only in the
89 // large-screen layouts (res/values-w900dp).
90 // If this view is present, then the
91 // activity should be in two-pane mode.
95 int action = getIntent().getIntExtra(ARG_ACTION, ACTION_INVALID);
96 if (action == ACTION_EDIT_PROFILE) {
97 Log.d("profiles", "got edit profile action");
98 int index = getIntent().getIntExtra(ARG_PROFILE_INDEX, PROFILE_INDEX_NONE);
100 MobileLedgerProfile profile = (index >= 0) ? Data.profiles.get(index) : null;
101 ProfilesRecyclerViewAdapter adapter =
102 (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
103 if (adapter != null) {
104 adapter.editProfile(recyclerView, profile);
106 // if invoked from the initial screen, get out so that when the new profile
107 // activity finishes the user i navigated to the main activity
108 if ((profile == null) && Data.profiles.getList().isEmpty()) finish();
114 protected void onStart() {
118 private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
119 final ProfilesRecyclerViewAdapter adapter = new ProfilesRecyclerViewAdapter(this, mTwoPane);
120 recyclerView.setAdapter(adapter);
121 ItemTouchHelper.Callback cb = new ItemTouchHelper.Callback() {
123 public int getMovementFlags(@NonNull RecyclerView recyclerView,
124 @NonNull RecyclerView.ViewHolder viewHolder) {
125 return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0);
128 public boolean onMove(@NonNull RecyclerView recyclerView,
129 @NonNull RecyclerView.ViewHolder viewHolder,
130 @NonNull RecyclerView.ViewHolder target) {
131 Collections.swap(Data.profiles.getList(), viewHolder.getAdapterPosition(),
132 target.getAdapterPosition());
133 MobileLedgerProfile.storeProfilesOrder();
134 adapter.notifyItemMoved(viewHolder.getAdapterPosition(),
135 target.getAdapterPosition());
139 public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
143 new ItemTouchHelper(cb).attachToRecyclerView(recyclerView);
144 recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(),
145 DividerItemDecoration.VERTICAL));
148 public static class ProfilesRecyclerViewAdapter
149 extends RecyclerView.Adapter<ProfilesRecyclerViewAdapter.ProfileListViewHolder> {
151 private final ProfileListActivity mParentActivity;
152 private final boolean mTwoPane;
153 private final View.OnClickListener mOnClickListener = view -> {
154 MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag();
155 editProfile(view, profile);
157 ProfilesRecyclerViewAdapter(ProfileListActivity parent, boolean twoPane) {
158 mParentActivity = parent;
160 Data.profiles.addObserver((o, arg) -> {
161 Log.d("profiles", "profile list changed");
162 if (arg == null) notifyDataSetChanged();
163 else notifyItemChanged((int) arg);
166 private void editProfile(View view, MobileLedgerProfile profile) {
167 int index = Data.profiles.indexOf(profile);
169 Bundle arguments = new Bundle();
170 arguments.putInt(ProfileDetailFragment.ARG_ITEM_ID, index);
171 ProfileDetailFragment fragment = new ProfileDetailFragment();
172 fragment.setArguments(arguments);
173 mParentActivity.getSupportFragmentManager().beginTransaction()
174 .replace(R.id.profile_detail_container, fragment).commit();
177 Context context = view.getContext();
178 Intent intent = new Intent(context, ProfileDetailActivity.class);
179 intent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
180 if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
182 context.startActivity(intent);
187 public ProfileListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
188 View view = LayoutInflater.from(parent.getContext())
189 .inflate(R.layout.profile_list_content, parent, false);
190 ProfileListViewHolder holder = new ProfileListViewHolder(view);
192 holder.mRadioView.setOnCheckedChangeListener((buttonView, isChecked) -> {
193 if (!isChecked) return;
195 String.format("Item %d got checked", holder.getAdapterPosition()));
196 MobileLedgerProfile profile = (MobileLedgerProfile) holder.itemView.getTag();
197 if (profile != null) {
199 String.format("Setting current profile to %s", profile.getUuid()));
200 Data.setCurrentProfile(profile);
203 View.OnClickListener profileSelector = v -> holder.mRadioView.setChecked(true);
204 holder.mTitle.setOnClickListener(profileSelector);
205 holder.mSubTitle.setOnClickListener(profileSelector);
206 Data.profile.addObserver((o, arg) -> {
207 MobileLedgerProfile myProfile = (MobileLedgerProfile) holder.itemView.getTag();
208 final MobileLedgerProfile currentProfile = Data.profile.get();
209 final boolean sameProfile = currentProfile.equals(myProfile);
210 if (holder.mRadioView.isChecked() != sameProfile) {
211 holder.mRadioView.setChecked(sameProfile);
217 public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
218 final MobileLedgerProfile profile = Data.profiles.get(position);
219 final MobileLedgerProfile currentProfile = Data.profile.get();
220 Log.d("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(),
221 (currentProfile == null) ? "<NULL>" : currentProfile.getUuid()));
222 holder.itemView.setTag(profile);
223 holder.mTitle.setText(profile.getName());
224 holder.mSubTitle.setText(profile.getUrl());
225 holder.mRadioView.setChecked(profile.equals(currentProfile));
227 holder.mEditButton.setOnClickListener(mOnClickListener);
230 public int getItemCount() {
231 return Data.profiles.size();
233 class ProfileListViewHolder extends RecyclerView.ViewHolder {
234 final RadioButton mRadioView;
235 final TextView mEditButton;
236 final TextView mTitle, mSubTitle;
238 ProfileListViewHolder(View view) {
240 mRadioView = view.findViewById(R.id.profile_list_radio);
241 mEditButton = view.findViewById(R.id.profile_list_edit_button);
242 mTitle = view.findViewById(R.id.title);
243 mSubTitle = view.findViewById(R.id.subtitle);