-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvement for deferred configuration
- Loading branch information
Showing
7 changed files
with
137 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
demo/src/main/java/com/peterlaurence/mapview/demo/fragments/DeferredFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.peterlaurence.mapview.demo.fragments | ||
|
||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.fragment.app.Fragment | ||
import com.peterlaurence.mapview.MapView | ||
import com.peterlaurence.mapview.MapViewConfiguration | ||
import com.peterlaurence.mapview.core.TileStreamProvider | ||
import com.peterlaurence.mapview.demo.R | ||
import kotlinx.coroutines.* | ||
import java.io.InputStream | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
/** | ||
* An example showing deferred configuration of [MapView]. In this example, a [MapView] is first | ||
* added to the view hierarchy, then a few moment later it's configured. | ||
* | ||
* It demonstrates that the [MapView] configuration can be done later (when all necessary data is | ||
* gathered). It's important since the Android framework requires a view to be added inside the | ||
* [onCreateView] lifecycle callback to have its state saved and restored upon device rotation. | ||
* But inside [onCreateView] we may not have (yet) all information needed to call [MapView.configure]. | ||
*/ | ||
class DeferredFragment : Fragment(), CoroutineScope { | ||
|
||
private lateinit var mapView: MapView | ||
private lateinit var parentView: ViewGroup | ||
private var job: Job? = null | ||
|
||
override val coroutineContext: CoroutineContext | ||
get() = Dispatchers.Main + Job().also { job = it } | ||
|
||
/** | ||
* The [MapView] should always be added inside [onCreateView], to ensure state save/restore. | ||
*/ | ||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View? { | ||
return inflater.inflate(R.layout.fragment_layout, container, false).also { | ||
parentView = it as ViewGroup | ||
context?.let { ctx -> | ||
MapView(ctx).addToFragment() | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Assign an id the the [MapView] (it's necessary to enable state save/restore). | ||
* And keep a ref on the [MapView] | ||
*/ | ||
private fun MapView.addToFragment() { | ||
this@DeferredFragment.mapView = this | ||
|
||
mapView.id = R.id.mapview_id | ||
mapView.isSaveEnabled = true | ||
|
||
parentView.addView(mapView, 0) | ||
} | ||
|
||
/** | ||
* In this example, the configuration isn't done **immediately** after the [MapView] is added to | ||
* the view hierarchy, in [onCreateView]. It's done in [onStart]. | ||
* But it's not mandatory, it could have been done right after the [MapView] creation. | ||
*/ | ||
override fun onStart() { | ||
super.onStart() | ||
|
||
val tileStreamProvider = object : TileStreamProvider { | ||
override fun getTileStream(row: Int, col: Int, zoomLvl: Int): InputStream? { | ||
return try { | ||
context?.assets?.open("tiles/esp/$zoomLvl/$row/$col.jpg") | ||
} catch (e: Exception) { | ||
null | ||
} | ||
} | ||
} | ||
val tileSize = 256 | ||
val config = MapViewConfiguration( | ||
5, 8192, 8192, tileSize, tileStreamProvider | ||
).setMaxScale(2f).setPadding(tileSize * 2) | ||
|
||
launch { | ||
delay(500) // simulate delay | ||
mapView.configure(config) | ||
} | ||
} | ||
|
||
override fun onStop() { | ||
super.onStop() | ||
|
||
job?.cancel() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters