From 01f2d908a42128257d430f09fcfd133e6aa7727a Mon Sep 17 00:00:00 2001 From: geeksville Date: Wed, 11 Mar 2020 18:13:44 -0700 Subject: [PATCH] more map wip --- app/build.gradle | 2 +- .../androidx/ui/androidview/ComposedView.kt | 79 +++++++++++++++++++ .../main/java/com/geeksville/mesh/ui/Map.kt | 11 +-- app/src/main/res/layout/map_view.xml | 5 ++ 4 files changed, 89 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/androidx/ui/androidview/ComposedView.kt create mode 100644 app/src/main/res/layout/map_view.xml diff --git a/app/build.gradle b/app/build.gradle index b6f46faf7..2afedd403 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -87,7 +87,7 @@ dependencies { //implementation 'com.google.protobuf:protobuf-java:3.11.1' //implementation 'com.google.protobuf:protobuf-java-util:3.11.1' implementation 'com.google.protobuf:protobuf-javalite:3.11.1' - + // mapbox implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:9.0.0' diff --git a/app/src/main/java/androidx/ui/androidview/ComposedView.kt b/app/src/main/java/androidx/ui/androidview/ComposedView.kt new file mode 100644 index 000000000..2c24ae27a --- /dev/null +++ b/app/src/main/java/androidx/ui/androidview/ComposedView.kt @@ -0,0 +1,79 @@ +/* + * Copyright 2020 The Android Open Source Project + * + * 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 + * + * http://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 androidx.ui.androidview + +import android.content.Context +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.view.ViewGroup.LayoutParams.MATCH_PARENT +import androidx.annotation.LayoutRes +import androidx.compose.Composable + +/** + * Composes an Android [View] given a layout resource [resId]. The method handles the inflation + * of the [View] and will call the [postInflationCallback] after this happens. Note that the + * callback will always be invoked on the main thread. + * + * @param resId The id of the layout resource to be inflated. + * @param postInflationCallback The callback to be invoked after the layout is inflated. + */ +@Composable +// TODO(popam): support modifiers here +fun AndroidView(@LayoutRes resId: Int, postInflationCallback: (View) -> Unit = { _ -> }) { + AndroidViewHolder( + postInflationCallback = postInflationCallback, + resId = resId + ) +} + +private class AndroidViewHolder(context: Context) : ViewGroup(context) { + var view: View? = null + set(value) { + if (value != field) { + field = value + removeAllViews() + addView(view) + } + } + + var postInflationCallback: (View) -> Unit = {} + + var resId: Int? = null + set(value) { + if (value != field) { + field = value + val inflater = LayoutInflater.from(context) + val view = inflater.inflate(resId!!, this, false) + this.view = view + postInflationCallback(view) + } + } + + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { + view?.measure(widthMeasureSpec, heightMeasureSpec) + setMeasuredDimension(view?.measuredWidth ?: 0, view?.measuredHeight ?: 0) + } + + override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) { + view?.layout(l, t, r, b) + } + + override fun getLayoutParams(): LayoutParams? { + return view?.layoutParams ?: LayoutParams(MATCH_PARENT, MATCH_PARENT) + } +} diff --git a/app/src/main/java/com/geeksville/mesh/ui/Map.kt b/app/src/main/java/com/geeksville/mesh/ui/Map.kt index 52d156d3d..92436f0f7 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/Map.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/Map.kt @@ -1,24 +1,21 @@ package com.geeksville.mesh.ui import androidx.compose.Composable +import androidx.ui.androidview.AndroidView import androidx.ui.core.ContextAmbient -import androidx.ui.layout.Column -import androidx.ui.layout.LayoutPadding -import androidx.ui.layout.LayoutSize import androidx.ui.material.MaterialTheme import androidx.ui.tooling.preview.Preview -import androidx.ui.unit.dp +import com.geeksville.mesh.R @Composable fun MapContent() { - analyticsScreen(name = "channel") + analyticsScreen(name = "map") val typography = MaterialTheme.typography() val context = ContextAmbient.current - Column(modifier = LayoutSize.Fill + LayoutPadding(16.dp)) { - + AndroidView(R.layout.map_view) { } } diff --git a/app/src/main/res/layout/map_view.xml b/app/src/main/res/layout/map_view.xml new file mode 100644 index 000000000..f629a9ee4 --- /dev/null +++ b/app/src/main/res/layout/map_view.xml @@ -0,0 +1,5 @@ + + \ No newline at end of file