Skip to content

Commit afb34d1

Browse files
author
Marco Romano
committed
Refactor a custom alignment to its own modifier for readability.
1 parent 4162d16 commit afb34d1

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

features/location/api/src/main/kotlin/io/element/android/features/location/api/StaticMapView.kt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ import androidx.compose.ui.Modifier
2929
import androidx.compose.ui.graphics.Color
3030
import androidx.compose.ui.layout.ContentScale
3131
import androidx.compose.ui.platform.LocalContext
32-
import androidx.compose.ui.unit.IntOffset
3332
import androidx.compose.ui.unit.dp
3433
import coil.compose.AsyncImagePainter
3534
import coil.compose.rememberAsyncImagePainter
3635
import coil.request.ImageRequest
3736
import io.element.android.features.location.api.internal.StaticMapPlaceholder
37+
import io.element.android.features.location.api.internal.centerBottomEdge
3838
import io.element.android.features.location.api.internal.staticMapUrl
3939
import io.element.android.libraries.designsystem.preview.DayNightPreviews
4040
import io.element.android.libraries.designsystem.preview.ElementPreview
@@ -106,13 +106,7 @@ fun StaticMapView(
106106
resourceId = DesignSystemR.drawable.pin,
107107
contentDescription = null,
108108
tint = Color.Unspecified,
109-
modifier = Modifier.align { size, space, _ ->
110-
// Center bottom edge of pin (i.e. its arrow) to center of screen
111-
IntOffset(
112-
x = (space.width - size.width) / 2,
113-
y = (space.height / 2) - size.height,
114-
)
115-
}
109+
modifier = Modifier.centerBottomEdge(this),
116110
)
117111
} else {
118112
StaticMapPlaceholder(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2023 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.element.android.features.location.api.internal
18+
19+
import androidx.compose.foundation.layout.BoxScope
20+
import androidx.compose.ui.Modifier
21+
import androidx.compose.ui.unit.IntOffset
22+
23+
/**
24+
* Horizontally aligns the content to the center of the space.
25+
* Vertically aligns the bottom edge of the content to the center of the space.
26+
*/
27+
fun Modifier.centerBottomEdge(scope: BoxScope): Modifier = with(scope) {
28+
then(
29+
Modifier.align { size, space, _ ->
30+
IntOffset(
31+
x = (space.width - size.width) / 2,
32+
y = (space.height / 2) - size.height,
33+
)
34+
}
35+
)
36+
}

features/location/impl/src/main/kotlin/io/element/android/features/location/impl/send/SendLocationView.kt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ import androidx.compose.ui.Modifier
4343
import androidx.compose.ui.graphics.Color
4444
import androidx.compose.ui.res.stringResource
4545
import androidx.compose.ui.tooling.preview.PreviewParameter
46-
import androidx.compose.ui.unit.IntOffset
4746
import androidx.compose.ui.unit.dp
4847
import com.mapbox.mapboxsdk.camera.CameraPosition
4948
import io.element.android.features.location.api.Location
49+
import io.element.android.features.location.api.internal.centerBottomEdge
5050
import io.element.android.features.location.api.internal.rememberTileStyleUrl
5151
import io.element.android.features.location.impl.MapDefaults
5252
import io.element.android.libraries.designsystem.components.button.BackButton
@@ -198,13 +198,7 @@ fun SendLocationView(
198198
resourceId = DesignSystemR.drawable.pin,
199199
contentDescription = null,
200200
tint = Color.Unspecified,
201-
modifier = Modifier.align { size, space, _ ->
202-
// Center bottom edge of pin (i.e. its arrow) to center of screen
203-
IntOffset(
204-
x = (space.width - size.width) / 2,
205-
y = (space.height / 2) - size.height,
206-
)
207-
}
201+
modifier = Modifier.centerBottomEdge(this),
208202
)
209203
FloatingActionButton(
210204
onClick = { state.eventSink(SendLocationEvents.SwitchToMyLocationMode) },

0 commit comments

Comments
 (0)