Skip to content

Commit

Permalink
Merge pull request #1677 from danishjamal104/fix-1671
Browse files Browse the repository at this point in the history
Fixes #1671 Search functionality added in individual collection sheet
  • Loading branch information
iamsh4shank committed Jul 20, 2021
2 parents ebf60ff + 315450f commit 14d4345
Show file tree
Hide file tree
Showing 6 changed files with 283 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.mifos.mifosxdroid.dialogfragments.searchdialog;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.mifos.mifosxdroid.R;

import java.util.ArrayList;
import java.util.List;

public class SearchDialog extends Dialog {

private List<String> mainList;
private List<String> filterList;
private AdapterView.OnItemClickListener clickListener;

private EditText editText;
private ListView listView;
private ArrayAdapter<String> adapter;

public SearchDialog(@NonNull Context context, List<String> items,
AdapterView.OnItemClickListener clickListener) {
super(context);
this.clickListener = clickListener;
this.mainList = items;
setUp();
}

private void setUp() {
filterList = new ArrayList<>();
filterList.addAll(mainList);
setContentView(0);

adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1,
android.R.id.text1, filterList);
listView = this.findViewById(R.id.lv_items);
editText = this.findViewById(R.id.et_drop_down_search);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { }
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { }
@Override
public void afterTextChanged(Editable editable) {
filterList.clear();
String text = editable.toString().toLowerCase();
for (String s: mainList) {
if (s.toLowerCase().contains(text)) {
filterList.add(s);
}
}
adapter.notifyDataSetChanged();
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
for (int pos = 0; pos < mainList.size(); pos++) {
if (mainList.get(pos).equals(filterList.get(i))) {
clickListener.onItemClick(adapterView, view, pos, l);
}
}
dismiss();
}
});
listView.setAdapter(adapter);
}

@Override
public void setContentView(int layoutResID) {
layoutResID = R.layout.search_list_dialog_layout;
super.setContentView(layoutResID);
}

@Override
@Deprecated
public void setContentView(@NonNull View view) {
super.setContentView(view);
}

