Skip to content

Commit

Permalink
Merge branch 'master' into anandwana001/2749/remove-dynamic-view
Browse files Browse the repository at this point in the history
  • Loading branch information
gino-m authored Dec 1, 2024
2 parents 7c48958 + c982a50 commit a8ca8ee
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ package com.google.android.ground.ui.datacollection.tasks.location
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.ComposeView
import androidx.lifecycle.lifecycleScope
import com.google.android.ground.R
import com.google.android.ground.model.submission.isNotNullOrEmpty
import com.google.android.ground.model.submission.isNullOrEmpty
Expand All @@ -27,9 +33,11 @@ import com.google.android.ground.ui.datacollection.components.ButtonAction
import com.google.android.ground.ui.datacollection.components.TaskView
import com.google.android.ground.ui.datacollection.components.TaskViewFactory
import com.google.android.ground.ui.datacollection.tasks.AbstractTaskFragment
import com.google.android.ground.ui.theme.AppTheme
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import javax.inject.Provider
import kotlinx.coroutines.launch

@AndroidEntryPoint
class CaptureLocationTaskFragment @Inject constructor() :
Expand Down Expand Up @@ -59,6 +67,13 @@ class CaptureLocationTaskFragment @Inject constructor() :
// Ensure that the location lock is enabled, if it hasn't been.
if (isVisible) {
viewModel.enableLocationLock()
lifecycleScope.launch {
viewModel.enableLocationLockFlow.collect {
if (it == LocationLockEnabledState.NEEDS_ENABLE) {
showLocationPermissionDialog()
}
}
}
}
}

Expand All @@ -76,4 +91,28 @@ class CaptureLocationTaskFragment @Inject constructor() :
}
.hide()
}

@Suppress("LabeledExpression")
private fun showLocationPermissionDialog() {
val dialogComposeView =
ComposeView(requireContext()).apply {
setContent {
val openAlertDialog = remember { mutableStateOf(true) }
when {
openAlertDialog.value -> {
AppTheme {
LocationPermissionDialog(
onCancel = { openAlertDialog.value = false },
onDismiss = { openAlertDialog.value = false },
)
}
}
}

DisposableEffect(Unit) { onDispose { (parent as? ViewGroup)?.removeView(this@apply) } }
}
}

(view as ViewGroup).addView(dialogComposeView)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,10 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.ComposeView
import androidx.lifecycle.lifecycleScope
import com.google.android.ground.ui.common.AbstractMapFragmentWithControls
import com.google.android.ground.ui.common.BaseMapViewModel
import com.google.android.ground.ui.map.MapFragment
import com.google.android.ground.ui.theme.AppTheme
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -62,46 +57,8 @@ class CaptureLocationTaskMapFragment @Inject constructor() : AbstractMapFragment
return root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
lifecycleScope.launch {
viewModel.enableLocationLockFlow.collect {
if (it == LocationLockEnabledState.NEEDS_ENABLE) {
showLocationPermissionDialog()
}
}
}
}

override fun onMapReady(map: MapFragment) {
super.onMapReady(map)
viewLifecycleOwner.lifecycleScope.launch { viewModel.onMapReady(mapViewModel) }
}

@Suppress("LabeledExpression")
private fun showLocationPermissionDialog() {
val dialogComposeView =
ComposeView(requireContext()).apply {
setContent {
val openAlertDialog = remember { mutableStateOf(true) }
when {
openAlertDialog.value -> {
AppTheme {
LocationPermissionDialog(
onCancel = {
binding.locationLockBtn.isClickable = false
openAlertDialog.value = false
},
onDismiss = { openAlertDialog.value = false },
)
}
}
}

DisposableEffect(Unit) { onDispose { (parent as? ViewGroup)?.removeView(this@apply) } }
}
}

(view as ViewGroup).addView(dialogComposeView)
}
}

0 comments on commit a8ca8ee

Please sign in to comment.