From 85be9af4098518974fc7ae1add1a5d58f5b4d995 Mon Sep 17 00:00:00 2001
From: "Daniele Verducci (Slimpenguin)" <daniele.verducci@gmail.com>
Date: Tue, 1 Feb 2022 12:50:44 +0100
Subject: [PATCH] Add and reorder cams is working

---
 .idea/misc.xml                                |  3 ++
 .../ojo/ui/SettingsFragment.java              | 45 +++++++++++++------
 .../adapters/SettingsRecyclerViewAdapter.java |  4 ++
 app/src/main/res/drawable/ic_add.xml          |  9 ++++
 app/src/main/res/drawable/ic_add_camera.xml   | 10 -----
 app/src/main/res/drawable/ic_settings.xml     | 10 +++++
 app/src/main/res/layout/activity_main.xml     |  2 +-
 .../main/res/layout/fragment_add_stream.xml   |  2 +-
 .../layout/fragment_settings_item_list.xml    |  3 +-
 app/src/main/res/menu/settings_menu.xml       |  2 +-
 app/src/main/res/values-it/strings.xml        |  1 +
 app/src/main/res/values/strings.xml           |  1 +
 app/src/main/res/values/themes.xml            |  5 ++-
 13 files changed, 69 insertions(+), 28 deletions(-)
 create mode 100644 app/src/main/res/drawable/ic_add.xml
 delete mode 100644 app/src/main/res/drawable/ic_add_camera.xml
 create mode 100644 app/src/main/res/drawable/ic_settings.xml

diff --git a/.idea/misc.xml b/.idea/misc.xml
index b6581c9..1af431a 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -3,6 +3,9 @@
   <component name="DesignSurface">
     <option name="filePathToZoomLevelMap">
       <map>
+        <entry key="../../../../layout/custom_preview.xml" value="0.36974358974358973" />
+        <entry key="app/src/main/res/drawable/ic_add.xml" value="0.1565" />
+        <entry key="app/src/main/res/drawable/ic_add_camera.xml" value="0.1565" />
         <entry key="app/src/main/res/layout/activity_main.xml" value="0.45" />
         <entry key="app/src/main/res/layout/fragment_add_stream.xml" value="0.524901185770751" />
         <entry key="app/src/main/res/layout/fragment_settings_item.xml" value="0.536" />
diff --git a/app/src/main/java/it/danieleverducci/ojo/ui/SettingsFragment.java b/app/src/main/java/it/danieleverducci/ojo/ui/SettingsFragment.java
index ed4f8d8..a4b35d8 100644
--- a/app/src/main/java/it/danieleverducci/ojo/ui/SettingsFragment.java
+++ b/app/src/main/java/it/danieleverducci/ojo/ui/SettingsFragment.java
@@ -5,6 +5,7 @@ import android.os.Bundle;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
+import androidx.appcompat.widget.Toolbar;
 import androidx.fragment.app.Fragment;
 import androidx.recyclerview.widget.GridLayoutManager;
 import androidx.recyclerview.widget.ItemTouchHelper;
@@ -31,14 +32,38 @@ import it.danieleverducci.ojo.utils.ItemMoveCallback;
 public class SettingsFragment extends Fragment {
 
     private FragmentSettingsItemListBinding binding;
+    private Settings settings;
 
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
         binding = FragmentSettingsItemListBinding.inflate(inflater, container, false);
 
+        return binding.getRoot();
+    }
+
+    @Override
+    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
+        // Setup toolbar and register for item click
+        binding.settingsToolbar.inflateMenu(R.menu.settings_menu);
+        binding.settingsToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
+            @Override
+            public boolean onMenuItemClick(MenuItem item) {
+                if (item.getItemId() == R.id.menuitem_add_camera) {
+                    ((MainActivity)getActivity()).navigateToFragment(R.id.action_settingsToCameraUrl);
+                    return true;
+                }
+                return false;
+            }
+        });
+    }
+
+    @Override
+    public void onResume() {
+        super.onResume();
+
         // Load cameras
-        Settings settings = Settings.fromDisk(getContext());
+        settings = Settings.fromDisk(getContext());
         List<Camera> cams = settings.getCameras();
 
         // Set the adapter
@@ -60,21 +85,15 @@ public class SettingsFragment extends Fragment {
                 ((MainActivity)getActivity()).navigateToFragment(R.id.action_settingsToCameraUrl, b);
             }
         });
-
-        return binding.getRoot();
     }
 
     @Override
-    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
-        binding.settingsToolbar.inflateMenu(R.menu.settings_menu);
-    }
+    public void onPause() {
+        super.onPause();
 
-    @Override
-    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
-        if (item.getItemId() == R.id.menuitem_add_camera) {
-            ((MainActivity)getActivity()).navigateToFragment(R.id.action_settingsToCameraUrl);
-            return true;
-        }
-        return super.onOptionsItemSelected(item);
+        // Save cameras
+        List<Camera> cams = ((SettingsRecyclerViewAdapter)binding.list.getAdapter()).getItems();
+        this.settings.setCameras(cams);
+        this.settings.save();
     }
 }
