Skip to content

Commit

Permalink
Make CameraUpdateReason to pure class to prepare KMP (#120)
Browse files Browse the repository at this point in the history
* Make CameraUpdateReason to pure class to prepare KMP

* Add test codes to compare

* Fix compile error
  • Loading branch information
fornewid authored Nov 19, 2024
1 parent 64a3d35 commit bd8587c
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fun CameraEventScreen(upPress: () -> Unit) {
if (cameraPositionState.isMoving) {
Log.d(
"CameraEventScreen",
"Map camera updated due to ${cameraPositionState.cameraUpdateReason.name}"
"Map camera updated due to ${cameraPositionState.cameraUpdateReason}"
)
}
}
Expand Down
40 changes: 29 additions & 11 deletions naver-map-compose/api/current.api
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,42 @@ package com.naver.maps.map.compose {
property @androidx.compose.runtime.Composable @androidx.compose.runtime.ReadOnlyComposable @com.naver.maps.map.compose.NaverMapComposable public static final com.naver.maps.map.compose.CameraPositionState currentCameraPositionState;
}

@androidx.compose.runtime.Immutable public enum CameraUpdateReason {
method public final int getValue();
method public static com.naver.maps.map.compose.CameraUpdateReason valueOf(String value) throws java.lang.IllegalArgumentException, java.lang.NullPointerException;
method public static com.naver.maps.map.compose.CameraUpdateReason[] values();
property public final int value;
enum_constant public static final com.naver.maps.map.compose.CameraUpdateReason CONTROL;
enum_constant public static final com.naver.maps.map.compose.CameraUpdateReason DEVELOPER;
enum_constant public static final com.naver.maps.map.compose.CameraUpdateReason GESTURE;
enum_constant public static final com.naver.maps.map.compose.CameraUpdateReason LOCATION;
enum_constant public static final com.naver.maps.map.compose.CameraUpdateReason NO_MOVEMENT_YET;
enum_constant public static final com.naver.maps.map.compose.CameraUpdateReason UNKNOWN;
@androidx.compose.runtime.Immutable public sealed interface CameraUpdateReason {
field public static final com.naver.maps.map.compose.CameraUpdateReason.Companion Companion;
}

public static final class CameraUpdateReason.CONTENT_PADDING implements com.naver.maps.map.compose.CameraUpdateReason {
field public static final com.naver.maps.map.compose.CameraUpdateReason.CONTENT_PADDING INSTANCE;
}

public static final class CameraUpdateReason.CONTROL implements com.naver.maps.map.compose.CameraUpdateReason {
field public static final com.naver.maps.map.compose.CameraUpdateReason.CONTROL INSTANCE;
}

public static final class CameraUpdateReason.Companion {
method public com.naver.maps.map.compose.CameraUpdateReason fromInt(int reason);
}

public static final class CameraUpdateReason.DEVELOPER implements com.naver.maps.map.compose.CameraUpdateReason {
field public static final com.naver.maps.map.compose.CameraUpdateReason.DEVELOPER INSTANCE;
}

public static final class CameraUpdateReason.GESTURE implements com.naver.maps.map.compose.CameraUpdateReason {
field public static final com.naver.maps.map.compose.CameraUpdateReason.GESTURE INSTANCE;
}

public static final class CameraUpdateReason.LOCATION implements com.naver.maps.map.compose.CameraUpdateReason {
field public static final com.naver.maps.map.compose.CameraUpdateReason.LOCATION INSTANCE;
}

public static final class CameraUpdateReason.NO_MOVEMENT_YET implements com.naver.maps.map.compose.CameraUpdateReason {
field public static final com.naver.maps.map.compose.CameraUpdateReason.NO_MOVEMENT_YET INSTANCE;
}

public static final class CameraUpdateReason.UNKNOWN implements com.naver.maps.map.compose.CameraUpdateReason {
field public static final com.naver.maps.map.compose.CameraUpdateReason.UNKNOWN INSTANCE;
}

public final class CircleOverlayDefaults {
field public static final int GlobalZIndex = -200000; // 0xfffcf2c0
field public static final com.naver.maps.map.compose.CircleOverlayDefaults INSTANCE;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2024 SOUP
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.naver.maps.map.compose

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.naver.maps.map.CameraUpdate
import org.junit.Test
import org.junit.runner.RunWith
import kotlin.test.assertEquals

@RunWith(AndroidJUnit4::class)
internal class CameraUpdateReasonTest {

@Test
fun equals_count_of_CameraUpdateReason() {
assertEquals(
expected = 5,
actual = CameraUpdate::class.publicStaticFields.count {
it.name.startsWith(prefix = "REASON_")
},
message = "The number of static fields CameraUpdate.REASON_* in NaverMap SDK is not exactly 5.",
)
}

@Test
fun equals_value_of_CameraUpdateReason() {
val naverReasons = listOf(
CameraUpdate.REASON_DEVELOPER,
CameraUpdate.REASON_GESTURE,
CameraUpdate.REASON_CONTROL,
CameraUpdate.REASON_LOCATION,
CameraUpdate.REASON_CONTENT_PADDING,
)
val composeReasons = listOf(
CameraUpdateReason.DEVELOPER,
CameraUpdateReason.GESTURE,
CameraUpdateReason.CONTROL,
CameraUpdateReason.LOCATION,
CameraUpdateReason.CONTENT_PADDING,
)
assertEquals(
expected = composeReasons,
actual = naverReasons.map { reason ->
CameraUpdateReason.fromInt(reason = reason)
},
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,23 @@
*/
package com.naver.maps.map.compose

import java.lang.reflect.Field
import java.lang.reflect.Modifier
import kotlin.reflect.KClass

/**
* The list of the immediate subclasses if this class is a sealed class, or an empty list otherwise.
*/
internal inline fun <reified T> sealedSubclasses(): List<T> {
return T::class.sealedSubclasses.mapNotNull { it.objectInstance }
}

/**
* Returns the public static fields of the class, or an empty list otherwise.
*/
internal val KClass<*>.publicStaticFields: List<Field>
get() = runCatching {
java.declaredFields.filter { field ->
Modifier.isStatic(field.modifiers) && Modifier.isPublic(field.modifiers)
}
}.getOrDefault(emptyList())
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.naver.maps.map.compose

import androidx.compose.runtime.Immutable
import com.naver.maps.map.CameraUpdate as NaverCameraUpdate

/**
* ์ง€๋„ ์นด๋ฉ”๋ผ๊ฐ€ ์›€์ง์˜€์Œ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ’๋“ค.
Expand All @@ -32,18 +33,25 @@ import androidx.compose.runtime.Immutable
* ์ด ๊ฒฝ์šฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์ƒˆ ์ƒ์ˆ˜๋ฅผ ํฌํ•จํ•˜๋„๋ก ์—…๋ฐ์ดํŠธ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
*/
@Immutable
public enum class CameraUpdateReason(public val value: Int) {
UNKNOWN(2),
NO_MOVEMENT_YET(1),
DEVELOPER(com.naver.maps.map.CameraUpdate.REASON_DEVELOPER),
GESTURE(com.naver.maps.map.CameraUpdate.REASON_GESTURE),
CONTROL(com.naver.maps.map.CameraUpdate.REASON_CONTROL),
LOCATION(com.naver.maps.map.CameraUpdate.REASON_LOCATION),
;
public sealed interface CameraUpdateReason {
public data object UNKNOWN : CameraUpdateReason
public data object NO_MOVEMENT_YET : CameraUpdateReason
public data object DEVELOPER : CameraUpdateReason
public data object GESTURE : CameraUpdateReason
public data object CONTROL : CameraUpdateReason
public data object LOCATION : CameraUpdateReason
public data object CONTENT_PADDING : CameraUpdateReason

public companion object {
public fun fromInt(reason: Int): CameraUpdateReason {
return values().firstOrNull { it.value == reason } ?: return UNKNOWN
return when (reason) {
NaverCameraUpdate.REASON_DEVELOPER -> DEVELOPER
NaverCameraUpdate.REASON_GESTURE -> GESTURE
NaverCameraUpdate.REASON_CONTROL -> CONTROL
NaverCameraUpdate.REASON_LOCATION -> LOCATION
NaverCameraUpdate.REASON_CONTENT_PADDING -> CONTENT_PADDING
else -> UNKNOWN
}
}
}
}

0 comments on commit bd8587c

Please sign in to comment.