Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
CS123-CanteeneoAndroid
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Willard Torres
CS123-CanteeneoAndroid
Commits
f7b8d3da
Commit
f7b8d3da
authored
Dec 06, 2016
by
Willard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Overhaul searching and filtering (done client side instead of server side)
parent
15839804
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
135 additions
and
71 deletions
+135
-71
CanteeneoApiInterface.java
app/src/main/java/com/testapp/CanteeneoApiInterface.java
+2
-7
DishAdapter.java
app/src/main/java/com/testapp/DishAdapter.java
+33
-5
NavDrawerActivity.java
app/src/main/java/com/testapp/NavDrawerActivity.java
+49
-50
Dish.java
app/src/main/java/com/testapp/entities/Dish.java
+33
-0
ic_refresh.xml
app/src/main/res/drawable/ic_refresh.xml
+9
-0
activity_nav_drawer.xml
app/src/main/res/layout/activity_nav_drawer.xml
+2
-2
nav_drawer.xml
app/src/main/res/menu/nav_drawer.xml
+4
-4
strings.xml
app/src/main/res/values/strings.xml
+3
-3
No files found.
app/src/main/java/com/testapp/CanteeneoApiInterface.java
View file @
f7b8d3da
...
...
@@ -28,13 +28,8 @@ import retrofit2.http.Query;
public
interface
CanteeneoApiInterface
{
@GET
(
"api/search/dish"
)
Call
<
List
<
Dish
>>
searchByName
(
@Query
(
"name"
)
String
name
,
@Query
(
"type_filters"
)
String
types
,
@Query
(
"cuisine_filters"
)
String
cuisines
,
@Query
(
"location_filters"
)
String
location
,
@Query
(
"min_price"
)
int
min_price
,
@Query
(
"max_price"
)
int
max_price
);
@GET
(
"api/dishes"
)
Call
<
List
<
Dish
>>
getAllDishes
();
@FormUrlEncoded
@POST
(
"api/users/new"
)
...
...
app/src/main/java/com/testapp/DishAdapter.java
View file @
f7b8d3da
...
...
@@ -6,27 +6,31 @@ import android.view.ViewGroup;
import
android.widget.BaseAdapter
;
import
android.widget.ImageView
;
import
android.widget.TextView
;
import
com.squareup.picasso.Picasso
;
import
com.testapp.entities.Dish
;
import
java.util.ArrayList
;
public
class
DishAdapter
extends
BaseAdapter
{
public
class
DishAdapter
extends
BaseAdapter
{
private
Activity
context
;
ArrayList
<
Dish
>
dishes
;
ArrayList
<
Dish
>
filteredDishes
;
public
DishAdapter
(
Activity
context
,
ArrayList
<
Dish
>
dishes
)
{
this
.
context
=
context
;
this
.
dishes
=
dishes
;
filteredDishes
=
new
ArrayList
<>();
}
@Override
public
int
getCount
()
{
return
d
ishes
.
size
();
return
filteredD
ishes
.
size
();
}
@Override
public
Dish
getItem
(
int
position
)
{
return
d
ishes
.
get
(
position
);
return
filteredD
ishes
.
get
(
position
);
}
@Override
...
...
@@ -48,7 +52,7 @@ public class DishAdapter extends BaseAdapter {
holder
=
(
DishViewHolder
)
convertView
.
getTag
();
}
Dish
d
=
d
ishes
.
get
(
position
);
Dish
d
=
filteredD
ishes
.
get
(
position
);
holder
.
name
.
setText
(
d
.
getName
());
holder
.
price
.
setText
(
d
.
getPrice
()+
""
);
Picasso
.
with
(
context
).
load
(
"http://"
+
parent
.
getResources
().
getString
(
R
.
string
.
server_ip
)
+
":5000/static/uploads/"
+
d
.
getImagePath
()).
fit
().
centerCrop
().
tag
(
context
).
into
(
holder
.
image
);
...
...
@@ -56,9 +60,33 @@ public class DishAdapter extends BaseAdapter {
return
convertView
;
}
public
void
filter
(
String
nameFilter
,
ArrayList
<
Integer
>
typeFilter
,
ArrayList
<
Integer
>
cuisineFilter
,
ArrayList
<
Integer
>
locationFilter
,
double
minPrice
,
double
maxPrice
)
{
filteredDishes
.
clear
();
for
(
Dish
dish:
dishes
)
{
boolean
skip
=
false
;
String
[]
keywords
=
nameFilter
.
toLowerCase
().
split
(
" "
);
for
(
String
keyword:
keywords
)
{
if
(
dish
.
getName
().
toLowerCase
().
indexOf
(
keyword
)
==
-
1
)
{
skip
=
true
;
break
;
}
}
if
(
skip
)
continue
;
if
(
typeFilter
.
indexOf
(
dish
.
getTypeId
())
==
-
1
)
continue
;
if
(
cuisineFilter
.
indexOf
(
dish
.
getCuisineId
())
==
-
1
)
continue
;
if
(
locationFilter
.
indexOf
(
dish
.
getLocationId
())
==
-
1
)
continue
;
if
(
dish
.
getPrice
()
<
minPrice
||
dish
.
getPrice
()
>
maxPrice
)
continue
;
filteredDishes
.
add
(
dish
);
}
}
static
class
DishViewHolder
{
TextView
name
;
TextView
price
;
ImageView
image
;
}
}
}
\ No newline at end of file
app/src/main/java/com/testapp/NavDrawerActivity.java
View file @
f7b8d3da
...
...
@@ -15,6 +15,7 @@ import android.support.v7.app.ActionBarDrawerToggle;
import
android.support.v7.app.AppCompatActivity
;
import
android.support.v7.widget.SearchView
;
import
android.support.v7.widget.Toolbar
;
import
android.view.Gravity
;
import
android.view.LayoutInflater
;
import
android.view.Menu
;
import
android.view.MenuItem
;
...
...
@@ -46,6 +47,8 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
public
static
String
EXTRA_MESSAGE
=
"com.testapp.MESSAGE"
;
String
query
=
""
;
@Override
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
...
...
@@ -72,8 +75,6 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
NavigationView
navigationView
=
(
NavigationView
)
findViewById
(
R
.
id
.
nav_view
);
navigationView
.
setNavigationItemSelectedListener
(
this
);
Intent
intent
=
getIntent
();
populateFilter
(
AppUtils
.
service
.
getDishTypes
(),
R
.
id
.
type_checkboxes
);
populateFilter
(
AppUtils
.
service
.
getCuisines
(),
R
.
id
.
cuisine_checkboxes
);
populateFilter
(
AppUtils
.
service
.
getLocations
(),
R
.
id
.
location_checkboxes
);
...
...
@@ -96,6 +97,15 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
}
});
Button
applyFilterButton
=
(
Button
)
findViewById
(
R
.
id
.
apply_filter_button
);
applyFilterButton
.
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
v
)
{
refreshDishes
();
((
DrawerLayout
)
NavDrawerActivity
.
this
.
findViewById
(
R
.
id
.
drawer_layout
)).
closeDrawer
(
Gravity
.
LEFT
);
}
});
RelativeLayout
loggedInHeader
=
(
RelativeLayout
)
findViewById
(
R
.
id
.
logged_in_header
);
if
(
AppUtils
.
isLoggedIn
())
{
loggedInHeader
.
setVisibility
(
View
.
VISIBLE
);
...
...
@@ -122,6 +132,23 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
}
else
{
loggedInHeader
.
setVisibility
(
View
.
GONE
);
}
Call
<
List
<
Dish
>>
call
=
AppUtils
.
service
.
getAllDishes
();
call
.
enqueue
(
new
Callback
<
List
<
Dish
>>()
{
@Override
public
void
onResponse
(
Call
<
List
<
Dish
>>
call
,
Response
<
List
<
Dish
>>
response
)
{
List
<
Dish
>
newDishes
=
response
.
body
();
dishes
.
clear
();
dishes
.
addAll
(
newDishes
);
refreshDishes
();
}
@Override
public
void
onFailure
(
Call
<
List
<
Dish
>>
call
,
Throwable
t
)
{
}
});
}
public
<
T
extends
Filter
>
void
populateFilter
(
Call
<
List
<
T
>>
call
,
int
layout_id
)
{
...
...
@@ -176,91 +203,63 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
@Override
public
boolean
onCreateOptionsMenu
(
Menu
menu
)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater
().
inflate
(
R
.
menu
.
nav_drawer
,
menu
);
MenuItem
searchItem
=
menu
.
findItem
(
R
.
id
.
action_search
);
SearchView
sv
=
(
SearchView
)
searchItem
.
getActionView
();
final
SearchView
sv
=
(
SearchView
)
searchItem
.
getActionView
();
sv
.
setOnQueryTextListener
(
new
SearchView
.
OnQueryTextListener
()
{
@Override
public
boolean
onQueryTextSubmit
(
String
query
)
{
searchDish
(
query
);
return
false
;
}
@Override
public
boolean
onQueryTextChange
(
String
newText
)
{
query
=
newText
;
refreshDishes
();
return
false
;
}
});
return
true
;
}
@Override
public
boolean
onOptionsItemSelected
(
MenuItem
item
)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int
id
=
item
.
getItemId
();
//noinspection SimplifiableIfStatement
if
(
id
==
R
.
id
.
action_settings
)
{
return
true
;
}
MenuItem
refreshItem
=
menu
.
findItem
(
R
.
id
.
action_refresh
);
refreshItem
.
setOnMenuItemClickListener
(
new
MenuItem
.
OnMenuItemClickListener
()
{
@Override
public
boolean
onMenuItemClick
(
MenuItem
item
)
{
refreshDishes
();
return
false
;
}
});
return
super
.
onOptionsItemSelected
(
item
)
;
return
true
;
}
@SuppressWarnings
(
"StatementWithEmptyBody"
)
@Override
public
boolean
onNavigationItemSelected
(
MenuItem
item
)
{
// Handle navigation view item clicks here.
int
id
=
item
.
getItemId
();
if
(
id
==
R
.
id
.
nav_all_items
)
{
// Handle the camera action
}
else
if
(
id
==
R
.
id
.
nav_filter
)
{
}
DrawerLayout
drawer
=
(
DrawerLayout
)
findViewById
(
R
.
id
.
drawer_layout
);
//drawer.closeDrawer(GravityCompat.START);
return
true
;
}
private
String
getChecked
(
int
layoutId
)
{
private
ArrayList
<
Integer
>
getChecked
(
int
layoutId
)
{
LinearLayout
checkboxes
=
(
LinearLayout
)
findViewById
(
layoutId
);
String
checked
=
""
;
ArrayList
<
Integer
>
checked
=
new
ArrayList
<>()
;
for
(
int
i
=
0
;
i
<
checkboxes
.
getChildCount
();
i
++)
{
View
v
=
checkboxes
.
getChildAt
(
i
);
if
(
v
instanceof
CheckBox
)
{
CheckBox
c
=
(
CheckBox
)
v
;
if
(
c
.
isChecked
())
{
checked
+=
c
.
getId
(
);
checked
.
add
(
c
.
getId
()
);
}
}
}
return
checked
;
}
private
void
searchDish
(
String
name
)
{
Call
<
List
<
Dish
>>
call
=
AppUtils
.
service
.
searchByName
(
name
,
getChecked
(
R
.
id
.
type_checkboxes
),
getChecked
(
R
.
id
.
cuisine_checkboxes
),
getChecked
(
R
.
id
.
location_checkboxes
),
0
,
1000
);
call
.
enqueue
(
new
Callback
<
List
<
Dish
>>()
{
@Override
public
void
onResponse
(
Call
<
List
<
Dish
>>
call
,
Response
<
List
<
Dish
>>
response
)
{
List
<
Dish
>
newDishes
=
response
.
body
();
dishes
.
clear
();
dishes
.
addAll
(
newDishes
);
adapter
.
notifyDataSetChanged
();
}
@Override
public
void
onFailure
(
Call
<
List
<
Dish
>>
call
,
Throwable
t
)
{
}
});
public
void
refreshDishes
()
{
adapter
.
filter
(
query
,
getChecked
(
R
.
id
.
type_checkboxes
),
getChecked
(
R
.
id
.
cuisine_checkboxes
),
getChecked
(
R
.
id
.
location_checkboxes
),
0
,
1000
);
adapter
.
notifyDataSetChanged
();
System
.
out
.
println
(
adapter
.
filteredDishes
.
size
());
}
}
app/src/main/java/com/testapp/entities/Dish.java
View file @
f7b8d3da
...
...
@@ -9,6 +9,15 @@ public class Dish {
double
price
;
String
description
;
@SerializedName
(
"type_id"
)
int
typeId
;
@SerializedName
(
"cuisine_id"
)
int
cuisineId
;
@SerializedName
(
"location_id"
)
int
locationId
;
@SerializedName
(
"image_path"
)
String
imagePath
;
...
...
@@ -70,4 +79,28 @@ public class Dish {
public
void
setDescription
(
String
description
)
{
this
.
description
=
description
;
}
public
int
getTypeId
()
{
return
typeId
;
}
public
void
setTypeId
(
int
typeId
)
{
this
.
typeId
=
typeId
;
}
public
int
getCuisineId
()
{
return
cuisineId
;
}
public
void
setCuisineId
(
int
cuisineId
)
{
this
.
cuisineId
=
cuisineId
;
}
public
int
getLocationId
()
{
return
locationId
;
}
public
void
setLocationId
(
int
locationId
)
{
this
.
locationId
=
locationId
;
}
}
app/src/main/res/drawable/ic_refresh.xml
0 → 100644
View file @
f7b8d3da
<vector
xmlns:android=
"http://schemas.android.com/apk/res/android"
android:width=
"24dp"
android:height=
"24dp"
android:viewportWidth=
"24.0"
android:viewportHeight=
"24.0"
>
<path
android:fillColor=
"#FFFFFFFF"
android:pathData=
"M17.65,6.35C16.2,4.9 14.21,4 12,4c-4.42,0 -7.99,3.58 -7.99,8s3.57,8 7.99,8c3.73,0 6.84,-2.55 7.73,-6h-2.08c-0.82,2.33 -3.04,4 -5.65,4 -3.31,0 -6,-2.69 -6,-6s2.69,-6 6,-6c1.66,0 3.14,0.69 4.22,1.78L13,11h7V4l-2.35,2.35z"
/>
</vector>
app/src/main/res/layout/activity_nav_drawer.xml
View file @
f7b8d3da
...
...
@@ -174,10 +174,10 @@
</LinearLayout>
<Button
android:text=
"@string/
hint_search
"
android:text=
"@string/
apply_filter
"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:id=
"@+id/
search
_button"
android:id=
"@+id/
apply_filter
_button"
android:elevation=
"10dp"
style=
"@style/Widget.AppCompat.Button"
/>
...
...
app/src/main/res/menu/nav_drawer.xml
View file @
f7b8d3da
...
...
@@ -5,14 +5,14 @@
<item
android:id=
"@+id/action_search"
android:orderInCategory=
"100"
android:title=
"@string/
hint_search
"
android:title=
"@string/
apply_filter
"
android:icon=
"@android:drawable/ic_menu_search"
app:actionViewClass=
"android.support.v7.widget.SearchView"
app:showAsAction=
"always"
/>
<item
android:id=
"@+id/action_
settings
"
android:id=
"@+id/action_
refresh
"
android:orderInCategory=
"101"
android:icon=
"@drawable/ic_
menu_manage
"
android:title=
"@string/action_
settings
"
android:icon=
"@drawable/ic_
refresh
"
android:title=
"@string/action_
refresh
"
app:showAsAction=
"always"
/>
</menu>
app/src/main/res/values/strings.xml
View file @
f7b8d3da
...
...
@@ -24,15 +24,15 @@
<string
name=
"drawer_4"
>
Price Range
</string>
<string
name=
"drawer_5"
>
Locations
</string>
<string
name=
"drawer_6"
>
Cuisine
</string>
<string
name=
"
hint_search"
>
Search
</string>
<string
name=
"
apply_filter"
>
Apply Filters
</string>
<string
name=
"navigation_drawer_open"
>
Open navigation drawer
</string>
<string
name=
"navigation_drawer_close"
>
Close navigation drawer
</string>
<string
name=
"action_
settings"
>
Settings
</string>
<string
name=
"action_
refresh"
>
Refresh
</string>
<string
name=
"title_activity_search"
>
SearchActivity
</string>
<string
name=
"server_ip"
>
1
92.168.11.18
</string>
<string
name=
"server_ip"
>
1
0.0.2.2
</string>
<string-array
name=
"sampleEntries"
>
<item>
Item 111
</item>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment