Skip to content

Commit 40168f0

Browse files
Rohan Maityiamareebjamal
authored andcommitted
feat: Add Free Stuff Filter (#1642)
1 parent 49e73f3 commit 40168f0

File tree

11 files changed

+239
-11
lines changed

11 files changed

+239
-11
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package org.fossasia.openevent.general.search
2+
3+
import android.graphics.drawable.Drawable
4+
import android.os.Bundle
5+
import android.view.LayoutInflater
6+
import android.view.Menu
7+
import android.view.MenuInflater
8+
import android.view.MenuItem
9+
import android.view.View
10+
import android.view.ViewGroup
11+
import androidx.appcompat.app.AppCompatActivity
12+
import androidx.fragment.app.Fragment
13+
import androidx.navigation.Navigation
14+
import androidx.navigation.fragment.navArgs
15+
import kotlinx.android.synthetic.main.fragment_search_filter.view.freeStuffCheckBox
16+
import org.fossasia.openevent.general.R
17+
import org.fossasia.openevent.general.utils.Utils.getAnimFade
18+
import org.fossasia.openevent.general.utils.Utils.setToolbar
19+
20+
class SearchFilterFragment : Fragment() {
21+
22+
private lateinit var rootView: View
23+
private var isFreeStuffChecked = false
24+
private val safeArgs: SearchFilterFragmentArgs by navArgs()
25+
26+
override fun onCreateView(
27+
inflater: LayoutInflater,
28+
container: ViewGroup?,
29+
savedInstanceState: Bundle?
30+
): View? {
31+
32+
setToolbar(activity)
33+
setHasOptionsMenu(true)
34+
rootView = inflater.inflate(R.layout.fragment_search_filter, container, false)
35+
setFilterToolbar()
36+
setFreeStuffCheckBox()
37+
return rootView
38+
}
39+
40+
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
41+
inflater.inflate(R.menu.search_filter, menu)
42+
super.onCreateOptionsMenu(menu, inflater)
43+
}
44+
45+
private fun setFilterToolbar() {
46+
val close = resources.getDrawable(R.drawable.ic_close)
47+
setBackIndicator(close)
48+
}
49+
50+
private fun setBackIndicator(indicator: Drawable? = null) {
51+
(activity as? AppCompatActivity)?.supportActionBar?.setHomeAsUpIndicator(indicator)
52+
}
53+
54+
override fun onOptionsItemSelected(item: MenuItem): Boolean {
55+
return when (item.itemId) {
56+
android.R.id.home -> {
57+
setBackIndicator()
58+
activity?.onBackPressed()
59+
true
60+
}
61+
R.id.filter_set -> {
62+
setBackIndicator()
63+
SearchFilterFragmentArgs.Builder()
64+
.setDate(safeArgs.date)
65+
.setFreeEvents(isFreeStuffChecked)
66+
.setLocation(safeArgs.location)
67+
.setType(safeArgs.type)
68+
.setQuery(safeArgs.query)
69+
.build()
70+
.toBundle()
71+
.also {
72+
Navigation.findNavController(rootView)
73+
.navigate(R.id.searchResultsFragment, it, getAnimFade())
74+
}
75+
true
76+
}
77+
else -> super.onOptionsItemSelected(item)
78+
}
79+
}
80+
81+
private fun setFreeStuffCheckBox() {
82+
rootView.freeStuffCheckBox.isChecked = safeArgs.freeEvents
83+
rootView.freeStuffCheckBox.setOnCheckedChangeListener { _, isChecked ->
84+
isFreeStuffChecked = isChecked
85+
}
86+
}
87+
}

app/src/main/java/org/fossasia/openevent/general/search/SearchResultsFragment.kt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package org.fossasia.openevent.general.search
33
import android.content.res.ColorStateList
44
import android.os.Bundle
55
import android.view.LayoutInflater
6+
import android.view.Menu
7+
import android.view.MenuInflater
68
import android.view.MenuItem
79
import android.view.View
810
import android.view.ViewGroup
@@ -42,6 +44,7 @@ import org.koin.androidx.scope.ext.android.getOrCreateScope
4244
import org.koin.androidx.viewmodel.ext.android.viewModel
4345
import timber.log.Timber
4446
import androidx.appcompat.view.ContextThemeWrapper
47+
import androidx.navigation.fragment.findNavController
4548

4649
class SearchResultsFragment : Fragment(), CompoundButton.OnCheckedChangeListener {
4750

@@ -220,8 +223,9 @@ class SearchResultsFragment : Fragment(), CompoundButton.OnCheckedChangeListener
220223
val location = safeArgs.location
221224
val type = eventType
222225
val date = eventDate
226+
val freeEvents = safeArgs.freeEvents
223227
searchViewModel.searchEvent = query
224-
searchViewModel.loadEvents(location, date, type)
228+
searchViewModel.loadEvents(location, date, type, freeEvents)
225229
}
226230

