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.RecyclerView;
27 import android.support.v7.widget.Toolbar;
28 import android.util.Log;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.CompoundButton;
33 import android.widget.RadioButton;
34 import android.widget.TextView;
36 import net.ktnx.mobileledger.R;
37 import net.ktnx.mobileledger.model.Data;
38 import net.ktnx.mobileledger.model.MobileLedgerProfile;
39 import net.ktnx.mobileledger.ui.profiles.ProfileDetailActivity;
40 import net.ktnx.mobileledger.ui.profiles.ProfileDetailFragment;
41 import net.ktnx.mobileledger.utils.MLDB;
43 import java.util.List;
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;
68 protected void onCreate(Bundle savedInstanceState) {
69 super.onCreate(savedInstanceState);
70 setContentView(R.layout.activity_profile_list);
72 Toolbar 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 = (FloatingActionButton) findViewById(R.id.fab);
81 fab.setOnClickListener(new View.OnClickListener() {
83 public void onClick(View view) {
84 ProfilesRecyclerViewAdapter adapter =
85 (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
86 if (adapter != null) adapter.editProfile(recyclerView, null);
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, -1);
103 List<MobileLedgerProfile> list = MobileLedgerProfile.loadAllFromDB();
104 if (index < list.size()) {
105 ProfilesRecyclerViewAdapter adapter =
106 (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
107 if (adapter != null) adapter.editProfile(recyclerView, list.get(index));
113 private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
114 recyclerView.setAdapter(new ProfilesRecyclerViewAdapter(this, mTwoPane));
117 public static class ProfilesRecyclerViewAdapter
118 extends RecyclerView.Adapter<ProfilesRecyclerViewAdapter.ProfileListViewHolder> {
120 private final ProfileListActivity mParentActivity;
121 private final boolean mTwoPane;
122 private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
124 public void onClick(View view) {
125 MobileLedgerProfile item = (MobileLedgerProfile) ((View) view.getParent()).getTag();
126 editProfile(view, item);
129 ProfilesRecyclerViewAdapter(ProfileListActivity parent, boolean twoPane) {
130 mParentActivity = parent;
132 Data.profiles.addObserver((o, arg) ->{
133 Log.d("profiles", "profile list changed");
134 notifyDataSetChanged();
137 private void editProfile(View view, MobileLedgerProfile item) {
139 Bundle arguments = new Bundle();
140 arguments.putString(ProfileDetailFragment.ARG_ITEM_ID, item.getUuid());
141 ProfileDetailFragment fragment = new ProfileDetailFragment();
142 fragment.setArguments(arguments);
143 mParentActivity.getSupportFragmentManager().beginTransaction()
144 .replace(R.id.profile_detail_container, fragment).commit();
147 Context context = view.getContext();
148 Intent intent = new Intent(context, ProfileDetailActivity.class);
149 intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID,
150 (item == null) ? null : item.getUuid());
152 context.startActivity(intent);
157 public ProfileListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
158 View view = LayoutInflater.from(parent.getContext())
159 .inflate(R.layout.profile_list_content, parent, false);
160 ProfileListViewHolder holder = new ProfileListViewHolder(view);
161 Data.profile.addObserver(new Observer() {
163 public void update(Observable o, Object arg) {
164 MobileLedgerProfile newProfile = Data.profile.get();
165 MobileLedgerProfile profile = (MobileLedgerProfile) holder.itemView.getTag();
166 holder.mRadioView.setChecked(
167 newProfile != null && newProfile.getUuid().equals(profile.getUuid()));
173 public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
174 final MobileLedgerProfile profile = Data.profiles.get(position);
175 final MobileLedgerProfile currentProfile = Data.profile.get();
176 Log.d("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(),
177 currentProfile.getUuid()));
178 holder.mRadioView.setText(profile.getName());
179 holder.mRadioView.setChecked(profile.getUuid().equals(currentProfile.getUuid()));
181 .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
183 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
184 if (!isChecked) return;
185 MLDB.set_option_value(MLDB.OPT_PROFILE_UUID, profile.getUuid());
186 Data.profile.set(profile);
190 holder.itemView.setTag(profile);
191 holder.mEditButton.setOnClickListener(mOnClickListener);
195 public int getItemCount() {
196 return Data.profiles.size();
198 class ProfileListViewHolder extends RecyclerView.ViewHolder {
199 final RadioButton mRadioView;
200 final TextView mEditButton;
202 ProfileListViewHolder(View view) {
204 mRadioView = view.findViewById(R.id.profile_list_radio);
205 mEditButton = view.findViewById(R.id.profile_list_edit_button);