]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java
migrate to AndroidX
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / ProfileListActivity.java
index 98afa5191f74185ab043668ca659951a06a51de2..2fde36402e6dc9303b213638b2642de0015c0931 100644 (file)
@@ -1,34 +1,35 @@
 /*
  * Copyright © 2019 Damyan Ivanov.
- * This file is part of Mobile-Ledger.
- * Mobile-Ledger is free software: you can distribute it and/or modify it
+ * This file is part of MoLe.
+ * MoLe is free software: you can distribute it and/or modify it
  * under the term of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your opinion), any later version.
  *
- * Mobile-Ledger is distributed in the hope that it will be useful,
+ * MoLe is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  * GNU General Public License terms for details.
  *
  * You should have received a copy of the GNU General Public License
- * along with Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
+ * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
  */
 
 package net.ktnx.mobileledger.ui.activity;
 
 import android.content.Context;
 import android.content.Intent;
+import android.graphics.Color;
 import android.os.Bundle;
-import android.support.annotation.NonNull;
-import android.support.design.widget.FloatingActionButton;
-import android.support.v7.app.AppCompatActivity;
-import android.support.v7.widget.DividerItemDecoration;
-import android.support.v7.widget.RecyclerView;
-import android.support.v7.widget.Toolbar;
-import android.support.v7.widget.helper.ItemTouchHelper;
+import androidx.annotation.NonNull;
+import androidx.appcompat.app.ActionBar;
+import androidx.recyclerview.widget.DividerItemDecoration;
+import androidx.recyclerview.widget.RecyclerView;
+import androidx.appcompat.widget.Toolbar;
+import androidx.recyclerview.widget.ItemTouchHelper;
 import android.util.Log;
 import android.view.LayoutInflater;
+import android.view.Menu;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.RadioButton;
@@ -37,8 +38,8 @@ import android.widget.TextView;
 import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.MobileLedgerProfile;
-import net.ktnx.mobileledger.ui.profiles.ProfileDetailActivity;
 import net.ktnx.mobileledger.ui.profiles.ProfileDetailFragment;
+import net.ktnx.mobileledger.utils.Colors;
 
 import java.util.Collections;
 
@@ -50,7 +51,7 @@ import java.util.Collections;
  * item details. On tablets, the activity presents the list of items and
  * item details side-by-side using two vertical panes.
  */
-public class ProfileListActivity extends AppCompatActivity {
+public class ProfileListActivity extends CrashReportingActivity {
 
     public static final String ARG_ACTION = "action";
     public static final String ARG_PROFILE_INDEX = "profile_index";
@@ -62,8 +63,13 @@ public class ProfileListActivity extends AppCompatActivity {
      * device.
      */
     private boolean mTwoPane;
-    private FloatingActionButton fab;
+    private RecyclerView recyclerView;
 
+    @Override
+    public boolean onSupportNavigateUp() {
+        onBackPressed();
+        return super.onSupportNavigateUp();
+    }
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -72,18 +78,16 @@ public class ProfileListActivity extends AppCompatActivity {
         Toolbar toolbar = findViewById(R.id.toolbar);
         setSupportActionBar(toolbar);
         toolbar.setTitle(getTitle());
+        final ActionBar supportActionBar = getSupportActionBar();
+        if (supportActionBar != null) {
+            supportActionBar.setDisplayHomeAsUpEnabled(true);
+            supportActionBar.setDisplayShowHomeEnabled(true);
+        }
 
-        RecyclerView recyclerView = findViewById(R.id.profile_list);
+        recyclerView = findViewById(R.id.profile_list);
         if (recyclerView == null) throw new AssertionError();
         setupRecyclerView(recyclerView);
 
-        fab = findViewById(R.id.fab);
-        fab.setOnClickListener(view -> {
-            ProfilesRecyclerViewAdapter adapter =
-                    (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
-            if (adapter != null) adapter.editProfile(recyclerView, null);
-        });
-
         if (findViewById(R.id.profile_detail_container) != null) {
             // The detail container view will be present only in the
             // large-screen layouts (res/values-w900dp).
@@ -109,11 +113,10 @@ public class ProfileListActivity extends AppCompatActivity {
             }
         }
     }
-
-    @Override
-    protected void onStart() {
-        super.onStart();
-        fab.show();
+    void launchNewProfileActivity() {
+        ProfilesRecyclerViewAdapter adapter =
+                (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
+        if (adapter != null) adapter.editProfile(recyclerView, null);
     }
     private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
         final ProfilesRecyclerViewAdapter adapter = new ProfilesRecyclerViewAdapter(this, mTwoPane);
@@ -144,7 +147,15 @@ public class ProfileListActivity extends AppCompatActivity {
         recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(),
                 DividerItemDecoration.VERTICAL));
     }
-
+    @Override
+    public boolean onCreateOptionsMenu(Menu menu) {
+        getMenuInflater().inflate(R.menu.profile_list, menu);
+        menu.findItem(R.id.menu_add_profile).setOnMenuItemClickListener(item -> {
+            launchNewProfileActivity();
+            return true;
+        });
+        return super.onCreateOptionsMenu(menu);
+    }
     public static class ProfilesRecyclerViewAdapter
             extends RecyclerView.Adapter<ProfilesRecyclerViewAdapter.ProfileListViewHolder> {
 
@@ -220,6 +231,11 @@ public class ProfileListActivity extends AppCompatActivity {
             Log.d("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(),
                     (currentProfile == null) ? "<NULL>" : currentProfile.getUuid()));
             holder.itemView.setTag(profile);
+
+            int hue = profile.getThemeId();
+            if (hue == -1) holder.mColorTag.setBackgroundColor(Color.TRANSPARENT);
+            else holder.mColorTag.setBackgroundColor(Colors.getPrimaryColorForHue(hue));
+
             holder.mTitle.setText(profile.getName());
             holder.mSubTitle.setText(profile.getUrl());
             holder.mRadioView.setChecked(profile.equals(currentProfile));
@@ -233,7 +249,7 @@ public class ProfileListActivity extends AppCompatActivity {
         class ProfileListViewHolder extends RecyclerView.ViewHolder {
             final RadioButton mRadioView;
             final TextView mEditButton;
-            final TextView mTitle, mSubTitle;
+            final TextView mTitle, mSubTitle, mColorTag;
 
             ProfileListViewHolder(View view) {
                 super(view);
@@ -241,6 +257,7 @@ public class ProfileListActivity extends AppCompatActivity {
                 mEditButton = view.findViewById(R.id.profile_list_edit_button);
                 mTitle = view.findViewById(R.id.title);
                 mSubTitle = view.findViewById(R.id.subtitle);
+                mColorTag = view.findViewById(R.id.colorTag);
             }
         }
     }