227231
private fun showNoSearchResults(events: List<Event>) {
@@ -239,9 +243,29 @@ class SearchResultsFragment : Fragment(), CompoundButton.OnCheckedChangeListener
239243
activity?.onBackPressed()
240244
true
241245
}
246+
R.id.filter -> {
247+
SearchFilterFragmentArgs.Builder()
248+
.setDate(safeArgs.date)
249+
.setFreeEvents(safeArgs.freeEvents)
250+
.setLocation(safeArgs.location)
251+
.setType(safeArgs.type)
252+
.setQuery(safeArgs.query)
253+
.build()
254+
.toBundle()
255+
.also {
256+
Navigation.findNavController(rootView).navigate(R.id.searchFilterFragment, it, getAnimFade())
257+
}
258+
true
259+
}
242260
else -> super.onOptionsItemSelected(item)
243261
}
244262
}
263+
264+
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
265+
inflater.inflate(R.menu.search_results, menu)
266+
super.onCreateOptionsMenu(menu, inflater)
267+
}
268+
245269
override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
246270
if (isChecked) {
247271
if (buttonView?.text == "Clear All") {

app/src/main/java/org/fossasia/openevent/general/search/SearchViewModel.kt

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,26 @@ class SearchViewModel(
7676
savedTime = preference.getString(SAVED_TIME)
7777
}
7878

79-
fun loadEvents(location: String, time: String, type: String) {
79+
fun loadEvents(location: String, time: String, type: String, freeEvents: Boolean) {
8080
if (mutableEvents.value != null) {
8181
mutableChipClickable.value = true
8282
return
8383
}
8484
if (!isConnected()) return
8585
preference.putString(SAVED_LOCATION, location)
86+
87+
val freeStuffFilter = if (freeEvents)
88+
""", {
89+
| 'name':'tickets',
90+
| 'op':'any',
91+
| 'val':{
92+
| 'name':'price',
93+
| 'op':'eq',
94+
| 'val':'0'
95+
| }
96+
|}
97+
""".trimIndent()
98+
else ""
8699
val query: String = when {
87100
TextUtils.isEmpty(location) -> """[{
88101
| 'name':'name',
@@ -98,7 +111,7 @@ class SearchViewModel(
98111
| 'name':'name',
99112
| 'op':'ilike',
100113
| 'val':'%$searchEvent%'
101-
| }]
114+
| }$freeStuffFilter]
102115
|}]""".trimMargin().replace("'", "\"")
103116
time == "Anytime" -> """[{
104117
| 'and':[{
@@ -117,7 +130,7 @@ class SearchViewModel(
117130
| 'op':'eq',
118131
| 'val':'$type'
119132
| }
120-
| }]
133+
| }$freeStuffFilter]
121134
|}]""".trimMargin().replace("'", "\"")
122135
time == "Today" -> """[{
123136
| 'and':[{
@@ -144,7 +157,7 @@ class SearchViewModel(
144157
| 'op':'eq',
145158
| 'val':'$type'
146159
| }
147-
| }]
160+
| }$freeStuffFilter]
148161
|}]""".trimMargin().replace("'", "\"")
149162
time == "Tomorrow" -> """[{
150163
| 'and':[{
@@ -171,7 +184,7 @@ class SearchViewModel(
171184
| 'op':'eq',
172185
| 'val':'$type'
173186
| }
174-
| }]
187+
| }$freeStuffFilter]
175188
|}]""".trimMargin().replace("'", "\"")
176189
time == "This weekend" -> """[{
177190
| 'and':[{
@@ -198,7 +211,7 @@ class SearchViewModel(
198211
| 'op':'eq',
199212
| 'val':'$type'
200213
| }
201-
| }]
214+
| }$freeStuffFilter]
202215
|}]""".trimMargin().replace("'", "\"")
203216
time == "In the next month" -> """[{
204217
| 'and':[{
@@ -225,8 +238,9 @@ class SearchViewModel(
225238
| 'op':'eq',
226239
| 'val':'$type'
227240
| }
228-
| }]
241+
| }$freeStuffFilter]
229242
|}]""".trimMargin().replace("'", "\"")
243+
230244
else -> """[{
231245
| 'and':[{
232246
| 'name':'location-name',
@@ -252,10 +266,9 @@ class SearchViewModel(
252266
| 'op':'eq',
253267
| 'val':'$type'
254268
| }
255-
| }]
269+
| }$freeStuffFilter]
256270
|}]""".trimMargin().replace("'", "\"")
257271
}
258-
259272
compositeDisposable.add(eventService.getSearchEvents(query)
260273
.subscribeOn(Schedulers.io())
261274
.observeOn(AndroidSchedulers.mainThread())
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:viewportHeight="24.0" android:viewportWidth="24.0" android:width="24dp">
3+
<path android:fillColor="#FFFFFF" android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z"/>
4+
</vector>

app/src/main/res/drawable/ic_done.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:viewportHeight="24.0" android:viewportWidth="24.0" android:width="24dp">
3+
<path android:fillColor="#FFFFFF" android:pathData="M9,16.2L4.8,12l-1.4,1.4L9,19 21,7l-1.4,-1.4L9,16.2z"/>
4+
</vector>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:viewportHeight="24.0" android:viewportWidth="24.0" android:width="24dp">
3+
<path android:fillColor="#FFFFFF" android:pathData="M3,17v2h6v-2L3,17zM3,5v2h10L13,5L3,5zM13,21v-2h8v-2h-8v-2h-2v6h2zM7,9v2L3,11v2h4v2h2L9,9L7,9zM21,13v-2L11,11v2h10zM15,9h2L17,7h4L21,5h-4L17,3h-2v6z"/>
4+
</vector>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:id="@+id/rootView"
7+
android:orientation="vertical"
8+
tools:context="org.fossasia.openevent.general.search.SearchFilterFragment">
9+
10+
<LinearLayout
11+
android:orientation="vertical"
12+
android:layout_width="match_parent"
13+
android:layout_height="wrap_content">
14+
15+
<TextView
16+
android:id="@+id/PriceLabel"
17+
android:padding="@dimen/padding_medium"
18+
android:textColor="@color/black"
19+
android:textSize="@dimen/text_size_expanded_title"
20+
android:text="@string/price"
21+
android:layout_width="match_parent"
22+
android:layout_height="wrap_content" />
23+
24+
<CheckBox
25+
android:padding="@dimen/padding_small"
26+
android:textSize="@dimen/text_size_large"
27+
android:text="@string/free_stuff"
28+
android:id="@+id/freeStuffCheckBox"
29+
android:layoutDirection="rtl"
30+
android:layout_width="match_parent"
31+
android:layout_height="wrap_content" />
32+
33+
</LinearLayout>
34+
</LinearLayout>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<menu xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto">
4+
<item
5+
android:id="@+id/filter_set"
6+
android:icon="@drawable/ic_done"
7+
android:title="@string/filter_set"
8+
app:showAsAction="always" />
9+
</menu>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<menu xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto">
4+
<item
5+
android:id="@+id/filter"
6+
android:icon="@drawable/ic_filter"
7+
android:title="@string/filter"
8+
app:showAsAction="always" />
9+
</menu>

app/src/main/res/navigation/navigation_graph.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@
103103
android:name="type"
104104
app:argType="string"
105105
android:defaultValue="''"/>
106+
107+
<argument
108+
android:name="freeEvents"
109+
app:argType="boolean"
110+
android:defaultValue="false"/>
106111
</fragment>
107112
<fragment
108113
android:id="@+id/favoriteFragment"
@@ -241,4 +246,36 @@
241246
android:name="org.fossasia.openevent.general.auth.SignUpFragment"
242247
android:label="SignUpFragment"
243248
tools:layout="@layout/fragment_signup" />
249+
<fragment
250+
android:id="@+id/searchFilterFragment"
251+
android:name="org.fossasia.openevent.general.search.SearchFilterFragment"
252+
android:label="fragment_search_filter"
253+
tools:layout="@layout/fragment_search_filter">
254+
255+
<argument
256+
android:name="query"
257+
app:argType="string"
258+
android:defaultValue="''"/>
259+
260+
<argument
261+
android:name="location"
262+
app:argType="string"
263+
android:defaultValue="''"/>
264+
265+
<argument
266+
android:name="date"
267+
app:argType="string"
268+
android:defaultValue="''"/>
269+
270+
<argument
271+
android:name="type"
272+
app:argType="string"
273+
android:defaultValue="''"/>
274+
275+
<argument
276+
android:name="freeEvents"
277+
app:argType="boolean"
278+
android:defaultValue="false"/>
279+
280+
</fragment>
244281
</navigation>

app/src/main/res/values/strings.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,14 @@
303303
<string name="social_link_check_internet_message">Information is not loaded! Please check your internet connection</string>
304304
<string name="popular_locations">Popular Locations</string>
305305

306-
<!-- TODO: Remove or change this placeholder text -->
307306
<string name="anything">Anything</string>
308307
<string name="and_i_m_up_for">And I\'m up for</string>
309308
<string name="savedType">savedType</string>
310309
<string name="what_type">What Sounds good?</string>
311310
<string name="feedback">Feedback</string>
312311

312+
<string name="filter_set">Filter Done</string>
313+
<string name="filter">Filter Search</string>
314+
<string name="free_stuff">Free stuff only</string>
315+
313316
</resources>

0 commit comments

Comments
 (0)