Skip to content

Commit

Permalink
Added permissions for switch-location
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevansurendran committed Mar 14, 2020
1 parent 60793c3 commit 8666a5c
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import java.io.File
class ShopRepository private constructor(context: Context) : BaseRepository() {
private val mWebService: WebApiService = ApiClient.getApiService(context)

fun nearbyRestaurants(id: Int): LiveData<Resource<List<NearbyRestaurantModel>>> =
fun getNearbyRestaurants(id: Int): LiveData<Resource<List<NearbyRestaurantModel>>> =
object : NetworkBoundResource<List<NearbyRestaurantModel>, List<NearbyRestaurantModel>>() {
override fun shouldUseLocalDb(): Boolean {
return false
}

override fun createCall(): LiveData<ApiResponse<List<NearbyRestaurantModel>>> {
return RetrofitLiveData(mWebService.nearbyRestaurants(if (id == 0) null else id))
return RetrofitLiveData(mWebService.getNearbyRestaurants(if (id == 0) null else id))
}
}.asLiveData

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ interface WebApiService {
fun getRestaurantProfile(@Path("restaurant_id") restaurantId: Long): Call<RestaurantModel>

@GET("restaurants/nearby/")
fun nearbyRestaurants(@Query("city_id") cityId: Int?): Call<List<NearbyRestaurantModel>>
fun getNearbyRestaurants(@Query("city_id") cityId: Int?): Call<List<NearbyRestaurantModel>>

@GET("restaurants/{restaurant_id}/brief/")
fun getRestaurantBriefDetail(@Path("restaurant_id") restaurantId: Long): Call<RestaurantServiceModel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ import com.checkin.app.checkin.user.fragments.UserPrivateProfileFragment
import com.checkin.app.checkin.user.models.UserModel
import com.checkin.app.checkin.user.viewmodels.UserViewModel
import com.checkin.app.checkin.utility.*
import com.checkin.app.checkin.utility.Constants.LOCATION_CITY_FILE
import com.checkin.app.checkin.utility.Constants.LOCATION_CITY_ID
import com.checkin.app.checkin.utility.Constants.LOCATION_CITY_NAME
import com.checkin.app.checkin.utility.OnBoardingUtils.OnBoardingModel
import com.golovin.fluentstackbar.FluentSnackbar
import com.google.android.material.snackbar.Snackbar
Expand Down Expand Up @@ -140,7 +143,7 @@ class HomeActivity : BaseAccountActivity() {
}
})

setupUserLocationTracker()

setupObserver()
explainQr()

Expand Down Expand Up @@ -247,20 +250,22 @@ class HomeActivity : BaseAccountActivity() {
})

mViewModel.cityId.observe(this, Observer {
val preferences = getSharedPreferences(com.checkin.app.checkin.utility.Constants.LOCATION_CITY_FILE, Context.MODE_PRIVATE)
tvCityLocation.text = preferences.getString(com.checkin.app.checkin.utility.Constants.LOCATION_CITY_NAME, "Select a City")
val preferences = getSharedPreferences(LOCATION_CITY_FILE, Context.MODE_PRIVATE)
tvCityLocation.text = preferences.getString(LOCATION_CITY_NAME, "Current Location")
if (it == 0) {
setupUserLocationTracker()
}
})
liveViewModel.clearCartData.observe(this, Observer { })

mViewModel.setCityId(getCityId())
mViewModel.fetchSessionStatus()
mViewModel.fetchNearbyRestaurants()
liveViewModel.fetchCartStatus()
}

private fun getCityId(): Int {
val preferences = getSharedPreferences(com.checkin.app.checkin.utility.Constants.LOCATION_CITY_FILE, Context.MODE_PRIVATE)
return preferences.getInt(com.checkin.app.checkin.utility.Constants.LOCATION_CITY_ID, 0)
val preferences = getSharedPreferences(LOCATION_CITY_FILE, Context.MODE_PRIVATE)
return preferences.getInt(LOCATION_CITY_ID, 0)
}

private fun resetUserIcon() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.checkin.app.checkin.home.epoxy

import android.view.View
import android.widget.TextView
import butterknife.BindView
import com.airbnb.epoxy.EpoxyAttribute
import com.airbnb.epoxy.EpoxyModelClass
import com.airbnb.epoxy.EpoxyModelWithHolder
import com.checkin.app.checkin.R
import com.checkin.app.checkin.home.model.CityLocationModel
import com.checkin.app.checkin.menu.listeners.LocationSelectedListener
import com.checkin.app.checkin.misc.epoxy.BaseEpoxyHolder

@EpoxyModelClass(layout = R.layout.item_user_location_city)
Expand All @@ -17,12 +17,14 @@ abstract class CityLocationModelHolder : EpoxyModelWithHolder<CityLocationModelH
internal lateinit var data: CityLocationModel

@EpoxyAttribute
internal lateinit var cityListener: View.OnClickListener
internal lateinit var locationSelected: LocationSelectedListener

override fun bind(holder: Holder) {
holder.bindData(data)
if (::cityListener.isInitialized)
holder.itemView.setOnClickListener(cityListener)
holder.itemView.setOnClickListener {
locationSelected.onSelectedLocation()
}

}

class Holder : BaseEpoxyHolder<CityLocationModel>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package com.checkin.app.checkin.home.epoxy

