Skip to content

fix: App crash when no location found #2259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ import android.location.LocationListener
import android.location.LocationManager
import android.os.Bundle
import io.reactivex.Single
import org.fossasia.openevent.general.R
import org.fossasia.openevent.general.data.Resource
import org.fossasia.openevent.general.utils.nullToEmpty
import java.lang.IllegalArgumentException
import java.util.Locale

class LocationServiceImpl(private val context: Context) : LocationService {
class LocationServiceImpl(
private val context: Context,
private val resource: Resource
) : LocationService {

@SuppressLint("MissingPermission")
override fun getAdministrativeArea(): Single<String> {
Expand All @@ -30,7 +36,8 @@ class LocationServiceImpl(private val context: Context) : LocationService {
}
val addresses = geoCoder.getFromLocation(location.latitude, location.longitude, 1)
try {
val adminArea = addresses[0].adminArea
val adminArea = if (addresses.isNotEmpty()) addresses[0].adminArea
else resource.getString(R.string.no_location).nullToEmpty()
emitter.onSuccess(adminArea)
} catch (e: IllegalArgumentException) {
emitter.onError(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ val commonModule = module {
single { Network() }
single { Resource() }
factory { MutableConnectionLiveData() }
factory<LocationService> { LocationServiceImpl(androidContext()) }
factory<LocationService> { LocationServiceImpl(androidContext(), get()) }
}

val apiModule = module {
Expand Down Expand Up @@ -246,7 +246,7 @@ val viewModelModule = module {
viewModel { SearchViewModel(get(), get()) }
viewModel { SearchResultsViewModel(get(), get(), get(), get(), get(), get()) }
viewModel { AttendeeViewModel(get(), get(), get(), get(), get(), get(), get(), get()) }
viewModel { SearchLocationViewModel(get(), get()) }
viewModel { SearchLocationViewModel(get(), get(), get()) }
viewModel { SearchTimeViewModel(get()) }
viewModel { SearchTypeViewModel(get(), get(), get()) }
viewModel { TicketsViewModel(get(), get(), get(), get(), get(), get()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ class EventsViewModel(
}

fun loadLocationEvents() {
if (mutableSavedLocation.value == null) return
val location = mutableSavedLocation.value
if (location == null || location == resource.getString(R.string.enter_location) ||
location == resource.getString(R.string.no_location)) {
mutableProgress.value = false
return
}

sourceFactory = EventsDataSourceFactory(
compositeDisposable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import org.fossasia.openevent.general.BuildConfig
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers
import org.fossasia.openevent.general.R
import org.fossasia.openevent.general.data.Preference
import org.fossasia.openevent.general.data.Resource
import org.fossasia.openevent.general.event.EventService
import org.fossasia.openevent.general.event.location.EventLocation
import org.jetbrains.anko.doAsync
Expand All @@ -28,7 +30,8 @@ const val SEARCH_INTERVAL = 250L

class SearchLocationViewModel(
private val eventService: EventService,
private val preference: Preference
private val preference: Preference,
private val resource: Resource
) : ViewModel() {

private val compositeDisposable = CompositeDisposable()
Expand All @@ -44,6 +47,8 @@ class SearchLocationViewModel(
fun saveSearch(query: String) {
preference.putString(SAVED_LOCATION, query)

if (query == resource.getString(R.string.no_location)) return

if (savedLocationList.size == SAVED_LOCATION_LIST_SIZE)
savedLocationList.removeAt(SAVED_LOCATION_LIST_SIZE - 1)
val index = savedLocationList.indexOf(query)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -525,5 +525,6 @@
<string name="speaker_experience">Speaking Experience</string>
<string name="linkedin">LinkedIn</string>
<string name="order_now">Order Now</string>
<string name="no_location">No Location</string>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ import com.google.android.gms.location.LocationRequest
import com.google.android.gms.location.LocationResult
import com.google.android.gms.location.LocationServices
import io.reactivex.Single
import org.fossasia.openevent.general.R
import org.fossasia.openevent.general.data.Resource
import org.fossasia.openevent.general.location.LocationPermissionException
import org.fossasia.openevent.general.location.NoLocationSourceException
import org.fossasia.openevent.general.utils.nullToEmpty
import timber.log.Timber
import java.io.IOException
import java.lang.Exception
import java.util.Locale

class LocationServiceImpl(private val context: Context) : LocationService {
class LocationServiceImpl(
private val context: Context,
private val resource: Resource
) : LocationService {

override fun getAdministrativeArea(): Single<String> {
val locationManager = context.getSystemService(Context.LOCATION_SERVICE)
Expand Down Expand Up @@ -44,8 +51,9 @@ class LocationServiceImpl(private val context: Context) : LocationService {
try {
val adminArea = locationResult.getAdminArea()
emitter.onSuccess(adminArea)
} catch (e: IllegalArgumentException) {
emitter.onError(e)
} catch (e: Exception) {
Timber.e(e, "Error finding user current location")
emitter.onSuccess(resource.getString(R.string.no_location).nullToEmpty())
}
}
}, null)
Expand Down