-
Notifications
You must be signed in to change notification settings - Fork 217
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
387 offline mappacks #417
base: master
Are you sure you want to change the base?
387 offline mappacks #417
Changes from 3 commits
e820300
3acb68f
6681dc6
cf879ea
8d61959
ae3d9b9
858a7c5
29a90da
f21bb75
282d5ae
34a0c93
8da0c32
8b45fa8
55b129e
9ab2421
486a8d5
ac94e9f
c7389c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package net.cyclestreets.api | ||
|
||
data class Map( | ||
val id: String, | ||
val name: String, | ||
val url: String, | ||
val parent: String | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package net.cyclestreets.api | ||
|
||
import android.os.AsyncTask | ||
|
||
class Maps( | ||
private val packs: Collection<Map> | ||
): Iterable<Map> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this would read easier as a one-liner rather than these 3 lines, but maybe that's just me. |
||
val size get() = packs.size | ||
override operator fun iterator() = packs.iterator() | ||
|
||
companion object { | ||
private var loaded_: Maps? = null | ||
|
||
fun get(): Maps? { | ||
if (loaded_ == null) | ||
backgroundLoad() | ||
return loaded_ | ||
} // get | ||
|
||
|
||
private fun backgroundLoad() { | ||
GetMapsTask().execute() | ||
} | ||
|
||
private class GetMapsTask : AsyncTask<Void?, Void?, Maps?>() { | ||
override fun doInBackground(vararg params: Void?): Maps? { | ||
return load() | ||
} | ||
|
||
override fun onPostExecute(maps: Maps?) { | ||
loaded_ = maps | ||
} | ||
} | ||
|
||
private fun load(): Maps? { | ||
try { | ||
return ApiClient.getMaps() | ||
} catch (e: Exception) { | ||
println(e.message) | ||
} | ||
return null | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package net.cyclestreets.api.client.geojson | ||
|
||
import net.cyclestreets.api.Map | ||
import net.cyclestreets.api.Maps | ||
import net.cyclestreets.api.client.geojson.AbstractObjectFactory.propertyOrDefault | ||
import org.geojson.Feature | ||
import org.geojson.FeatureCollection | ||
|
||
class MapsFactory { | ||
companion object { | ||
fun toMaps(featureCollection: FeatureCollection): Maps { | ||
return Maps( | ||
featureCollection.features | ||
.map { f -> toMap(f) } | ||
.filter { m -> isBritainOrIreland(m) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with this restriction as a starting point. We can consider adding others subsequently as part of #46. |
||
) | ||
} | ||
|
||
private fun toMap(feature: Feature): Map { | ||
return Map( | ||
feature.getProperty("id"), | ||
feature.getProperty("name"), | ||
feature.getProperty("url"), | ||
propertyOrDefault(feature, "parent", "") | ||
) | ||
} | ||
|
||
private fun isBritainOrIreland(m: Map): Boolean { | ||
return isBritainOrIreland(m.id) | ||
} | ||
private fun isBritainOrIreland(p: String): Boolean { | ||
return p.contains("great-britain") || p.contains("ireland") | ||
} | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package net.cyclestreets.util | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.net.Uri | ||
import net.cyclestreets.api.Maps | ||
import net.cyclestreets.api.Map | ||
import java.io.File | ||
import java.io.FileInputStream | ||
import java.io.FilenameFilter | ||
import java.io.IOException | ||
import java.util.* | ||
|
||
class MapPack private constructor( | ||
private val map: Map | ||
) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this would read easier as a one-liner rather than these 3 lines, but maybe that's just me. |
||
val name get() = map.name | ||
val path get() = "none" | ||
val current get() = false | ||
|
||
private class CycleStreetsMapFilter : FilenameFilter { | ||
override fun accept(dir: File, name: String): Boolean { | ||
return name.contains("net.cyclestreets.maps") | ||
} | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
fun searchGooglePlay(context: Context) { | ||
val play = Intent(Intent.ACTION_VIEW) | ||
play.data = Uri.parse("market://search?q=net.cyclestreets") | ||
context.startActivity(play) | ||
} | ||
|
||
fun availableMapPacks(context: Context?): List<MapPack> { | ||
val maps = Maps.get() ?: return emptyList() | ||
|
||
return maps.map {m -> MapPack(m) } | ||
} | ||
|
||
@JvmStatic | ||
fun findByPackage(context: Context?, packageName: String?): MapPack? { | ||
for (pack in availableMapPacks(context)) if (pack.path.contains(packageName!!)) return pack | ||
return null | ||
} | ||
|
||
private fun findMapFile(mapDir: File, prefix: String): File? { | ||
for (c in mapDir.listFiles()) if (c.name.startsWith(prefix)) return c | ||
return null | ||
} | ||
|
||
private fun mapProperties(mapDir: File): Properties { | ||
val details = Properties() | ||
try { | ||
val detailsFile = findMapFile(mapDir, "patch.") | ||
details.load(FileInputStream(detailsFile)) | ||
} catch (e: IOException) { | ||
} catch (e: RuntimeException) { | ||
} | ||
return details | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
picky, but 8 characters indentation seems excessive.
Also I wonder if we would be better off naming this class differently - e.g.
MapPack
- so we don't confuse it with the nativeMap
class in Kotlin and/or Java.If you agree with this we could probably update the naming of the
Maps
class, and possible thegetMaps
method also.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I now see it can't be
MapPack
as that would conflict.What about
MapReference
or something similar; as effectively it acts as a reference to a downloadable map pack?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I started with MapPack, realised, changed to Map as a holding name, now considering DownloadableMap