import android.view.View
import com.airbnb.epoxy.EpoxyAttribute
import com.airbnb.epoxy.EpoxyModelClass
import com.airbnb.epoxy.EpoxyModelWithHolder
import com.checkin.app.checkin.R
import com.checkin.app.checkin.menu.listeners.LocationSelectedListener
import com.checkin.app.checkin.misc.epoxy.BaseEpoxyHolder

@EpoxyModelClass(layout = R.layout.item_user_location_current)
abstract class CurrentLocationModelHolder : EpoxyModelWithHolder<CurrentLocationModelHolder.Holder>() {

@EpoxyAttribute
internal lateinit var currentListener: View.OnClickListener
internal lateinit var locationSelected: LocationSelectedListener

override fun bind(holder: Holder) {
if (::currentListener.isInitialized)
holder.itemView.setOnClickListener(currentListener)
if (::locationSelected.isInitialized)
holder.itemView.setOnClickListener {
locationSelected.onSelectedLocation()
}
}

class Holder : BaseEpoxyHolder<String>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import com.checkin.app.checkin.home.epoxy.currentLocationModelHolder
import com.checkin.app.checkin.home.viewmodels.HomeViewModel
import com.checkin.app.checkin.home.viewmodels.UserLocationViewModel
import com.checkin.app.checkin.misc.fragments.BaseFragment
import com.checkin.app.checkin.misc.holders.textModelHolder
import com.checkin.app.checkin.utility.Constants

class UserLocationFragment : BaseFragment() {
Expand All @@ -36,6 +35,7 @@ class UserLocationFragment : BaseFragment() {
@BindView(R.id.im_user_location_cross)
internal lateinit var imUserCross: ImageView


@OnTextChanged(R.id.et_user_location, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
fun onTextChanged(et: Editable?) {
if (et.toString().isEmpty()) {
Expand All @@ -58,12 +58,20 @@ class UserLocationFragment : BaseFragment() {
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

epoxyUserLocation.withModels {
if (viewModel.locationData.value?.inError == true) {
textModelHolder {
id("not.available")
text("Sorry not available in your city.")
if (etUserLocation.text.isEmpty() || viewModel.locationData.value?.inError == true) {
currentLocationModelHolder {
id("present.location")
locationSelected {
val preferences = requireActivity().getSharedPreferences(Constants.LOCATION_CITY_FILE, Context.MODE_PRIVATE)
with(preferences.edit()) {
putInt(Constants.LOCATION_CITY_ID, 0)
putString(Constants.LOCATION_CITY_NAME, "Current Location")
commit()
}
homeViewModel.setCityId(0)
requireActivity().supportFragmentManager.popBackStack()
}
}
}

Expand All @@ -72,7 +80,7 @@ class UserLocationFragment : BaseFragment() {
cityLocationModelHolder {
id(model.id)
data(model)
cityListener { _ ->
locationSelected {
val preferences = requireActivity().getSharedPreferences(Constants.LOCATION_CITY_FILE, Context.MODE_PRIVATE)
with(preferences.edit()) {
putInt(Constants.LOCATION_CITY_ID, model.id)
Expand All @@ -84,20 +92,6 @@ class UserLocationFragment : BaseFragment() {
}
}
}

currentLocationModelHolder {
id("present.location")
currentListener { _ ->
val preferences = requireActivity().getSharedPreferences(Constants.LOCATION_CITY_FILE, Context.MODE_PRIVATE)
with(preferences.edit()) {
putInt(Constants.LOCATION_CITY_ID, 0)
putString(Constants.LOCATION_CITY_NAME, "Current Location")
commit()
}
homeViewModel.setCityId(0)
requireActivity().supportFragmentManager.popBackStack()
}
}
}

viewModel.locationData.observe(viewLifecycleOwner, Observer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class HomeViewModel(application: Application) : BaseViewModel(application) {
}

fun fetchNearbyRestaurants() {
mNearbyRestaurants.addSource(mShopRepository.nearbyRestaurants(cityId.value
mNearbyRestaurants.addSource(mShopRepository.getNearbyRestaurants(cityId.value
?: 0), mNearbyRestaurants::setValue)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,11 @@ class UserLocationViewModel(application: Application) : BaseMenuViewModel(applic
private fun internalSearchCities(query: String) {
val resourceLiveData = Transformations.map(allLocationsData) { input ->
val items = input.data?.filter {
it.name.contains(query, ignoreCase = true)
it.name.contains(query, ignoreCase = true) || it.state.contains(query, ignoreCase = true)
}
if (items?.isEmpty() == true) return@map Resource.errorNotFound<List<CityLocationModel>>("No Such City Found.")
Resource.cloneResource(input, items)
}
mLocationData.addSource(resourceLiveData) {
mLocationData.value = it
}
mLocationData.addSource(resourceLiveData, mLocationData::setValue)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.checkin.app.checkin.menu.listeners;

public interface LocationSelectedListener {
void onSelectedLocation();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ abstract class BaseMenuViewModel(application: Application) : BaseViewModel(appli
protected val mMenuData = createNetworkLiveData<MenuModel>()
private val mOriginalMenuGroups = createNetworkLiveData<List<MenuGroupModel>>()
private val mMenuGroups = MediatorLiveData<Resource<List<MenuGroupModel>>>()
private val mMenuItems =
createNetworkLiveData<List<MenuItemModel>>()
private val mMenuItems = createNetworkLiveData<List<MenuItemModel>>()

private val mCurrentItem = MutableLiveData<OrderedItemModel>()
private val mFilteredString = MutableLiveData<String>()
Expand Down

0 comments on commit 8666a5c

Please sign in to comment.