Skip to content

Commit

Permalink
Merge pull request #31 from tal-sitton/offline-distance
Browse files Browse the repository at this point in the history
added an option to calculate distance offline
  • Loading branch information
tal-sitton authored Sep 15, 2022
2 parents 8d4f844 + 996ee30 commit d7d7df3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 10 additions & 3 deletions app/src/main/java/com/example/firstkotlinapp/CinemaActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices
import java.net.UnknownHostException
import java.util.concurrent.Executors

class CinemaActivity : MyTemplateActivity() {
Expand Down Expand Up @@ -140,7 +141,7 @@ class CinemaActivity : MyTemplateActivity() {
val district: String,
val location: Location
) {
var distance: Int = 0
var distance: Double = 0.0

override fun equals(other: Any?): Boolean {
return other is Cinema && other.name == name && other.district == district
Expand Down Expand Up @@ -188,8 +189,14 @@ class CinemaActivity : MyTemplateActivity() {

private fun sortByLocation(myLocation: Location, mHandler: Handler) {
Looper.prepare()
if (cinemas[0].distance == 0) {
Utils.calcDistance(myLocation, cinemas)
if (cinemas[0].distance == 0.0) {
try {
Utils.calcDistance(myLocation, cinemas)
} catch (ex: UnknownHostException) {
cinemas.forEach { cinema ->
cinema.distance = myLocation.distanceTo(cinema.location).toDouble()
}
}
}
cinemas.sortBy { cinema -> cinema.distance }
mHandler.sendEmptyMessage(0)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/example/firstkotlinapp/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Utils {
val json = JSONObject(url.readText())
val dist = json.getJSONArray("distances").getJSONArray(0)
for (i in 0 until dist.length() - 1) {
targetCinemas[i].distance = dist.getInt(i + 1)
targetCinemas[i].distance = dist.getDouble(i + 1)
}

}
Expand Down

0 comments on commit d7d7df3

Please sign in to comment.