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_uuid";
57 public static final int ACTION_EDIT_PROFILE = 1;
58 public static final int ACTION_INVALID = -1;
60 * Whether or not the activity is in two-pane mode, i.e. running on a tablet
63 private boolean mTwoPane;
64 private FloatingActionButton fab;
67 protected void onCreate(Bundle savedInstanceState) {
68 super.onCreate(savedInstanceState);
69 setContentView(R.layout.activity_profile_list);
71 Toolbar toolbar = findViewById(R.id.toolbar);
72 setSupportActionBar(toolbar);
73 toolbar.setTitle(getTitle());
75 RecyclerView recyclerView = findViewById(R.id.profile_list);
76 if (recyclerView == null) throw new AssertionError();
77 setupRecyclerView(recyclerView);
79 fab = findViewById(R.id.fab);
80 fab.setOnClickListener(view -> {
81 ProfilesRecyclerViewAdapter adapter =
82 (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
83 if (adapter != null) adapter.editProfile(recyclerView, null);
86 if (findViewById(R.id.profile_detail_container) != null) {
87 // The detail container view will be present only in the
88 // large-screen layouts (res/values-w900dp).
89 // If this view is present, then the
90 // activity should be in two-pane mode.
94 int action = getIntent().getIntExtra(ARG_ACTION, ACTION_INVALID);
95 if (action == ACTION_EDIT_PROFILE) {
96 Log.d("profiles", "got edit profile action");
97 int index = getIntent().getIntExtra(ARG_PROFILE_INDEX, -1);
99 MobileLedgerProfile profile = Data.profiles.get(index);
100 ProfilesRecyclerViewAdapter adapter =
101 (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
102 if (adapter != null) adapter.editProfile(recyclerView, profile);
108 protected void onStart() {
112 private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
113 final ProfilesRecyclerViewAdapter adapter = new ProfilesRecyclerViewAdapter(this, mTwoPane);
114 recyclerView.setAdapter(adapter);
115 ItemTouchHelper.Callback cb = new ItemTouchHelper.Callback() {
117 public int getMovementFlags(@NonNull RecyclerView recyclerView,
118 @NonNull RecyclerView.ViewHolder viewHolder) {
119 return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0);
122 public boolean onMove(@NonNull RecyclerView recyclerView,
123 @NonNull RecyclerView.ViewHolder viewHolder,
124 @NonNull RecyclerView.ViewHolder target) {
125 Collections.swap(Data.profiles.getList(), viewHolder.getAdapterPosition(),
126 target.getAdapterPosition());
127 MobileLedgerProfile.storeProfilesOrder();
128 adapter.notifyItemMoved(viewHolder.getAdapterPosition(),
129 target.getAdapterPosition());
133 public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
137 new ItemTouchHelper(cb).attachToRecyclerView(recyclerView);
138 recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(),
139 DividerItemDecoration.VERTICAL));
141 recyclerView.setOnFlingListener(new RecyclerView.OnFlingListener() {
143 public boolean onFling(int dX, int dY) {
144 // Log.d("tmp", String.format("fling %d %d", dX, dY));
145 if (dY > 0) fab.hide();
146 if (dY < 0) fab.show();
151 recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
153 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
154 super.onScrolled(recyclerView, dx, dy);
155 if (dy > 0) fab.hide();
156 if (dy < 0) fab.show();
161 public static class ProfilesRecyclerViewAdapter
162 extends RecyclerView.Adapter<ProfilesRecyclerViewAdapter.ProfileListViewHolder> {
164 private final ProfileListActivity mParentActivity;
165 private final boolean mTwoPane;
166 private final View.OnClickListener mOnClickListener = view -> {
167 MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag();
168 editProfile(view, profile);
170 ProfilesRecyclerViewAdapter(ProfileListActivity parent, boolean twoPane) {
171 mParentActivity = parent;
173 Data.profiles.addObserver((o, arg) -> {
174 Log.d("profiles", "profile list changed");
175 if (arg == null) notifyDataSetChanged();
176 else notifyItemChanged((int) arg);
179 private void editProfile(View view, MobileLedgerProfile profile) {
180 int index = Data.profiles.indexOf(profile);
182 Bundle arguments = new Bundle();
183 arguments.putInt(ProfileDetailFragment.ARG_ITEM_ID, index);
184 ProfileDetailFragment fragment = new ProfileDetailFragment();
185 fragment.setArguments(arguments);
186 mParentActivity.getSupportFragmentManager().beginTransaction()
187 .replace(R.id.profile_detail_container, fragment).commit();
190 Context context = view.getContext();
191 Intent intent = new Intent(context, ProfileDetailActivity.class);
192 if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
194 context.startActivity(intent);
199 public ProfileListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
200 View view = LayoutInflater.from(parent.getContext())
201 .inflate(R.layout.profile_list_content, parent, false);
202 ProfileListViewHolder holder = new ProfileListViewHolder(view);
203 Data.profile.addObserver((o, arg) -> {
204 MobileLedgerProfile newProfile = Data.profile.get();
205 MobileLedgerProfile profile = (MobileLedgerProfile) holder.itemView.getTag();
206 holder.mRadioView.setChecked(profile.equals(newProfile));
209 holder.mRadioView.setOnCheckedChangeListener((buttonView, isChecked) -> {
210 if (!isChecked) return;
211 MobileLedgerProfile profile = (MobileLedgerProfile) holder.itemView.getTag();
212 if (profile != null) 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 profile = (MobileLedgerProfile) holder.itemView.getTag();
219 holder.mRadioView.setChecked(Data.profile.get().equals(profile));
224 public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
225 final MobileLedgerProfile profile = Data.profiles.get(position);
226 final MobileLedgerProfile currentProfile = Data.profile.get();
227 Log.d("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(),
228 currentProfile.getUuid()));
229 holder.mTitle.setText(profile.getName());
230 holder.mSubTitle.setText(profile.getUrl());
231 holder.mRadioView.setChecked(profile.getUuid().equals(currentProfile.getUuid()));
233 holder.itemView.setTag(profile);
234 holder.mEditButton.setOnClickListener(mOnClickListener);
237 public int getItemCount() {
238 return Data.profiles.size();
240 class ProfileListViewHolder extends RecyclerView.ViewHolder {
241 final RadioButton mRadioView;
242 final TextView mEditButton;
243 final TextView mTitle, mSubTitle;
245 ProfileListViewHolder(View view) {
247 mRadioView = view.findViewById(R.id.profile_list_radio);
248 mEditButton = view.findViewById(R.id.profile_list_edit_button);
249 mTitle = view.findViewById(R.id.title);
250 mSubTitle = view.findViewById(R.id.subtitle);