Skip to content

Commit e21e4fd

Browse files
committed
add filtering the results
1 parent 07ca1f3 commit e21e4fd

File tree

5 files changed

+84
-16
lines changed

5 files changed

+84
-16
lines changed

10-mars-photos/app/src/main/java/com/example/android/marsphotos/network/MarsApiService.kt

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
55
import retrofit2.Retrofit
66
import retrofit2.converter.moshi.MoshiConverterFactory
77
import retrofit2.http.GET
8+
import retrofit2.http.Query
9+
10+
enum class MarsApiFilter(val value: String) {
11+
SHOW_RENT("rent"),
12+
SHOW_BUY("buy"),
13+
SHOW_ALL("all")
14+
}
815

916
private const val BASE_URL = "https://android-kotlin-fun-mars-server.appspot.com"
1017

@@ -20,7 +27,7 @@ private val retrofit = Retrofit.Builder()
2027

2128
interface MarsApiService {
2229
@GET("realestate")
23-
suspend fun getPhotos(): List<MarsPhoto>
30+
suspend fun getPhotos(@Query("filter") type: String): List<MarsPhoto>
2431
}
2532

2633
object MarsApi {

10-mars-photos/app/src/main/java/com/example/android/marsphotos/overview/OverviewFragment.kt

+29-12
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,28 @@
1717
package com.example.android.marsphotos.overview
1818

1919
import android.os.Bundle
20-
import android.view.LayoutInflater
21-
import android.view.View
22-
import android.view.ViewGroup
20+
import android.view.*
2321
import androidx.fragment.app.Fragment
24-
import androidx.fragment.app.viewModels
22+
import androidx.lifecycle.ViewModelProvider
23+
import com.example.android.marsphotos.R
2524
import com.example.android.marsphotos.databinding.FragmentOverviewBinding
26-
import com.example.android.marsphotos.databinding.GridViewItemBinding
25+
import com.example.android.marsphotos.network.MarsApiFilter
2726

2827
/**
2928
* This fragment shows the the status of the Mars photos web services transaction.
3029
*/
3130
class OverviewFragment : Fragment() {
3231

33-
private val viewModel: OverviewViewModel by viewModels()
32+
private val viewModel: OverviewViewModel by lazy {
33+
ViewModelProvider(this).get(OverviewViewModel::class.java)
34+
}
3435

3536
/**
3637
* Inflates the layout with Data Binding, sets its lifecycle owner to the OverviewFragment
3738
* to enable Data Binding to observe LiveData, and sets up the RecyclerView with an adapter.
3839
*/
39-
override fun onCreateView(
40-
inflater: LayoutInflater, container: ViewGroup?,
41-
savedInstanceState: Bundle?
42-
): View? {
40+
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
41+
savedInstanceState: Bundle?): View? {
4342
val binding = FragmentOverviewBinding.inflate(inflater)
4443
//val binding = GridViewItemBinding.inflate(inflater)
4544

@@ -49,11 +48,29 @@ class OverviewFragment : Fragment() {
4948
// Giving the binding access to the OverviewViewModel
5049
binding.viewModel = viewModel
5150

52-
// Display a grid of images
51+
// Sets the adapter of the photosGrid RecyclerView
5352
binding.photosGrid.adapter = PhotoGridAdapter()
5453

5554
setHasOptionsMenu(true)
56-
5755
return binding.root
5856
}
57+
58+
/**
59+
* Inflates the overflow menu that contains filtering options.
60+
*/
61+
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
62+
inflater.inflate(R.menu.overflow_menu, menu)
63+
super.onCreateOptionsMenu(menu, inflater)
64+
}
65+
66+
override fun onOptionsItemSelected(item: MenuItem): Boolean {
67+
viewModel.updateFilter(
68+
when (item.itemId) {
69+
R.id.show_rent_menu -> MarsApiFilter.SHOW_RENT
70+
R.id.show_buy_menu -> MarsApiFilter.SHOW_BUY
71+
else -> MarsApiFilter.SHOW_ALL
72+
}
73+
)
74+
return true
75+
}
5976
}

10-mars-photos/app/src/main/java/com/example/android/marsphotos/overview/OverviewViewModel.kt

+9-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import androidx.lifecycle.MutableLiveData
2121
import androidx.lifecycle.ViewModel
2222
import androidx.lifecycle.viewModelScope
2323
import com.example.android.marsphotos.network.MarsApi
24+
import com.example.android.marsphotos.network.MarsApiFilter
2425
import com.example.android.marsphotos.network.MarsPhoto
2526
import kotlinx.coroutines.launch
2627

@@ -52,14 +53,14 @@ class OverviewViewModel : ViewModel() {
5253
* Call getMarsPhotos() on init so we can display status immediately.
5354
*/
5455
init {
55-
getMarsPhotos()
56+
getMarsPhotos(MarsApiFilter.SHOW_ALL)
5657
}
5758

5859
/**
5960
* Gets Mars photos information from the Mars API Retrofit service and updates the
6061
* [MarsPhoto] [List] [LiveData].
6162
*/
62-
private fun getMarsPhotos() {
63+
private fun getMarsPhotos(filter: MarsApiFilter) {
6364
viewModelScope.launch {
6465
_status.value = MarsApiStatus.LOADING
6566
try {
@@ -68,12 +69,17 @@ class OverviewViewModel : ViewModel() {
6869
/*if (listResult.isNotEmpty()) {
6970
//_property.value = listResult[0]
7071
}*/
71-
_properties.value = MarsApi.retrofitService.getPhotos()
72+
_properties.value = MarsApi.retrofitService.getPhotos(filter.value)
7273
_status.value = MarsApiStatus.DONE
7374
} catch (e: Exception) {
7475
_status.value = MarsApiStatus.ERROR
7576
_properties.value = ArrayList()
7677
}
7778
}
7879
}
80+
81+
// filter
82+
fun updateFilter(filter: MarsApiFilter) {
83+
getMarsPhotos(filter)
84+
}
7985
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<!--
4+
~ Copyright 2019, The Android Open Source Project
5+
~
6+
~ Licensed under the Apache License, Version 2.0 (the "License");
7+
~ you may not use this file except in compliance with the License.
8+
~ You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
~
18+
-->
19+
20+
<menu xmlns:android="http://schemas.android.com/apk/res/android">
21+
<item
22+
android:id="@+id/show_all_menu"
23+
android:title="@string/show_all" />
24+
<item
25+
android:id="@+id/show_rent_menu"
26+
android:title="@string/show_rent" />
27+
<item
28+
android:id="@+id/show_buy_menu"
29+
android:title="@string/show_buy" />
30+
</menu>

10-mars-photos/app/src/main/res/values/strings.xml

+8
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,12 @@
1717

1818
<resources>
1919
<string name="app_name">Mars Photos</string>
20+
<string name="show_all">Show all</string>
21+
<string name="show_rent">Rent</string>
22+
<string name="show_buy">Buy</string>
23+
<string name="type_rent">Rent</string>
24+
<string name="type_sale">Sale</string>
25+
<string name="display_type">For %s</string>
26+
<string name="display_price_monthly_rental">$%,.0f/month</string>
27+
<string name="display_price">$%,.0f</string>
2028
</resources>

0 commit comments

Comments
 (0)