]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java
1643b9e43819b97b52ca0d0f2807b1d307caafb0
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / ProfileListActivity.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.ui.activity;
19
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.v7.app.ActionBar;
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.Menu;
33 import android.view.View;
34 import android.view.ViewGroup;
35 import android.widget.RadioButton;
36 import android.widget.TextView;
37
38 import net.ktnx.mobileledger.R;
39 import net.ktnx.mobileledger.model.Data;
40 import net.ktnx.mobileledger.model.MobileLedgerProfile;
41 import net.ktnx.mobileledger.ui.profiles.ProfileDetailActivity;
42 import net.ktnx.mobileledger.ui.profiles.ProfileDetailFragment;
43
44 import java.util.Collections;
45
46 /**
47  * An activity representing a list of Profiles. This activity
48  * has different presentations for handset and tablet-size devices. On
49  * handsets, the activity presents a list of items, which when touched,
50  * lead to a {@link ProfileDetailActivity} representing
51  * item details. On tablets, the activity presents the list of items and
52  * item details side-by-side using two vertical panes.
53  */
54 public class ProfileListActivity extends AppCompatActivity {
55
56     public static final String ARG_ACTION = "action";
57     public static final String ARG_PROFILE_INDEX = "profile_index";
58     public static final int PROFILE_INDEX_NONE = -1;
59     public static final int ACTION_EDIT_PROFILE = 1;
60     public static final int ACTION_INVALID = -1;
61     /**
62      * Whether or not the activity is in two-pane mode, i.e. running on a tablet
63      * device.
64      */
65     private boolean mTwoPane;
66     private RecyclerView recyclerView;
67
68     @Override
69     public boolean onSupportNavigateUp() {
70         onBackPressed();
71         return super.onSupportNavigateUp();
72     }
73     @Override
74     protected void onCreate(Bundle savedInstanceState) {
75         super.onCreate(savedInstanceState);
76         setContentView(R.layout.activity_profile_list);
77
78         Toolbar toolbar = findViewById(R.id.toolbar);
79         setSupportActionBar(toolbar);
80         toolbar.setTitle(getTitle());
81         final ActionBar supportActionBar = getSupportActionBar();
82         if (supportActionBar != null) {
83             supportActionBar.setDisplayHomeAsUpEnabled(true);
84             supportActionBar.setDisplayShowHomeEnabled(true);
85         }
86
87         recyclerView = findViewById(R.id.profile_list);
88         if (recyclerView == null) throw new AssertionError();
89         setupRecyclerView(recyclerView);
90
91         if (findViewById(R.id.profile_detail_container) != null) {
92             // The detail container view will be present only in the
93             // large-screen layouts (res/values-w900dp).
94             // If this view is present, then the
95             // activity should be in two-pane mode.
96             mTwoPane = true;
97         }
98
99         int action = getIntent().getIntExtra(ARG_ACTION, ACTION_INVALID);
100         if (action == ACTION_EDIT_PROFILE) {
101             Log.d("profiles", "got edit profile action");
102             int index = getIntent().getIntExtra(ARG_PROFILE_INDEX, PROFILE_INDEX_NONE);
103
104             MobileLedgerProfile profile = (index >= 0) ? Data.profiles.get(index) : null;
105             ProfilesRecyclerViewAdapter adapter = (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
106             if (adapter != null) {
107                 adapter.editProfile(recyclerView, profile);
108
109                 // if invoked from the initial screen, get out so that when the new profile
110                 // activity finishes the user i navigated to the main activity
111                 if ((profile == null) && Data.profiles.getList().isEmpty()) finish();
112             }
113         }
114     }
115     void launchNewProfileActivity() {
116         ProfilesRecyclerViewAdapter adapter =
117                 (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
118         if (adapter != null) adapter.editProfile(recyclerView, null);
119     }
120     private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
121         final ProfilesRecyclerViewAdapter adapter = new ProfilesRecyclerViewAdapter(this, mTwoPane);
122         recyclerView.setAdapter(adapter);
123         ItemTouchHelper.Callback cb = new ItemTouchHelper.Callback() {
124             @Override
125             public int getMovementFlags(@NonNull RecyclerView recyclerView,
126                                         @NonNull RecyclerView.ViewHolder viewHolder) {
127                 return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0);
128             }
129             @Override
130             public boolean onMove(@NonNull RecyclerView recyclerView,
131                                   @NonNull RecyclerView.ViewHolder viewHolder,
132                                   @NonNull RecyclerView.ViewHolder target) {
133                 Collections.swap(Data.profiles.getList(), viewHolder.getAdapterPosition(),
134                         target.getAdapterPosition());
135                 MobileLedgerProfile.storeProfilesOrder();
136                 adapter.notifyItemMoved(viewHolder.getAdapterPosition(),
137                         target.getAdapterPosition());
138                 return true;
139             }
140             @Override
141             public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
142
143             }
144         };
145         new ItemTouchHelper(cb).attachToRecyclerView(recyclerView);
146         recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(),
147                 DividerItemDecoration.VERTICAL));
148     }
149     @Override
150     public boolean onCreateOptionsMenu(Menu menu) {
151         getMenuInflater().inflate(R.menu.profile_list, menu);
152         menu.findItem(R.id.menu_add_profile).setOnMenuItemClickListener(item -> {
153             launchNewProfileActivity();
154             return true;
155         });
156         return super.onCreateOptionsMenu(menu);
157     }
158     public static class ProfilesRecyclerViewAdapter
159             extends RecyclerView.Adapter<ProfilesRecyclerViewAdapter.ProfileListViewHolder> {
160
161         private final ProfileListActivity mParentActivity;
162         private final boolean mTwoPane;
163         private final View.OnClickListener mOnClickListener = view -> {
164             MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag();
165             editProfile(view, profile);
166         };
167         ProfilesRecyclerViewAdapter(ProfileListActivity parent, boolean twoPane) {
168             mParentActivity = parent;
169             mTwoPane = twoPane;
170             Data.profiles.addObserver((o, arg) -> {
171                 Log.d("profiles", "profile list changed");
172                 if (arg == null) notifyDataSetChanged();
173                 else notifyItemChanged((int) arg);
174             });
175         }
176         private void editProfile(View view, MobileLedgerProfile profile) {
177             int index = Data.profiles.indexOf(profile);
178             if (mTwoPane) {
179                 Bundle arguments = new Bundle();
180                 arguments.putInt(ProfileDetailFragment.ARG_ITEM_ID, index);
181                 ProfileDetailFragment fragment = new ProfileDetailFragment();
182                 fragment.setArguments(arguments);
183                 mParentActivity.getSupportFragmentManager().beginTransaction()
184                         .replace(R.id.profile_detail_container, fragment).commit();
185             }
186             else {
187                 Context context = view.getContext();
188                 Intent intent = new Intent(context, ProfileDetailActivity.class);
189                 intent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
190                 if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
191
192                 context.startActivity(intent);
193             }
194         }
195         @NonNull
196         @Override
197         public ProfileListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
198             View view = LayoutInflater.from(parent.getContext())
199                     .inflate(R.layout.profile_list_content, parent, false);
200             ProfileListViewHolder holder = new ProfileListViewHolder(view);
201
202             holder.mRadioView.setOnCheckedChangeListener((buttonView, isChecked) -> {
203                 if (!isChecked) return;
204                 Log.d("profiles",
205                         String.format("Item %d got checked", holder.getAdapterPosition()));
206                 MobileLedgerProfile profile = (MobileLedgerProfile) holder.itemView.getTag();
207                 if (profile != null) {
208                     Log.d("profiles",
209                             String.format("Setting current profile to %s", profile.getUuid()));
210                     Data.setCurrentProfile(profile);
211                 }
212             });
213             View.OnClickListener profileSelector = v -> holder.mRadioView.setChecked(true);
214             holder.mTitle.setOnClickListener(profileSelector);
215             holder.mSubTitle.setOnClickListener(profileSelector);
216             Data.profile.addObserver((o, arg) -> {
217                 MobileLedgerProfile myProfile = (MobileLedgerProfile) holder.itemView.getTag();
218                 final MobileLedgerProfile currentProfile = Data.profile.get();
219                 final boolean sameProfile = currentProfile.equals(myProfile);
220                 if (holder.mRadioView.isChecked() != sameProfile) {
221                     holder.mRadioView.setChecked(sameProfile);
222                 }
223             });
224             return holder;
225         }
226         @Override
227         public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
228             final MobileLedgerProfile profile = Data.profiles.get(position);
229             final MobileLedgerProfile currentProfile = Data.profile.get();
230             Log.d("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(),
231                     (currentProfile == null) ? "<NULL>" : currentProfile.getUuid()));
232             holder.itemView.setTag(profile);
233             holder.mTitle.setText(profile.getName());
234             holder.mSubTitle.setText(profile.getUrl());
235             holder.mRadioView.setChecked(profile.equals(currentProfile));
236
237             holder.mEditButton.setOnClickListener(mOnClickListener);
238         }
239         @Override
240         public int getItemCount() {
241             return Data.profiles.size();
242         }
243         class ProfileListViewHolder extends RecyclerView.ViewHolder {
244             final RadioButton mRadioView;
245             final TextView mEditButton;
246             final TextView mTitle, mSubTitle;
247
248             ProfileListViewHolder(View view) {
249                 super(view);
250                 mRadioView = view.findViewById(R.id.profile_list_radio);
251                 mEditButton = view.findViewById(R.id.profile_list_edit_button);
252                 mTitle = view.findViewById(R.id.title);
253                 mSubTitle = view.findViewById(R.id.subtitle);
254             }
255         }
256     }
257 }