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;
42 import net.ktnx.mobileledger.utils.MLDB;
44 import java.util.Collections;
45 import java.util.List;
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;
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 FloatingActionButton 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, -1);
100 List<MobileLedgerProfile> list = MobileLedgerProfile.loadAllFromDB();
101 if (index < list.size()) {
102 ProfilesRecyclerViewAdapter adapter =
103 (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
104 if (adapter != null) adapter.editProfile(recyclerView, list.get(index));
110 private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
111 final ProfilesRecyclerViewAdapter adapter = new ProfilesRecyclerViewAdapter(this, mTwoPane);
112 recyclerView.setAdapter(adapter);
113 ItemTouchHelper.Callback cb = new ItemTouchHelper.Callback() {
115 public int getMovementFlags(@NonNull RecyclerView recyclerView,
116 @NonNull RecyclerView.ViewHolder viewHolder) {
117 return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0);
120 public boolean onMove(@NonNull RecyclerView recyclerView,
121 @NonNull RecyclerView.ViewHolder viewHolder,
122 @NonNull RecyclerView.ViewHolder target) {
123 Collections.swap(Data.profiles.getList(), viewHolder.getAdapterPosition(),
124 target.getAdapterPosition());
125 MobileLedgerProfile.storeProfilesOrder();
126 adapter.notifyItemMoved(viewHolder.getAdapterPosition(),
127 target.getAdapterPosition());
131 public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
135 new ItemTouchHelper(cb).attachToRecyclerView(recyclerView);
136 recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(),
137 DividerItemDecoration.VERTICAL));
140 public static class ProfilesRecyclerViewAdapter
141 extends RecyclerView.Adapter<ProfilesRecyclerViewAdapter.ProfileListViewHolder> {
143 private final ProfileListActivity mParentActivity;
144 private final boolean mTwoPane;
145 private final View.OnClickListener mOnClickListener = view -> {
146 MobileLedgerProfile item = (MobileLedgerProfile) ((View) view.getParent()).getTag();
147 editProfile(view, item);
149 ProfilesRecyclerViewAdapter(ProfileListActivity parent, boolean twoPane) {
150 mParentActivity = parent;
152 Data.profiles.addObserver((o, arg) -> {
153 Log.d("profiles", "profile list changed");
154 notifyDataSetChanged();
157 private void editProfile(View view, MobileLedgerProfile item) {
159 Bundle arguments = new Bundle();
160 arguments.putString(ProfileDetailFragment.ARG_ITEM_ID, item.getUuid());
161 ProfileDetailFragment fragment = new ProfileDetailFragment();
162 fragment.setArguments(arguments);
163 mParentActivity.getSupportFragmentManager().beginTransaction()
164 .replace(R.id.profile_detail_container, fragment).commit();
167 Context context = view.getContext();
168 Intent intent = new Intent(context, ProfileDetailActivity.class);
169 intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID,
170 (item == null) ? null : item.getUuid());
172 context.startActivity(intent);
177 public ProfileListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
178 View view = LayoutInflater.from(parent.getContext())
179 .inflate(R.layout.profile_list_content, parent, false);
180 ProfileListViewHolder holder = new ProfileListViewHolder(view);
181 Data.profile.addObserver((o, arg) -> {
182 MobileLedgerProfile newProfile = Data.profile.get();
183 MobileLedgerProfile profile = (MobileLedgerProfile) holder.itemView.getTag();
184 holder.mRadioView.setChecked(
185 newProfile != null && newProfile.getUuid().equals(profile.getUuid()));
190 public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
191 final MobileLedgerProfile profile = Data.profiles.get(position);
192 final MobileLedgerProfile currentProfile = Data.profile.get();
193 Log.d("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(),
194 currentProfile.getUuid()));
195 View.OnClickListener profileSelector = v -> holder.mRadioView.setChecked(true);
196 holder.mTitle.setText(profile.getName());
197 holder.mTitle.setOnClickListener(profileSelector);
198 holder.mSubTitle.setText(profile.getUrl());
199 holder.mSubTitle.setOnClickListener(profileSelector);
200 holder.mRadioView.setChecked(profile.getUuid().equals(currentProfile.getUuid()));
201 holder.mRadioView.setOnCheckedChangeListener((buttonView, isChecked) -> {
202 if (!isChecked) return;
203 MLDB.set_option_value(MLDB.OPT_PROFILE_UUID, profile.getUuid());
204 Data.profile.set(profile);
207 holder.itemView.setTag(profile);
208 holder.mEditButton.setOnClickListener(mOnClickListener);
212 public int getItemCount() {
213 return Data.profiles.size();
215 class ProfileListViewHolder extends RecyclerView.ViewHolder {
216 final RadioButton mRadioView;
217 final TextView mEditButton;
218 final TextView mTitle, mSubTitle;
220 ProfileListViewHolder(View view) {
222 mRadioView = view.findViewById(R.id.profile_list_radio);
223 mEditButton = view.findViewById(R.id.profile_list_edit_button);
224 mTitle = view.findViewById(R.id.title);
225 mSubTitle = view.findViewById(R.id.subtitle);