From 84716495237add6db52384389404e35cc7b02dd0 Mon Sep 17 00:00:00 2001
From: Vatsal <patelvatsalb21@gmail.com>
Date: Thu, 22 Apr 2021 14:36:56 +0530
Subject: [PATCH] fix 323: Improved in Adding/Editing Members or Leaders

---
 .../creategroup/AddGroupLeaderStepFragment.kt | 78 +++++++++---------
 .../creategroup/AddGroupMemberStepFragment.kt | 79 ++++++++++---------
 .../layout/fragment_step_add_group_leader.xml | 47 -----------
 .../layout/fragment_step_add_group_member.xml | 47 -----------
 .../main/res/layout/member_leader_layout.xml  | 53 +++++++++++++
 app/src/main/res/values/strings.xml           |  5 ++
 6 files changed, 137 insertions(+), 172 deletions(-)
 create mode 100644 app/src/main/res/layout/member_leader_layout.xml

diff --git a/app/src/main/java/org/apache/fineract/ui/online/groups/creategroup/AddGroupLeaderStepFragment.kt b/app/src/main/java/org/apache/fineract/ui/online/groups/creategroup/AddGroupLeaderStepFragment.kt
index 55c2e4d6..f8bd2a95 100644
--- a/app/src/main/java/org/apache/fineract/ui/online/groups/creategroup/AddGroupLeaderStepFragment.kt
+++ b/app/src/main/java/org/apache/fineract/ui/online/groups/creategroup/AddGroupLeaderStepFragment.kt
@@ -1,5 +1,6 @@
 package org.apache.fineract.ui.online.groups.creategroup
 
+import android.app.AlertDialog
 import android.content.DialogInterface
 import android.os.Bundle
 import android.view.LayoutInflater
@@ -12,10 +13,9 @@ import butterknife.Optional
 import com.stepstone.stepper.Step
 import com.stepstone.stepper.VerificationError
 import com.wajahatkarim3.easyvalidation.core.view_ktx.validator
-import kotlinx.android.synthetic.main.fragment_step_add_group_leader.*
 import kotlinx.android.synthetic.main.fragment_step_add_group_leader.view.*
-import kotlinx.android.synthetic.main.fragment_step_add_group_member.view.*
 import kotlinx.android.synthetic.main.fragment_step_add_group_member.view.rv_name
+import kotlinx.android.synthetic.main.member_leader_layout.view.*
 import org.apache.fineract.R
 import org.apache.fineract.ui.adapters.NameListAdapter
 import org.apache.fineract.ui.base.FineractBaseActivity
@@ -89,36 +89,7 @@ class AddGroupLeaderStepFragment : FineractBaseFragment(), Step, NameListAdapter
 
     private fun showAddLeaderView(action: GroupAction, name: String?) {
         currentAction = action
-        llAddLeader.visibility = View.VISIBLE
-        when (action) {
-            GroupAction.CREATE -> {
-                btnAddLeader.text = getString(R.string.add)
-            }
-            GroupAction.EDIT -> {
-                etNewLeader.setText(name)
-                btnAddLeader.text = getString(R.string.update)
-            }
-        }
-    }
-
-    @Optional
-    @OnClick(R.id.btnAddLeader)
-    fun addLeader() {
-        if (etNewLeader.validator()
-                        .nonEmpty()
-                        .noNumbers()
-                        .addErrorCallback { etNewLeader.error = it }.check()) {
-            if (currentAction == GroupAction.CREATE) {
-                leaders.add(etNewLeader.text.toString())
-            } else {
-                leaders[editItemPosition] = etNewLeader.text.toString()
-            }
-            etNewLeader.text.clear()
-            llAddLeader.visibility = View.GONE
-            Utils.hideKeyboard(context, etNewLeader)
-            showRecyclerView(true)
-            nameLisAdapter.submitList(leaders)
-        }
+        showAddEditDialog(action, name)
     }
 
     fun showRecyclerView(isShow: Boolean) {
@@ -132,13 +103,6 @@ class AddGroupLeaderStepFragment : FineractBaseFragment(), Step, NameListAdapter
 
     }
 
-    @Optional
-    @OnClick(R.id.btnCancelAddLeader)
-    fun cancelLeaderAddition() {
-        etNewLeader.text.clear()
-        llAddLeader.visibility = View.GONE
-    }
-
     override fun onSelected() {
     }
 