@Override
@Deprecated
public void addContentView(@NonNull View view, @Nullable ViewGroup.LayoutParams params) {
super.addContentView(view, params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ import com.mifos.mifosxdroid.core.MifosBaseActivity
import com.mifos.mifosxdroid.core.MifosBaseFragment
import com.mifos.mifosxdroid.core.util.Toaster
import com.mifos.mifosxdroid.dialogfragments.collectionsheetdialog.CollectionSheetDialogFragment
import com.mifos.mifosxdroid.dialogfragments.searchdialog.SearchDialog
import com.mifos.mifosxdroid.online.collectionsheetindividualdetails.IndividualCollectionSheetDetailsFragment
import com.mifos.mifosxdroid.uihelpers.MFDatePicker
import com.mifos.mifosxdroid.uihelpers.MFDatePicker.OnDatePickListener
import com.mifos.mifosxdroid.views.CustomSpinner
import com.mifos.mifosxdroid.views.CustomSpinner.OnSpinnerEventsListener
import com.mifos.objects.collectionsheet.IndividualCollectionSheet
import com.mifos.objects.organisation.Office
import com.mifos.objects.organisation.Staff
Expand All @@ -39,11 +42,11 @@ class NewIndividualCollectionSheetFragment : MifosBaseFragment(), IndividualColl

@JvmField
@BindView(R.id.sp_office_list)
var spOffices: Spinner? = null
var spOffices: CustomSpinner? = null

@JvmField
@BindView(R.id.sp_staff_list)
var spStaff: Spinner? = null
var spStaff: CustomSpinner? = null

@JvmField
@BindView(R.id.tv_repayment_date)
Expand Down Expand Up @@ -72,6 +75,10 @@ class NewIndividualCollectionSheetFragment : MifosBaseFragment(), IndividualColl
private var success = true
private var actualDisbursementDate: String? = null
private var transactionDate: String? = null

private var officeSearchDialog: SearchDialog? = null
private var staffSearchDialog: SearchDialog? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
(activity as MifosBaseActivity?)!!.activityComponent.inject(this)
Expand Down Expand Up @@ -107,6 +114,43 @@ class NewIndividualCollectionSheetFragment : MifosBaseFragment(), IndividualColl
tvRepaymentDate!!.setOnClickListener(this)
btnFetchSheet!!.setOnClickListener(this)
presenter!!.fetchOffices()

spOffices!!.setSpinnerEventsListener(object : OnSpinnerEventsListener {
override fun onSpinnerOpened(spinner: Spinner, isItemListLarge: Boolean) {
if (isItemListLarge) {
enableOfficeSearch()
}
}

override fun onSpinnerClosed(spinner: Spinner) {}
})

spStaff!!.setSpinnerEventsListener(object : OnSpinnerEventsListener {
override fun onSpinnerOpened(spinner: Spinner, isItemListLarge: Boolean) {
if (isItemListLarge) {
enableStaffSearch()
}
}

override fun onSpinnerClosed(spinner: Spinner) {}
})

}

fun enableOfficeSearch() {
if (officeSearchDialog == null) {
val listener = AdapterView.OnItemClickListener { adapterView, view, i, l -> spOffices!!.setSelection(i) }
officeSearchDialog = SearchDialog(requireContext(), officeNameList, listener)
}
officeSearchDialog!!.show()
}

fun enableStaffSearch() {
if (staffSearchDialog == null) {
val listener = AdapterView.OnItemClickListener { adapterView, view, i, l -> spStaff!!.setSelection(i) }
staffSearchDialog = SearchDialog(requireContext(), staffNameList, listener)
}
staffSearchDialog!!.show()
}

fun setRepaymentDate() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.mifos.mifosxdroid.views;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.widget.Spinner;

public class CustomSpinner extends androidx.appcompat.widget.AppCompatSpinner {

public interface OnSpinnerEventsListener {

/**
* Callback triggered when the spinner was opened.
*/
void onSpinnerOpened(Spinner spinner, boolean isItemListLarge);

/**
* Callback triggered when the spinner was closed.
*/
void onSpinnerClosed(Spinner spinner);

}

private OnSpinnerEventsListener mListener;
private boolean mOpenInitiated = false;

@Override
public boolean performClick() {
// register that the Spinner was opened so we have a status
// indicator for when the container holding this Spinner may lose focus
mOpenInitiated = true;
if (mListener != null) {
if (super.getAdapter().getCount() >= 10) {
mListener.onSpinnerOpened(this, true);
return true;
} else {
mListener.onSpinnerOpened(this, false);
}
}
return super.performClick();
}

@Override
public void onWindowFocusChanged (boolean hasFocus) {
if (hasBeenOpened() && hasFocus) {
performClosedEvent();
}
}

/**
* Register the listener which will listen for events.
*/
public void setSpinnerEventsListener(
OnSpinnerEventsListener onSpinnerEventsListener) {
mListener = onSpinnerEventsListener;
}

/**
* Propagate the closed Spinner event to the listener from outside if needed.
*/
public void performClosedEvent() {
mOpenInitiated = false;
if (mListener != null) {
mListener.onSpinnerClosed(this);
}
}


/**
* A boolean flag indicating that the Spinner triggered an open event.
*
* @return true for opened Spinner
*/
public boolean hasBeenOpened() {
return mOpenInitiated;
}

public CustomSpinner(Context context) {
super(context);
}

public CustomSpinner(Context context, int mode) {
super(context, mode);
}

public CustomSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}

public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr, int mode) {
super(context, attrs, defStyleAttr, mode);
}

public CustomSpinner(Context context, AttributeSet attrs,
int defStyleAttr, int mode, Resources.Theme popupTheme) {
super(context, attrs, defStyleAttr, mode, popupTheme);
}
}
8 changes: 8 additions & 0 deletions mifosng-android/src/main/res/drawable/round_outline_bg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<stroke android:color="@color/primary" android:width="2dp"/>
<corners android:radius="2dp"/>

</shape>
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
android:layout_marginTop="5dp"
android:layout_marginBottom="10dp"/>

<androidx.appcompat.widget.AppCompatSpinner
<com.mifos.mifosxdroid.views.CustomSpinner
style="@style/spinner_style"
android:layout_height="wrap_content"
android:layout_width="match_parent"
Expand All @@ -41,7 +41,7 @@
android:layout_marginBottom="40dp"
android:text="@string/repayment_date"/>

<androidx.appcompat.widget.AppCompatSpinner
<com.mifos.mifosxdroid.views.CustomSpinner
style="@style/spinner_style"
android:layout_height="wrap_content"
android:layout_width="match_parent"
Expand Down
28 changes: 28 additions & 0 deletions mifosng-android/src/main/res/layout/search_list_dialog_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
android:id="@+id/et_drop_down_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search"
android:padding="8dp"
android:background="@drawable/round_outline_bg"
android:visibility="visible"
android:layout_margin="12dp"/>

<ListView
android:id="@+id/lv_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et_drop_down_search"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginEnd="12dp"/>


</RelativeLayout>

0 comments on commit 14d4345

Please sign in to comment.