\ No newline at end of file
diff --git a/app/src/main/java/it/danieleverducci/ojo/ui/adapters/SettingsRecyclerViewAdapter.java b/app/src/main/java/it/danieleverducci/ojo/ui/adapters/SettingsRecyclerViewAdapter.java
index c5dacbe..7ffa2f3 100644
--- a/app/src/main/java/it/danieleverducci/ojo/ui/adapters/SettingsRecyclerViewAdapter.java
+++ b/app/src/main/java/it/danieleverducci/ojo/ui/adapters/SettingsRecyclerViewAdapter.java
@@ -97,6 +97,10 @@ public class SettingsRecyclerViewAdapter extends RecyclerView.Adapter<SettingsRe
         this.clickListener = clickListener;
     }
 
+    public List<Camera> getItems() {
+        return mValues;
+    }
+
     public class ViewHolder extends RecyclerView.ViewHolder {
         public View root;
         public TextView name;
diff --git a/app/src/main/res/drawable/ic_add.xml b/app/src/main/res/drawable/ic_add.xml
new file mode 100644
index 0000000..bdd99f4
--- /dev/null
+++ b/app/src/main/res/drawable/ic_add.xml
@@ -0,0 +1,9 @@
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+    android:width="24dp"
+    android:height="24dp"
+    android:viewportWidth="24"
+    android:viewportHeight="24">
+  <path
+      android:fillColor="@android:color/white"
+      android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
+</vector>
diff --git a/app/src/main/res/drawable/ic_add_camera.xml b/app/src/main/res/drawable/ic_add_camera.xml
deleted file mode 100644
index b4f90ff..0000000
--- a/app/src/main/res/drawable/ic_add_camera.xml
+++ /dev/null
@@ -1,10 +0,0 @@
-<vector xmlns:android="http://schemas.android.com/apk/res/android"
-    android:width="24dp"
-    android:height="24dp"
-    android:viewportWidth="24"
-    android:viewportHeight="24"
-    android:tint="?attr/colorControlNormal">
-  <path
-      android:fillColor="@android:color/white"
-      android:pathData="M3,4V1h2v3h3v2H5v3H3V6H0V4H3zM6,10V7h3V4h7l1.83,2H21c1.1,0 2,0.9 2,2v12c0,1.1 -0.9,2 -2,2H5c-1.1,0 -2,-0.9 -2,-2V10H6zM13,19c2.76,0 5,-2.24 5,-5s-2.24,-5 -5,-5s-5,2.24 -5,5S10.24,19 13,19zM9.8,14c0,1.77 1.43,3.2 3.2,3.2s3.2,-1.43 3.2,-3.2s-1.43,-3.2 -3.2,-3.2S9.8,12.23 9.8,14z"/>
-</vector>
diff --git a/app/src/main/res/drawable/ic_settings.xml b/app/src/main/res/drawable/ic_settings.xml
new file mode 100644
index 0000000..41a82ed
--- /dev/null
+++ b/app/src/main/res/drawable/ic_settings.xml
@@ -0,0 +1,10 @@
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+    android:width="24dp"
+    android:height="24dp"
+    android:viewportWidth="24"
+    android:viewportHeight="24"
+    android:tint="?attr/colorControlNormal">
+  <path
+      android:fillColor="@android:color/white"
+      android:pathData="M19.14,12.94c0.04,-0.3 0.06,-0.61 0.06,-0.94c0,-0.32 -0.02,-0.64 -0.07,-0.94l2.03,-1.58c0.18,-0.14 0.23,-0.41 0.12,-0.61l-1.92,-3.32c-0.12,-0.22 -0.37,-0.29 -0.59,-0.22l-2.39,0.96c-0.5,-0.38 -1.03,-0.7 -1.62,-0.94L14.4,2.81c-0.04,-0.24 -0.24,-0.41 -0.48,-0.41h-3.84c-0.24,0 -0.43,0.17 -0.47,0.41L9.25,5.35C8.66,5.59 8.12,5.92 7.63,6.29L5.24,5.33c-0.22,-0.08 -0.47,0 -0.59,0.22L2.74,8.87C2.62,9.08 2.66,9.34 2.86,9.48l2.03,1.58C4.84,11.36 4.8,11.69 4.8,12s0.02,0.64 0.07,0.94l-2.03,1.58c-0.18,0.14 -0.23,0.41 -0.12,0.61l1.92,3.32c0.12,0.22 0.37,0.29 0.59,0.22l2.39,-0.96c0.5,0.38 1.03,0.7 1.62,0.94l0.36,2.54c0.05,0.24 0.24,0.41 0.48,0.41h3.84c0.24,0 0.44,-0.17 0.47,-0.41l0.36,-2.54c0.59,-0.24 1.13,-0.56 1.62,-0.94l2.39,0.96c0.22,0.08 0.47,0 0.59,-0.22l1.92,-3.32c0.12,-0.22 0.07,-0.47 -0.12,-0.61L19.14,12.94zM12,15.6c-1.98,0 -3.6,-1.62 -3.6,-3.6s1.62,-3.6 3.6,-3.6s3.6,1.62 3.6,3.6S13.98,15.6 12,15.6z"/>
+</vector>
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index ba4c96f..d7051a7 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -14,7 +14,7 @@
         android:layout_height="wrap_content"
         android:layout_gravity="bottom|end"
         android:layout_margin="@dimen/fab_margin"
-        app:srcCompat="@drawable/ic_add_camera"
+        app:srcCompat="@drawable/ic_settings"
         app:tint="@color/purple_500"/>
 
 </androidx.coordinatorlayout.widget.CoordinatorLayout>
\ No newline at end of file
diff --git a/app/src/main/res/layout/fragment_add_stream.xml b/app/src/main/res/layout/fragment_add_stream.xml
index 19b2b21..ae7db5d 100644
--- a/app/src/main/res/layout/fragment_add_stream.xml
+++ b/app/src/main/res/layout/fragment_add_stream.xml
@@ -29,7 +29,7 @@
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_marginTop="10dp"
-            android:hint="@string/add_stream_placeholder_url"
+            android:hint="@string/add_stream_placeholder_name"
             android:lines="1"
             android:maxLines="1"
             android:inputType="textUri"/>
diff --git a/app/src/main/res/layout/fragment_settings_item_list.xml b/app/src/main/res/layout/fragment_settings_item_list.xml
index 2e1eb9e..df78bee 100644
--- a/app/src/main/res/layout/fragment_settings_item_list.xml
+++ b/app/src/main/res/layout/fragment_settings_item_list.xml
@@ -10,7 +10,8 @@
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:id="@+id/settingsToolbar"
-        app:title="@string/app_name" />
+        app:title="@string/app_name"
+        style="@style/ToolBarStyle"/>
 
     <androidx.recyclerview.widget.RecyclerView
         android:id="@+id/list"
diff --git a/app/src/main/res/menu/settings_menu.xml b/app/src/main/res/menu/settings_menu.xml
index d9ab9f2..2e87800 100644
--- a/app/src/main/res/menu/settings_menu.xml
+++ b/app/src/main/res/menu/settings_menu.xml
@@ -2,7 +2,7 @@
 <menu xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto">
     <item android:id="@+id/menuitem_add_camera"
-        android:icon="@drawable/ic_add_camera"
+        android:icon="@drawable/ic_add"
         android:title="@string/add_stream"
         app:showAsAction="always"/>
 </menu>
\ No newline at end of file
diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml
index 025a77f..c0d3625 100644
--- a/app/src/main/res/values-it/strings.xml
+++ b/app/src/main/res/values-it/strings.xml
@@ -8,6 +8,7 @@
     <string name="stream_list_default_camera_name">Videocamera senza nome n°{camNo}</string>
 
     <string name="add_stream_placeholder_url">rtsp://username:password@192.168.1.123:554</string>
+    <string name="add_stream_placeholder_name">Nome della IP Camera</string>
     <string name="add_stream_name">Nome della IP Camera</string>
     <string name="add_stream">Inserisci l\'url dello stream RTSP della tua IP Camera. Nota che questo differisce tra un modello e l\'altro. Consulta il pannello di configurazione o il manuale della tua IP Camera.</string>
     <string name="add_stream_save">Salva</string>
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 6758831..08ba789 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -7,6 +7,7 @@
     <string name="stream_list_default_camera_name">Unnamed camera {camNo}</string>
 
     <string name="add_stream_placeholder_url">rtsp://username:password@192.168.1.123:554</string>
+    <string name="add_stream_placeholder_name">Camera name</string>
     <string name="add_stream_name">Camera name</string>
     <string name="add_stream">Please insert your camera\'s RTSP stream. Note that the URL differs from camera to camera: you can find the complete URL in your camera\'s settings or user manual.</string>
     <string name="add_stream_save">Save</string>
diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml
index 31f47b3..5e96b3c 100644
--- a/app/src/main/res/values/themes.xml
+++ b/app/src/main/res/values/themes.xml
@@ -14,6 +14,9 @@
         <item name="colorOnSecondary">@color/white</item>
     </style>
 
-
+    <style name="ToolBarStyle" parent="">
+        <item name="android:background">@color/purple_500</item>
+        <item name="titleTextColor">@color/white</item>
+    </style>
 
 </resources>
\ No newline at end of file