@@ -178,4 +142,40 @@ class AddGroupLeaderStepFragment : FineractBaseFragment(), Step, NameListAdapter
         }.run { show() }
     }
 
+    private fun showAddEditDialog(action: GroupAction, name: String?) {
+        val layout = layoutInflater.inflate(R.layout.member_leader_layout, null)
+        val dialog = AlertDialog.Builder(context)
+                .setView(layout)
+                .create()
+        dialog.show()
+
+        if (action == GroupAction.CREATE) {
+            layout.btnAddMember.text = getString(R.string.add)
+            layout.tv_member_head.text = getString(R.string.add_new_leader)
+        }
+        else if (action == GroupAction.EDIT) {
+            layout.etNewMember.setText(name)
+            layout.btnAddMember.text = getString(R.string.update)
+            layout.tv_member_head.text = getString(R.string.edit_leader)
+        }
+
+        layout.btnAddMember.setOnClickListener {
+            if (layout.etNewMember.validator()
+                            .nonEmpty()
+                            .noNumbers()
+                            .addErrorCallback { layout.etNewMember.error = it }.check()) {
+                if (currentAction == GroupAction.CREATE) {
+                    leaders.add(layout.etNewMember.text.toString())
+                } else {
+                    leaders[editItemPosition] = layout.etNewMember.text.toString()
+                }
+                Utils.hideKeyboard(context, layout.etNewMember)
+                showRecyclerView(true)
+                nameLisAdapter.submitList(leaders)
+                dialog.dismiss()
+            }
+        }
+
+        layout.btnCancelAddMember.setOnClickListener { dialog.dismiss() }
+    }
 }
\ No newline at end of file
diff --git a/app/src/main/java/org/apache/fineract/ui/online/groups/creategroup/AddGroupMemberStepFragment.kt b/app/src/main/java/org/apache/fineract/ui/online/groups/creategroup/AddGroupMemberStepFragment.kt
index b7fe562e..33ae353d 100644
--- a/app/src/main/java/org/apache/fineract/ui/online/groups/creategroup/AddGroupMemberStepFragment.kt
+++ b/app/src/main/java/org/apache/fineract/ui/online/groups/creategroup/AddGroupMemberStepFragment.kt
@@ -1,5 +1,6 @@
 package org.apache.fineract.ui.online.groups.creategroup
 
+import android.app.AlertDialog
 import android.content.DialogInterface
 import android.os.Bundle
 import android.view.LayoutInflater
@@ -12,8 +13,8 @@ import butterknife.Optional
 import com.stepstone.stepper.Step
 import com.stepstone.stepper.VerificationError
 import com.wajahatkarim3.easyvalidation.core.view_ktx.validator
-import kotlinx.android.synthetic.main.fragment_step_add_group_member.*
 import kotlinx.android.synthetic.main.fragment_step_add_group_member.view.*
+import kotlinx.android.synthetic.main.member_leader_layout.view.*
 import org.apache.fineract.R
 import org.apache.fineract.ui.adapters.NameListAdapter
 import org.apache.fineract.ui.base.FineractBaseActivity
@@ -87,36 +88,7 @@ class AddGroupMemberStepFragment : FineractBaseFragment(), Step, NameListAdapter
 
     private fun showAddMemberView(action: GroupAction, name: String?) {
         currentAction = action
-        llAddMember.visibility = View.VISIBLE
-        when (action) {
-            GroupAction.CREATE -> {
-                btnAddMember.text = getString(R.string.add)
-            }
-            GroupAction.EDIT -> {
-                etNewMember.setText(name)
-                btnAddMember.text = getString(R.string.update)
-            }
-        }
-    }
-
-    @Optional
-    @OnClick(R.id.btnAddMember)
-    fun addMember() {
-        if (etNewMember.validator()
-                        .nonEmpty()
-                        .noNumbers()
-                        .addErrorCallback { etNewMember.error = it }.check()) {
-            if (currentAction == GroupAction.CREATE) {
-                members.add(etNewMember.text.toString())
-            } else {
-                members[editItemPosition] = etNewMember.text.toString()
-            }
-            etNewMember.text.clear()
-            llAddMember.visibility = View.GONE
-            Utils.hideKeyboard(context, etNewMember)
-            showRecyclerView(true)
-            nameLisAdapter.submitList(members)
-        }
+        showAddEditDialog(action, name)
     }
 
     fun showRecyclerView(isShow: Boolean) {
@@ -127,14 +99,6 @@ class AddGroupMemberStepFragment : FineractBaseFragment(), Step, NameListAdapter
             rootView.rv_name.visibility = View.GONE
             rootView.tvAddedMember.visibility = View.VISIBLE
         }
