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;
44 import java.util.Observable;
45 import java.util.Observer;
48 * An activity representing a list of Profiles. This activity
49 * has different presentations for handset and tablet-size devices. On
50 * handsets, the activity presents a list of items, which when touched,
51 * lead to a {@link ProfileDetailActivity} representing
52 * item details. On tablets, the activity presents the list of items and
53 * item details side-by-side using two vertical panes.
55 public class ProfileListActivity extends AppCompatActivity {
57 public static final String ARG_ACTION = "action";
58 public static final String ARG_PROFILE_INDEX = "profile_uuid";
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 FloatingActionButton fab;
69 protected void onCreate(Bundle savedInstanceState) {
70 super.onCreate(savedInstanceState);
71 setContentView(R.layout.activity_profile_list);
73 Toolbar toolbar = findViewById(R.id.toolbar);
74 setSupportActionBar(toolbar);
75 toolbar.setTitle(getTitle());
77 RecyclerView recyclerView = findViewById(R.id.profile_list);
78 if (recyclerView == null) throw new AssertionError();
79 setupRecyclerView(recyclerView);
81 fab = findViewById(R.id.fab);
82 fab.setOnClickListener(view -> {
83 ProfilesRecyclerViewAdapter adapter =
84 (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
85 if (adapter != null) adapter.editProfile(recyclerView, null);
88 if (findViewById(R.id.profile_detail_container) != null) {
89 // The detail container view will be present only in the
90 // large-screen layouts (res/values-w900dp).
91 // If this view is present, then the
92 // activity should be in two-pane mode.
96 int action = getIntent().getIntExtra(ARG_ACTION, ACTION_INVALID);
97 if (action == ACTION_EDIT_PROFILE) {
98 Log.d("profiles", "got edit profile action");
99 int index = getIntent().getIntExtra(ARG_PROFILE_INDEX, -1);
101 MobileLedgerProfile profile = Data.profiles.get(index);
102 ProfilesRecyclerViewAdapter adapter =
103 (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
104 if (adapter != null) adapter.editProfile(recyclerView, profile);
110 protected void onStart() {
114 private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
115 final ProfilesRecyclerViewAdapter adapter = new ProfilesRecyclerViewAdapter(this, mTwoPane);
116 recyclerView.setAdapter(adapter);
117 ItemTouchHelper.Callback cb = new ItemTouchHelper.Callback() {
119 public int getMovementFlags(@NonNull RecyclerView recyclerView,
120 @NonNull RecyclerView.ViewHolder viewHolder) {
121 return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0);
124 public boolean onMove(@NonNull RecyclerView recyclerView,
125 @NonNull RecyclerView.ViewHolder viewHolder,
126 @NonNull RecyclerView.ViewHolder target) {
127 Collections.swap(Data.profiles.getList(), viewHolder.getAdapterPosition(),
128 target.getAdapterPosition());
129 MobileLedgerProfile.storeProfilesOrder();
130 adapter.notifyItemMoved(viewHolder.getAdapterPosition(),
131 target.getAdapterPosition());
135 public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
139 new ItemTouchHelper(cb).attachToRecyclerView(recyclerView);
140 recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(),
141 DividerItemDecoration.VERTICAL));
143 recyclerView.setOnFlingListener(new RecyclerView.OnFlingListener() {
145 public boolean onFling(int dX, int dY) {
146 Log.d("tmp", String.format("fling %d %d", dX, dY));
147 if (dY > 0) fab.hide();
148 if (dY < 0) fab.show();
153 recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
155 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
156 super.onScrolled(recyclerView, dx, dy);
157 if (dy > 0) fab.hide();
158 if (dy < 0) fab.show();
163 public static class ProfilesRecyclerViewAdapter
164 extends RecyclerView.Adapter<ProfilesRecyclerViewAdapter.ProfileListViewHolder> {
166 private final ProfileListActivity mParentActivity;
167 private final boolean mTwoPane;
168 private final View.OnClickListener mOnClickListener = view -> {
169 MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag();
170 editProfile(view, profile);
172 ProfilesRecyclerViewAdapter(ProfileListActivity parent, boolean twoPane) {
173 mParentActivity = parent;
175 Data.profiles.addObserver((o, arg) -> {
176 Log.d("profiles", "profile list changed");
177 if (arg == null) notifyDataSetChanged();
178 else notifyItemChanged((int) arg);
181 private void editProfile(View view, MobileLedgerProfile profile) {
182 int index = Data.profiles.indexOf(profile);
184 Bundle arguments = new Bundle();
185 arguments.putInt(ProfileDetailFragment.ARG_ITEM_ID, index);
186 ProfileDetailFragment fragment = new ProfileDetailFragment();
187 fragment.setArguments(arguments);
188 mParentActivity.getSupportFragmentManager().beginTransaction()
189 .replace(R.id.profile_detail_container, fragment).commit();
192 Context context = view.getContext();
193 Intent intent = new Intent(context, ProfileDetailActivity.class);
194 if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
196 context.startActivity(intent);
201 public ProfileListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
202 View view = LayoutInflater.from(parent.getContext())
203 .inflate(R.layout.profile_list_content, parent, false);
204 ProfileListViewHolder holder = new ProfileListViewHolder(view);
205 Data.profile.addObserver((o, arg) -> {
206 MobileLedgerProfile newProfile = Data.profile.get();
207 MobileLedgerProfile profile = (MobileLedgerProfile) holder.itemView.getTag();
208 holder.mRadioView.setChecked(profile.equals(newProfile));
213 public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
214 final MobileLedgerProfile profile = Data.profiles.get(position);
215 final MobileLedgerProfile currentProfile = Data.profile.get();
216 Log.d("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(),
217 currentProfile.getUuid()));
218 View.OnClickListener profileSelector = v -> {
219 holder.mRadioView.setChecked(true);
220 Data.setCurrentProfile(profile);
222 Data.profile.addObserver(new Observer() {
224 public void update(Observable o, Object arg) {
225 holder.mRadioView.setChecked(Data.profile.get().equals(profile));
228 holder.mTitle.setText(profile.getName());
229 holder.mTitle.setOnClickListener(profileSelector);
230 holder.mSubTitle.setText(profile.getUrl());
231 holder.mSubTitle.setOnClickListener(profileSelector);
232 holder.mRadioView.setChecked(profile.getUuid().equals(currentProfile.getUuid()));
234 holder.itemView.setTag(profile);
235 holder.mEditButton.setOnClickListener(mOnClickListener);
238 public int getItemCount() {
239 return Data.profiles.size();
241 class ProfileListViewHolder extends RecyclerView.ViewHolder {
242 final RadioButton mRadioView;
243 final TextView mEditButton;
244 final TextView mTitle, mSubTitle;
246 ProfileListViewHolder(View view) {
248 mRadioView = view.findViewById(R.id.profile_list_radio);
249 mEditButton = view.findViewById(R.id.profile_list_edit_button);
250 mTitle = view.findViewById(R.id.title);
251 mSubTitle = view.findViewById(R.id.subtitle);