Skip to content
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

Moving the Building Levels quest form to Jetpack Compose. #6022

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
@@ -0,0 +1,62 @@
package de.westnordost.streetcomplete.quests.building_levels

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import de.westnordost.streetcomplete.R

@Composable
fun AddBuildingLevelsButton(lastLevels: Int, lastRoofLevels: Int?, position: Int,onClick: (position: Int) -> Unit){
Row(modifier = Modifier.padding(5.dp)) {
Row(modifier = Modifier
.height(52.dp)
.wrapContentWidth()
.widthIn(min=42.dp)
.clip(RoundedCornerShape(3.dp))
.background(Color.LightGray)
.padding(3.dp)
.clickable { onClick(position) }, verticalAlignment = Alignment.CenterVertically) {
Text(
text = lastLevels.toString(),
fontSize = 14.sp,
modifier = Modifier.align(Alignment.Bottom)
)
Image(
painterResource(R.drawable.ic_building_levels_illustration),
"Building Illustration",
modifier = Modifier.wrapContentSize()
)
Text(
text = lastRoofLevels?.toString() ?: " ",
fontSize = 14.sp,
modifier = Modifier.align(Alignment.Top)
)
}
}
}


@Composable
@Preview(showBackground = true,
name = "Add Building Levels Button"
)
fun PreviewAddBuildingLevelsButton(){
AddBuildingLevelsButton(3,1,2) { }
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package de.westnordost.streetcomplete.quests.building_levels

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.compose.material.MaterialTheme
import androidx.compose.ui.platform.ComposeView
import androidx.core.widget.doAfterTextChanged
import androidx.recyclerview.widget.RecyclerView
import de.westnordost.streetcomplete.R
import de.westnordost.streetcomplete.data.preferences.Preferences
import de.westnordost.streetcomplete.databinding.QuestBuildingLevelsBinding
import de.westnordost.streetcomplete.databinding.QuestBuildingLevelsLastPickedButtonBinding
import de.westnordost.streetcomplete.quests.AbstractOsmQuestForm
import de.westnordost.streetcomplete.quests.AnswerItem
import de.westnordost.streetcomplete.util.ktx.intOrNull
Expand Down Expand Up @@ -90,28 +90,31 @@ private class LastPickedAdapter(
) : RecyclerView.Adapter<LastPickedAdapter.ViewHolder>() {

class ViewHolder(
private val binding: QuestBuildingLevelsLastPickedButtonBinding,
private val composeView: ComposeView,
private val onItemClicked: (position: Int) -> Unit
) : RecyclerView.ViewHolder(binding.root) {
) : RecyclerView.ViewHolder(composeView) {

init {
itemView.setOnClickListener { onItemClicked(bindingAdapterPosition) }
}

fun onBind(item: BuildingLevelsAnswer) {
binding.lastLevelsLabel.text = item.levels.toString()
binding.lastRoofLevelsLabel.text = item.roofLevels?.toString() ?: " "
fun onBind(item: BuildingLevelsAnswer, position: Int) {
composeView.setContent {
MaterialTheme {
AddBuildingLevelsButton(item.levels,item.roofLevels,position,onItemClicked)
}
}
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = QuestBuildingLevelsLastPickedButtonBinding.inflate(inflater, parent, false)
return ViewHolder(binding, onItemClicked)
val composeView = ComposeView(parent.context)
composeView.layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
return ViewHolder(composeView, onItemClicked)
}

override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
viewHolder.onBind(lastPickedAnswers[position])
viewHolder.onBind(lastPickedAnswers[position],position)
}

override fun getItemCount() = lastPickedAnswers.size
Expand Down