-
-    }
-
-    @Optional
-    @OnClick(R.id.btnCancelAddMember)
-    fun cancelMemberAddition() {
-        etNewMember.text.clear()
-        llAddMember.visibility = View.GONE
     }
 
     override fun onSelected() {
@@ -175,4 +139,41 @@ class AddGroupMemberStepFragment : FineractBaseFragment(), Step, NameListAdapter
             createMaterialDialog()
         }.run { show() }
     }
+
+    private fun showAddEditDialog(action: GroupAction, name: String?) {
+        val layout = layoutInflater.inflate(R.layout.member_leader_layout, null)
+        val dialog = AlertDialog.Builder(context)
+                .setView(layout)
+                .create()
+        dialog.show()
+
+        if (action == GroupAction.CREATE) {
+            layout.btnAddMember.text = getString(R.string.add)
+            layout.tv_member_head.text = getString(R.string.add_new_member)
+        }
+        else if (action == GroupAction.EDIT) {
+            layout.etNewMember.setText(name)
+            layout.btnAddMember.text = getString(R.string.update)
+            layout.tv_member_head.text = getString(R.string.edit_member)
+        }
+
+        layout.btnAddMember.setOnClickListener {
+            if (layout.etNewMember.validator()
+                            .nonEmpty()
+                            .noNumbers()
+                            .addErrorCallback { layout.etNewMember.error = it }.check()) {
+                if (currentAction == GroupAction.CREATE) {
+                    members.add(layout.etNewMember.text.toString())
+                } else {
+                    members[editItemPosition] = layout.etNewMember.text.toString()
+                }
+                Utils.hideKeyboard(context, layout.etNewMember)
+                showRecyclerView(true)
+                nameLisAdapter.submitList(members)
+                dialog.dismiss()
+            }
+        }
+
+        layout.btnCancelAddMember.setOnClickListener { dialog.dismiss() }
+    }
 }
\ No newline at end of file
diff --git a/app/src/main/res/layout/fragment_step_add_group_leader.xml b/app/src/main/res/layout/fragment_step_add_group_leader.xml
index b49c067c..164b54c1 100644
--- a/app/src/main/res/layout/fragment_step_add_group_leader.xml
+++ b/app/src/main/res/layout/fragment_step_add_group_leader.xml
@@ -61,53 +61,6 @@
                         android:text="@string/no_group_leader_added"
                         android:visibility="visible" />
 
-                    <LinearLayout
-                        android:id="@+id/llAddLeader"
-                        android:layout_width="match_parent"
-                        android:layout_height="wrap_content"
-                        android:layout_marginTop="@dimen/layout_padding_12dp"
-                        android:background="@drawable/border"
-                        android:orientation="vertical"
-                        android:padding="@dimen/layout_padding_16dp"
-                        android:visibility="gone">
-
-                        <com.google.android.material.textfield.TextInputLayout
-                            android:layout_width="match_parent"
-                            android:layout_height="wrap_content">
-
-                            <EditText
-                                android:id="@+id/etNewLeader"
-                                android:layout_width="match_parent"
-                                android:layout_height="wrap_content"
-                                android:hint="@string/name" />
-
-                        </com.google.android.material.textfield.TextInputLayout>
-
-                        <LinearLayout
-                            android:layout_width="match_parent"
-                            android:layout_height="wrap_content"
-                            android:layout_marginTop="@dimen/layout_padding_16dp"
-                            android:gravity="right">
-
-                            <Button
-                                android:id="@+id/btnCancelAddLeader"
-                                style="@style/Widget.MaterialComponents.Button.TextButton"
-                                android:layout_width="wrap_content"
-                                android:layout_height="wrap_content"
-                                android:text="@string/cancel"
-                                android:textSize="@dimen/text_size_14sp" />
-
-                            <Button
-                                android:id="@+id/btnAddLeader"
-                                android:layout_width="wrap_content"
-                                android:layout_height="wrap_content"
-                                android:text="@string/add" />
-
-                        </LinearLayout>
-
-                    </LinearLayout>
-
-
                 </LinearLayout>
 
                 <androidx.recyclerview.widget.RecyclerView
diff --git a/app/src/main/res/layout/fragment_step_add_group_member.xml b/app/src/main/res/layout/fragment_step_add_group_member.xml
index aac7ef54..ea6ec1f6 100644
--- a/app/src/main/res/layout/fragment_step_add_group_member.xml
+++ b/app/src/main/res/layout/fragment_step_add_group_member.xml
@@ -62,53 +62,6 @@
                         android:text="@string/no_group_member_added"
                         android:visibility="visible" />
 
-                    <LinearLayout
-                        android:id="@+id/llAddMember"
-                        android:layout_width="match_parent"
-                        android:layout_height="wrap_content"
-                        android:layout_marginTop="@dimen/layout_padding_12dp"
-                        android:background="@drawable/border"
-                        android:orientation="vertical"
-                        android:padding="@dimen/layout_padding_16dp"
-                        android:visibility="gone">
-
-                        <com.google.android.material.textfield.TextInputLayout
-                            android:layout_width="match_parent"
-                            android:layout_height="wrap_content">
-
-                            <EditText
-                                android:id="@+id/etNewMember"
-                                android:layout_width="match_parent"
-                                android:layout_height="wrap_content"
-                                android:hint="@string/name" />
-
-                        </com.google.android.material.textfield.TextInputLayout>
-
-                        <LinearLayout
-                            android:layout_width="match_parent"
-                            android:layout_height="wrap_content"
-                            android:layout_marginTop="@dimen/layout_padding_16dp"
-                            android:gravity="right">
-
-                            <Button
-                                android:id="@+id/btnCancelAddMember"
-                                style="@style/Widget.MaterialComponents.Button.TextButton"
-                                android:layout_width="wrap_content"
-                                android:layout_height="wrap_content"
-                                android:text="@string/cancel"
-                                android:textSize="@dimen/text_size_14sp" />
-
-                            <Button
-                                android:id="@+id/btnAddMember"
-                                android:layout_width="wrap_content"
-                                android:layout_height="wrap_content"
-                                android:text="@string/add" />
-
-                        </LinearLayout>
-
-                    </LinearLayout>
-
-
                 </LinearLayout>
 
                 <androidx.recyclerview.widget.RecyclerView
diff --git a/app/src/main/res/layout/member_leader_layout.xml b/app/src/main/res/layout/member_leader_layout.xml
new file mode 100644
index 00000000..2ba13007
--- /dev/null
+++ b/app/src/main/res/layout/member_leader_layout.xml
@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+        android:id="@+id/llAddMember"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="@dimen/layout_padding_12dp"
+        android:background="@drawable/border"
+        android:orientation="vertical"
+        android:padding="@dimen/layout_padding_16dp">
+
+    <TextView
+        android:id="@+id/tv_member_head"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="@string/add_new_member"
+        android:textColor="@color/colorPrimary"
+        android:layout_marginBottom="@dimen/layout_padding_16dp"
+        />
+
+        <com.google.android.material.textfield.TextInputLayout
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content">
+
+            <EditText
+                android:id="@+id/etNewMember"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:hint="@string/name" />
+
+        </com.google.android.material.textfield.TextInputLayout>
+
+        <LinearLayout
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:layout_marginTop="@dimen/layout_padding_16dp"
+            android:gravity="right">
+
+            <Button
+                android:id="@+id/btnCancelAddMember"
+                style="@style/Widget.MaterialComponents.Button.TextButton"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:text="@string/cancel"
+                android:textSize="@dimen/text_size_14sp" />
+
+            <Button
+                android:id="@+id/btnAddMember"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:text="@string/add" />
+
+        </LinearLayout>
+</LinearLayout>
\ No newline at end of file
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index af22aaa1..3af1958c 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -542,4 +542,9 @@
     <string name="please_wait_updating_group_status">"Updating group status, please wait... "</string>
     <string name="error_while_updating_group_status">Error while updating group status</string>
     <string name="error_while_creating_group">Error while creating group</string>
+
+    <string name="add_new_member">Add New Member</string>
+    <string name="edit_member">Edit Member</string>
+    <string name="add_new_leader">Add New Leader</string>
+    <string name="edit_leader">Edit Leader</string>
 </resources>