From fde2f6df1b79de9d0b7c3666d21ec649e200f056 Mon Sep 17 00:00:00 2001 From: Apostolof Date: Fri, 4 Jan 2019 20:54:19 +0200 Subject: [PATCH] Add filter options --- .../activities/main/MainActivity.java | 103 ++++++++++++++++++ .../res/layout/dialog_filter_by_calories.xml | 16 +++ .../res/layout/dialog_filter_by_distance.xml | 16 +++ .../main/res/menu/main_filters_popup_menu.xml | 27 +++++ ..._spinner_type.xml => restaurant_types.xml} | 0 .../app/src/main/res/values/strings.xml | 8 ++ 6 files changed, 170 insertions(+) create mode 100644 UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/layout/dialog_filter_by_calories.xml create mode 100644 UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/layout/dialog_filter_by_distance.xml create mode 100644 UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/menu/main_filters_popup_menu.xml rename UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/values/{activity_add_restaurant_spinner_type.xml => restaurant_types.xml} (100%) diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/main/MainActivity.java b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/main/MainActivity.java index 60cf9cb..8a99459 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/main/MainActivity.java +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/java/gr/auth/databases/flavours/activities/main/MainActivity.java @@ -1,15 +1,22 @@ package gr.auth.databases.flavours.activities.main; +import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; +import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.widget.EditText; import com.google.android.material.floatingactionbutton.FloatingActionButton; +import java.util.ArrayList; + import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.widget.PopupMenu; import androidx.appcompat.widget.Toolbar; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; @@ -28,6 +35,8 @@ public class MainActivity extends BaseActivity { private ViewPager viewPager; private MenuItem menuMapItem; private FloatingActionButton FAB; + private ArrayList mSelectedRestaurantTypes; + private int maxDistance = -1, maxCalories = -1; @Override protected void onCreate(Bundle savedInstanceState) { @@ -104,6 +113,100 @@ public class MainActivity extends BaseActivity { int id = item.getItemId(); if (id == R.id.main_toolbar_menu_filter) { + View menuItemView = findViewById(R.id.main_toolbar_menu_filter); + PopupMenu popupMenu = new PopupMenu(this, menuItemView); + popupMenu.inflate(R.menu.main_filters_popup_menu); + popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { + @Override + public boolean onMenuItemClick(MenuItem item) { + AlertDialog.Builder builder = new AlertDialog.Builder(getThemedContext()); + LayoutInflater inflater = getLayoutInflater(); + + switch (item.getItemId()) { + case R.id.main_filters_type: + mSelectedRestaurantTypes = new ArrayList<>(); + + builder.setTitle(R.string.main_filters_menu_type) + .setMultiChoiceItems(R.array.restaurant_type, null, + new DialogInterface.OnMultiChoiceClickListener() { + @Override + public void onClick(DialogInterface dialog, int which, + boolean isChecked) { + if (isChecked) { + // If the user checked the item, add it to the selected items + mSelectedRestaurantTypes.add(which); + } else if (mSelectedRestaurantTypes.contains(which)) { + // Else, if the item is already in the array, remove it + mSelectedRestaurantTypes.remove(Integer.valueOf(which)); + } + } + }) + .setPositiveButton(R.string.main_filters_dialog_positive, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + //TODO + } + }) + .setNegativeButton(R.string.main_filters_dialog_cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + mSelectedRestaurantTypes.clear(); + } + }); + builder.create().show(); + break; + case R.id.main_filters_distance: + maxDistance = -1; + final View customDistanceDialogView = inflater.inflate(R.layout.dialog_filter_by_distance, null); + builder.setView(customDistanceDialogView) + .setTitle(R.string.main_filters_menu_distance) + .setPositiveButton(R.string.main_filters_dialog_positive, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + String distanceString = ((EditText) customDistanceDialogView + .findViewById(R.id.dialog_filter_distance)).getText().toString(); + if (!distanceString.isEmpty()) { + maxDistance = Integer.parseInt(distanceString); + } + } + }) + .setNegativeButton(R.string.main_filters_dialog_cancel, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + maxDistance = -1; + } + }); + builder.create().show(); + break; + case R.id.main_filters_calories: + maxCalories = -1; + final View customCaloriesDialogView = inflater.inflate(R.layout.dialog_filter_by_calories, null); + builder.setView(customCaloriesDialogView) + .setTitle(R.string.main_filters_menu_calories) + .setPositiveButton(R.string.main_filters_dialog_positive, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + String caloriesString = ((EditText) customCaloriesDialogView + .findViewById(R.id.dialog_filter_calories)).getText().toString(); + if (!caloriesString.isEmpty()) { + maxCalories = Integer.parseInt(caloriesString); + } + } + }) + .setNegativeButton(R.string.main_filters_dialog_cancel, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + maxCalories = -1; + } + }); + builder.create().show(); + break; + case R.id.main_filters_clear: + //TODO + break; + } + return false; + } + }); + popupMenu.show(); return true; } else if (id == R.id.main_toolbar_menu_map) { if (viewPager.getCurrentItem() == 0) { diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/layout/dialog_filter_by_calories.xml b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/layout/dialog_filter_by_calories.xml new file mode 100644 index 0000000..06e7ece --- /dev/null +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/layout/dialog_filter_by_calories.xml @@ -0,0 +1,16 @@ + + + + + diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/layout/dialog_filter_by_distance.xml b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/layout/dialog_filter_by_distance.xml new file mode 100644 index 0000000..f60bac5 --- /dev/null +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/layout/dialog_filter_by_distance.xml @@ -0,0 +1,16 @@ + + + + + diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/menu/main_filters_popup_menu.xml b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/menu/main_filters_popup_menu.xml new file mode 100644 index 0000000..72a378f --- /dev/null +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/menu/main_filters_popup_menu.xml @@ -0,0 +1,27 @@ + + + + + + + + + + \ No newline at end of file diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/values/activity_add_restaurant_spinner_type.xml b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/values/restaurant_types.xml similarity index 100% rename from UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/values/activity_add_restaurant_spinner_type.xml rename to UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/values/restaurant_types.xml diff --git a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/values/strings.xml b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/values/strings.xml index d29a772..376ee49 100644 --- a/UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/values/strings.xml +++ b/UI/AndroidApp/flavoursWithoutBorders/app/src/main/res/values/strings.xml @@ -30,6 +30,14 @@ Restaurants Filter results Show in map + Filter by type + Filter by distance + Filter by calories + Clear filters + Filter + Cancel + Max distance + Restaurant Bar Cafeteria