Skip to content

fix #53 #61

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 2 commits into from
Mar 28, 2023
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 @@ -2,7 +2,8 @@ package dev.note11.flutter_naver_map.flutter_naver_map.view

import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.ComponentCallbacks
import android.content.res.Configuration
import android.os.Bundle
import android.view.View
import com.naver.maps.map.MapView
Expand All @@ -18,24 +19,23 @@ import dev.note11.flutter_naver_map.flutter_naver_map.util.NLocationSource
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.platform.PlatformView


internal class NaverMapView(
context: Context,
private val activity: Activity,
private val naverMapViewOptions: NaverMapViewOptions,
private val channel: MethodChannel,
private val overlayController: OverlayHandler,
) : PlatformView, Application.ActivityLifecycleCallbacks {
) : PlatformView, Application.ActivityLifecycleCallbacks, ComponentCallbacks {

private lateinit var naverMap: NaverMap
private lateinit var naverMapControlSender: NaverMapControlSender
private val mapView = MapView(context, naverMapViewOptions.naverMapOptions).apply {
private val mapView = MapView(activity, naverMapViewOptions.naverMapOptions).apply {
setTempMethodCallHandler()
getMapAsync { naverMap ->
[email protected] = naverMap
onMapReady()
}
}
private var isListenerRegistered = false
private var rawNaverMapOptionTempCache: Any? = null

init {
Expand All @@ -45,9 +45,7 @@ internal class NaverMapView(

private fun setTempMethodCallHandler() {
channel.setMethodCallHandler { call, _ ->
if (call.method == "updateOptions") { // todo : test
rawNaverMapOptionTempCache = call.arguments
}
if (call.method == "updateOptions") rawNaverMapOptionTempCache = call.arguments
}
}

Expand All @@ -73,6 +71,7 @@ internal class NaverMapView(
}

private fun setMapTapListener() {
isListenerRegistered = true
naverMap.run {
setOnMapClickListener { pointFPx, latLng ->
naverMapControlSender.onMapTapped(NPoint.fromPointFWithPx(pointFPx), latLng)
Expand All @@ -87,10 +86,23 @@ internal class NaverMapView(
}
}

private fun removeMapTapListener() {
if (isListenerRegistered) {
naverMap.run {
onMapClickListener = null
onSymbolClickListener = null
removeOnCameraChangeListener(naverMapControlSender::onCameraChange)
removeOnCameraIdleListener(naverMapControlSender::onCameraIdle)
removeOnIndoorSelectionChangeListener(naverMapControlSender::onSelectedIndoorChanged)
}
}
}

override fun getView(): View = mapView

override fun dispose() {
unRegisterLifecycleCallback()
removeMapTapListener()

mapView.run {
onPause()
Expand All @@ -108,24 +120,20 @@ internal class NaverMapView(
}

private fun registerLifecycleCallback() {
mapView.onSaveInstanceState(Bundle())
activity.registerComponentCallbacks(this)
activity.application.registerActivityLifecycleCallbacks(this)
}

private fun unRegisterLifecycleCallback() {
activity.unregisterComponentCallbacks(this)
activity.application.unregisterActivityLifecycleCallbacks(this)
}

override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
if (activity != this.activity) return

mapView.onCreate(savedInstanceState)
}

override fun onActivityStarted(activity: Activity) {
if (activity != this.activity) return
/** 실행되지 않음 */
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) = Unit

mapView.onStart()
}
override fun onActivityStarted(activity: Activity) = Unit

override fun onActivityResumed(activity: Activity) {
if (activity != this.activity) return
Expand All @@ -134,14 +142,6 @@ internal class NaverMapView(
mapView.onResume()
}

private fun reloadMap() {
if (this::naverMap.isInitialized) {
val nowMapType = naverMap.mapType
naverMap.mapType = NaverMap.MapType.None
naverMap.mapType = nowMapType
}
}

override fun onActivityPaused(activity: Activity) {
if (activity != this.activity) return

Expand All @@ -166,4 +166,18 @@ internal class NaverMapView(
mapView.onDestroy()
unRegisterLifecycleCallback()
}

override fun onConfigurationChanged(newConfig: Configuration) {}

override fun onLowMemory() {
mapView.onLowMemory()
}

private fun reloadMap() {
if (this::naverMap.isInitialized) {
val nowMapType = naverMap.mapType
naverMap.mapType = NaverMap.MapType.None
naverMap.mapType = nowMapType
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ internal class NaverMapViewFactory(
val options = NaverMapViewOptions.fromMessageable(convertedArgs)

return NaverMapView(
context = context,
activity = activity,
naverMapViewOptions = options,
channel = channel,
overlayController = overlayController
overlayController = overlayController,
)
}
}