Skip to content

Commit 47a24ed

Browse files
anhanh11001iamareebjamal
authored andcommitted
fix: App crash when no location found (#2259)
Fixes: #2255
1 parent d46c109 commit 47a24ed

File tree

6 files changed

+35
-9
lines changed

6 files changed

+35
-9
lines changed

app/src/fdroid/java/org/fossasia/openevent/general/search/location/LocationServiceImpl.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ import android.location.LocationListener
88
import android.location.LocationManager
99
import android.os.Bundle
1010
import io.reactivex.Single
11+
import org.fossasia.openevent.general.R
12+
import org.fossasia.openevent.general.data.Resource
13+
import org.fossasia.openevent.general.utils.nullToEmpty
1114
import java.lang.IllegalArgumentException
1215
import java.util.Locale
1316

14-
class LocationServiceImpl(private val context: Context) : LocationService {
17+
class LocationServiceImpl(
18+
private val context: Context,
19+
private val resource: Resource
20+
) : LocationService {
1521

1622
@SuppressLint("MissingPermission")
1723
override fun getAdministrativeArea(): Single<String> {
@@ -30,7 +36,8 @@ class LocationServiceImpl(private val context: Context) : LocationService {
3036
}
3137
val addresses = geoCoder.getFromLocation(location.latitude, location.longitude, 1)
3238
try {
33-
val adminArea = addresses[0].adminArea
39+
val adminArea = if (addresses.isNotEmpty()) addresses[0].adminArea
40+
else resource.getString(R.string.no_location).nullToEmpty()
3441
emitter.onSuccess(adminArea)
3542
} catch (e: IllegalArgumentException) {
3643
emitter.onError(e)

app/src/main/java/org/fossasia/openevent/general/di/Modules.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ val commonModule = module {
132132
single { Network() }
133133
single { Resource() }
134134
factory { MutableConnectionLiveData() }
135-
factory<LocationService> { LocationServiceImpl(androidContext()) }
135+
factory<LocationService> { LocationServiceImpl(androidContext(), get()) }
136136
}
137137

138138
val apiModule = module {
@@ -246,7 +246,7 @@ val viewModelModule = module {
246246
viewModel { SearchViewModel(get(), get()) }
247247
viewModel { SearchResultsViewModel(get(), get(), get(), get(), get(), get()) }
248248
viewModel { AttendeeViewModel(get(), get(), get(), get(), get(), get(), get(), get()) }
249-
viewModel { SearchLocationViewModel(get(), get()) }
249+
viewModel { SearchLocationViewModel(get(), get(), get()) }
250250
viewModel { SearchTimeViewModel(get()) }
251251
viewModel { SearchTypeViewModel(get(), get(), get()) }
252252
viewModel { TicketsViewModel(get(), get(), get(), get(), get(), get()) }

app/src/main/java/org/fossasia/openevent/general/event/EventsViewModel.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ class EventsViewModel(
5353
}
5454

5555
fun loadLocationEvents() {
56-
if (mutableSavedLocation.value == null) return
56+
val location = mutableSavedLocation.value
57+
if (location == null || location == resource.getString(R.string.enter_location) ||
58+
location == resource.getString(R.string.no_location)) {
59+
mutableProgress.value = false
60+
return
61+
}
5762

5863
sourceFactory = EventsDataSourceFactory(
5964
compositeDisposable,

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import org.fossasia.openevent.general.BuildConfig
1212
import io.reactivex.android.schedulers.AndroidSchedulers
1313
import io.reactivex.disposables.CompositeDisposable
1414
import io.reactivex.schedulers.Schedulers
15+
import org.fossasia.openevent.general.R
1516
import org.fossasia.openevent.general.data.Preference
17+
import org.fossasia.openevent.general.data.Resource
1618
import org.fossasia.openevent.general.event.EventService
1719
import org.fossasia.openevent.general.event.location.EventLocation
1820
import org.jetbrains.anko.doAsync
@@ -28,7 +30,8 @@ const val SEARCH_INTERVAL = 250L
2830

2931
class SearchLocationViewModel(
3032
private val eventService: EventService,
31-
private val preference: Preference
33+
private val preference: Preference,
34+
private val resource: Resource
3235
) : ViewModel() {
3336

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

50+
if (query == resource.getString(R.string.no_location)) return
51+
4752
if (savedLocationList.size == SAVED_LOCATION_LIST_SIZE)
4853
savedLocationList.removeAt(SAVED_LOCATION_LIST_SIZE - 1)
4954
val index = savedLocationList.indexOf(query)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,5 +525,6 @@
525525
<string name="speaker_experience">Speaking Experience</string>
526526
<string name="linkedin">LinkedIn</string>
527527
<string name="order_now">Order Now</string>
528+
<string name="no_location">No Location</string>
528529

529530
</resources>

app/src/playStore/java/org/fossasia/openevent/general/search/location/LocationServiceImpl.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@ import com.google.android.gms.location.LocationRequest
88
import com.google.android.gms.location.LocationResult
99
import com.google.android.gms.location.LocationServices
1010
import io.reactivex.Single
11+
import org.fossasia.openevent.general.R
12+
import org.fossasia.openevent.general.data.Resource
1113
import org.fossasia.openevent.general.location.LocationPermissionException
1214
import org.fossasia.openevent.general.location.NoLocationSourceException
15+
import org.fossasia.openevent.general.utils.nullToEmpty
1316
import timber.log.Timber
1417
import java.io.IOException
18+
import java.lang.Exception
1519
import java.util.Locale
1620

17-
class LocationServiceImpl(private val context: Context) : LocationService {
21+
class LocationServiceImpl(
22+
private val context: Context,
23+
private val resource: Resource
24+
) : LocationService {
1825

1926
override fun getAdministrativeArea(): Single<String> {
2027
val locationManager = context.getSystemService(Context.LOCATION_SERVICE)
@@ -44,8 +51,9 @@ class LocationServiceImpl(private val context: Context) : LocationService {
4451
try {
4552
val adminArea = locationResult.getAdminArea()
4653
emitter.onSuccess(adminArea)
47-
} catch (e: IllegalArgumentException) {
48-
emitter.onError(e)
54+
} catch (e: Exception) {
55+
Timber.e(e, "Error finding user current location")
56+
emitter.onSuccess(resource.getString(R.string.no_location).nullToEmpty())
4957
}
5058
}
5159
}, null)

0 commit comments

Comments
 (0)