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

Compose statistics #5855

Merged
merged 17 commits into from
Sep 1, 2024
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
3 changes: 0 additions & 3 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,6 @@ dependencies {
implementation("me.grantland:autofittextview:0.2.1")
implementation("com.google.android.flexbox:flexbox:3.0.0")

// box2d view
implementation("org.jbox2d:jbox2d-library:2.2.1.1")

// sharing presets/settings via QR Code
implementation("io.github.alexzhirkevich:qrose:1.0.1")
// for encoding information for the URL configuration (QR code)
Expand Down
10 changes: 7 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,19 @@
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="de.westnordost.streetcomplete.screens.FragmentContainerActivity" />
<activity
android:name="de.westnordost.streetcomplete.screens.about.AboutActivity"
android:label="@string/action_about2"
android:parentActivityName="de.westnordost.streetcomplete.screens.MainActivity"
android:configChanges="density|fontScale|keyboard|keyboardHidden|layoutDirection|locale|mcc|mnc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|touchscreen|uiMode"
/>
<activity
android:name="de.westnordost.streetcomplete.screens.user.UserActivity"
android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity" />
android:label="@string/user_profile"
android:windowSoftInputMode="adjustResize"
android:parentActivityName="de.westnordost.streetcomplete.screens.MainActivity"
android:configChanges="density|fontScale|keyboard|keyboardHidden|layoutDirection|locale|mcc|mnc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|touchscreen|uiMode"
/>
<!-- For WorkManager -->
<service
android:name="androidx.work.impl.foreground.SystemForegroundService"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import de.westnordost.streetcomplete.screens.about.logs.LogsScreen
import de.westnordost.streetcomplete.ui.ktx.dir
import org.koin.androidx.compose.koinViewModel

@Composable fun AboutNavHost(onClickBack: () -> Unit) {
@Composable
fun AboutNavHost(onClickBack: () -> Unit) {
val navController = rememberNavController()
val dir = LocalLayoutDirection.current.dir

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime

@Composable
fun LogsItem(
fun LogsRow(
log: LogMessage,
modifier: Modifier = Modifier,
) {
Expand Down Expand Up @@ -46,8 +46,8 @@ fun LogsItem(

@Preview
@Composable
private fun LogsItemPreview() {
LogsItem(LogMessage(
private fun LogsRowPreview() {
LogsRow(LogMessage(
level = LogLevel.DEBUG,
tag = "Test",
message = "Aspernatur rerum aperiam id error laborum possimus rerum",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fun LogsScreen(
LazyColumn(state = listState) {
itemsIndexed(logs) { index, item ->
if (index > 0) Divider()
LogsItem(item, modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp))
LogsRow(item, modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import de.westnordost.streetcomplete.ui.common.dialogs.ConfirmationDialog
import de.westnordost.streetcomplete.ui.common.dialogs.TextInputDialog

@Composable
fun QuestPresetItem(
fun QuestPresetRow(
item: QuestPresetSelection,
onSelect: () -> Unit,
onRename: (name: String) -> Unit,
Expand Down Expand Up @@ -120,8 +120,8 @@ fun QuestPresetItem(

@Preview
@Composable
private fun PreviewQuestPresetItem() {
QuestPresetItem(
private fun PreviewQuestPresetRow() {
QuestPresetRow(
item = QuestPresetSelection(1L, "A quest preset name", false),
onSelect = {},
onRename = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private fun QuestPresetsList(viewModel: QuestPresetsViewModel) {
itemsIndexed(presets, key = { _, it -> it.id }) { index, item ->
Column {
if (index > 0) Divider()
QuestPresetItem(
QuestPresetRow(
item = item,
onSelect = { viewModel.select(item.id) },
onRename = { viewModel.rename(item.id, it) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fun QuestSelectionList(
itemsIndexed(items, key = { _, it -> it.questType.name }) { index, item ->
Column(Modifier.background(MaterialTheme.colors.surface)) {
if (index > 0) Divider()
QuestSelectionItem(
QuestSelectionRow(
item = item,
onToggleSelection = { isSelected ->
// when enabling quest that is disabled by default, require confirmation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import de.westnordost.streetcomplete.quests.surface.AddRoadSurface
/** Single item the the quest selection list. Shows icon + title, whether it is enabled and whether
* it is disabled by default / disabled in the country one is in */
@Composable
fun QuestSelectionItem(
fun QuestSelectionRow(
item: QuestSelection,
onToggleSelection: (isSelected: Boolean) -> Unit,
displayCountry: String,
Expand Down Expand Up @@ -93,10 +93,10 @@ private fun DisabledHint(text: String) {

@Preview
@Composable
private fun QuestSelectionItemPreview() {
private fun QuestSelectionRowPreview() {
var selected by remember { mutableStateOf(true) }

QuestSelectionItem(
QuestSelectionRow(
item = QuestSelection(AddRoadSurface(), selected, false),
onToggleSelection = { selected = !selected },
displayCountry = "Atlantis",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package de.westnordost.streetcomplete.screens.user

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.LocalContentColor
import androidx.compose.material.MaterialTheme
import androidx.compose.material.contentColorFor
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.coerceAtMost
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.min
import de.westnordost.streetcomplete.ui.util.backgroundWithPadding

/** Layout that appears like a dialog and leaves an offset for an icon that appears displaced
* half-on-top on the dialog. On portrait layout, the icon is on top of the content, on landscape
* layout, it is to the start */
@Composable
fun DialogContentWithIconLayout(
icon: @Composable () -> Unit,
content: @Composable (isLandscape: Boolean) -> Unit,
modifier: Modifier = Modifier,
) {
// in landscape layout, dialog would become too tall to fit
BoxWithConstraints(modifier) {
val isLandscape = maxWidth > maxHeight

// scale down icon to fit small devices
val iconSize = (min(maxWidth, maxHeight) * 0.67f).coerceAtMost(320.dp)

val backgroundPadding =
if (isLandscape) PaddingValues(start = iconSize * 0.75f)
else PaddingValues(top = iconSize * 0.75f)

val dialogModifier = modifier
.backgroundWithPadding(
color = MaterialTheme.colors.surface,
padding = backgroundPadding,
shape = MaterialTheme.shapes.medium
)
.padding(24.dp)

val contentColor = contentColorFor(MaterialTheme.colors.surface)
CompositionLocalProvider(LocalContentColor provides contentColor) {
if (isLandscape) {
Row(
modifier = dialogModifier.width(maxWidth.coerceAtMost(720.dp)),
horizontalArrangement = Arrangement.spacedBy(24.dp),
verticalAlignment = Alignment.CenterVertically
) {
Box(
modifier = Modifier.size(iconSize),
contentAlignment = Alignment.CenterEnd
) { icon() }
content(true)
}
} else {
Column(
modifier = dialogModifier,
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
Box(
modifier = Modifier.size(iconSize),
contentAlignment = Alignment.BottomCenter
) { icon() }
content(false)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,109 +1,25 @@
package de.westnordost.streetcomplete.screens.user

import android.os.Bundle
import android.view.MenuItem
import android.view.View
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import de.westnordost.streetcomplete.R
import de.westnordost.streetcomplete.data.osm.edits.EditType
import de.westnordost.streetcomplete.screens.FragmentContainerActivity
import de.westnordost.streetcomplete.screens.HasTitle
import de.westnordost.streetcomplete.screens.user.login.LoginFragment
import de.westnordost.streetcomplete.screens.user.statistics.CountryInfoFragment
import de.westnordost.streetcomplete.screens.user.statistics.EditStatisticsFragment
import de.westnordost.streetcomplete.screens.user.statistics.EditTypeInfoFragment
import de.westnordost.streetcomplete.util.ktx.observe
import org.koin.androidx.viewmodel.ext.android.viewModel
import androidx.activity.compose.setContent
import androidx.compose.material.Surface
import de.westnordost.streetcomplete.screens.BaseActivity
import de.westnordost.streetcomplete.ui.theme.AppTheme

/** Shows all the user information, login etc.
* This activity coordinates quite a number of fragments, which all call back to this one. In order
* of appearance:
* The LoginFragment, the UserFragment (which contains the viewpager with more
* fragments) and the "fake" dialog QuestTypeInfoFragment.
*/
class UserActivity :
FragmentContainerActivity(R.layout.activity_user),
EditStatisticsFragment.Listener {

private val viewModel by viewModel<UserViewModel>()

private val countryDetailsFragment get() =
supportFragmentManager.findFragmentById(R.id.countryDetailsFragment) as CountryInfoFragment?

private val editTypeDetailsFragment get() =
supportFragmentManager.findFragmentById(R.id.editTypeDetailsFragment) as EditTypeInfoFragment?

private val fragmentLifecycleCallbacks = object : FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentStarted(fragmentManager: FragmentManager, fragment: Fragment) {
if (fragment.id == R.id.fragment_container && fragment is HasTitle) {
title = fragment.title
supportActionBar?.subtitle = fragment.subtitle
}
}
}

/* --------------------------------------- Lifecycle --------------------------------------- */
class UserActivity : BaseActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (savedInstanceState == null) {
replaceMainFragment(when {
intent.getBooleanExtra(EXTRA_LAUNCH_AUTH, false) -> LoginFragment.create(true)
viewModel.isLoggedIn.value -> UserFragment()
else -> LoginFragment.create()
})
}

observe(viewModel.isLoggedIn) { isLoggedIn ->
val current = getMainFragment()
val replaceFragment = when (isLoggedIn) {
true -> current !is UserFragment
false -> current !is LoginFragment
val launchAuth = intent.getBooleanExtra(EXTRA_LAUNCH_AUTH, false)
setContent {
AppTheme {
Surface {
UserNavHost(
launchAuth = launchAuth,
onClickBack = { finish() }
)
}
}
if (replaceFragment) {
replaceMainFragmentAnimated(if (isLoggedIn) UserFragment() else LoginFragment())
}
}

val toolbar = findViewById<Toolbar>(R.id.toolbar)
if (toolbar != null) {
setSupportActionBar(toolbar)
supportActionBar!!.setDisplayShowHomeEnabled(true)
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
}

supportFragmentManager.registerFragmentLifecycleCallbacks(fragmentLifecycleCallbacks, false)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return if (item.itemId == android.R.id.home) {
finish()
return true
} else {
super.onOptionsItemSelected(item)
}
}

/* --------------------------- QuestStatisticsFragment.Listener ----------------------------- */

override fun onClickedEditType(editType: EditType, editCount: Int, questBubbleView: View) {
editTypeDetailsFragment?.show(editType, editCount, questBubbleView)
}

override fun onClickedCountryFlag(country: String, editCount: Int, rank: Int?, countryBubbleView: View) {
countryDetailsFragment?.show(country, editCount, rank, countryBubbleView)
}

/* ------------------------------------------------------------------------------------------ */

private fun replaceMainFragmentAnimated(fragment: Fragment) {
replaceMainFragment(fragment) {
setCustomAnimations(
R.anim.fade_in_from_bottom, R.anim.fade_out_to_top,
R.anim.fade_in_from_bottom, R.anim.fade_out_to_top
)
}
}

Expand Down
Loading