Skip to content

Commit

Permalink
Log unknown MatchType/ExpressionType and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
sufyanAbbasi committed Feb 28, 2024
1 parent 369eaea commit e9e07d3
Show file tree
Hide file tree
Showing 14 changed files with 199 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ typealias TaskSelections = Map<TaskId, Set<OptionId>>
* Describes a user-defined condition on a task, which determines whether the given task should be
* hidden due to failure of fulfillment based on the input expressions.
*/
data class Condition
@JvmOverloads
constructor(
data class Condition(
/** Determines the evaluation condition for fulfillment (e.g. all or some expressions). */
val matchType: MatchType = MatchType.UNKNOWN,
/** The expressions to evaluate to fulfill the condition. */
Expand All @@ -55,9 +53,7 @@ constructor(
}.exhaustive
}

data class Expression
@JvmOverloads
constructor(
data class Expression(
/** Determines the evaluation condition for the expression (e.g. all or some selected options). */
val expressionType: ExpressionType = ExpressionType.UNKNOWN,
/** The task ID associated with this expression. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ abstract class LocalDatabase : RoomDatabase() {
abstract fun offlineAreaDao(): OfflineAreaDao

abstract fun userDao(): UserDao

abstract fun conditionDao(): ConditionDao

abstract fun expressionDao(): ExpressionDao
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ import com.google.android.ground.persistence.local.room.relations.TaskEntityAndR
import com.google.android.ground.ui.map.Bounds
import com.google.common.reflect.TypeToken
import com.google.gson.Gson
import java.util.*
import kotlinx.collections.immutable.toPersistentList
import kotlinx.collections.immutable.toPersistentMap
import org.json.JSONObject
import timber.log.Timber
import java.util.*

fun AuditInfo.toLocalDataStoreObject(): AuditInfoEntity =
AuditInfoEntity(
Expand Down Expand Up @@ -117,14 +117,14 @@ fun JobEntityAndRelations.toModelObject(): Job {
style = jobEntity.style?.toModelObject(),
name = jobEntity.name,
strategy =
jobEntity.strategy.let {
try {
DataCollectionStrategy.valueOf(it)
} catch (e: IllegalArgumentException) {
Timber.e("unknown data collection strategy $it")
DataCollectionStrategy.UNKNOWN
}
},
jobEntity.strategy.let {
try {
DataCollectionStrategy.valueOf(it)
} catch (e: IllegalArgumentException) {
Timber.e("unknown data collection strategy $it")
DataCollectionStrategy.UNKNOWN
}
},
tasks = taskMap.toPersistentMap()
)
}
Expand Down Expand Up @@ -166,10 +166,11 @@ fun LocationOfInterestEntity.toModelObject(survey: Survey): LocationOfInterest =
geometry = geometry.getGeometry(),
submissionCount = submissionCount,
properties = properties,
job = survey.getJob(jobId = jobId)
?: throw LocalDataConsistencyException(
"Unknown jobId ${this.jobId} in location of interest ${this.id}"
)
job =
survey.getJob(jobId = jobId)
?: throw LocalDataConsistencyException(
"Unknown jobId ${this.jobId} in location of interest ${this.id}"
)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ package com.google.android.ground.persistence.local.room.dao
import androidx.room.Dao
import com.google.android.ground.persistence.local.room.entity.ConditionEntity

@Dao
interface ConditionDao : BaseDao<ConditionEntity>
@Dao interface ConditionDao : BaseDao<ConditionEntity>
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ package com.google.android.ground.persistence.local.room.dao
import androidx.room.Dao
import com.google.android.ground.persistence.local.room.entity.ExpressionEntity

@Dao
interface ExpressionDao : BaseDao<ExpressionEntity>
@Dao interface ExpressionDao : BaseDao<ExpressionEntity>
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import com.google.android.ground.persistence.local.room.fields.MatchEntityType
@Entity(
tableName = "condition",
foreignKeys =
[
ForeignKey(
entity = TaskEntity::class,
parentColumns = ["id"],
childColumns = ["parent_task_id"],
onDelete = ForeignKey.CASCADE
)
],
[
ForeignKey(
entity = TaskEntity::class,
parentColumns = ["id"],
childColumns = ["parent_task_id"],
onDelete = ForeignKey.CASCADE
)
],
indices = [Index("parent_task_id")]
)
data class ConditionEntity(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import com.google.android.ground.persistence.local.room.fields.ExpressionEntityT
@Entity(
tableName = "expression",
foreignKeys =
[
ForeignKey(
entity = ConditionEntity::class,
parentColumns = ["parent_task_id"],
childColumns = ["parent_task_id"],
onDelete = ForeignKey.CASCADE
)
],
[
ForeignKey(
entity = ConditionEntity::class,
parentColumns = ["parent_task_id"],
childColumns = ["parent_task_id"],
onDelete = ForeignKey.CASCADE
)
],
indices = [Index("parent_task_id")]
)
data class ExpressionEntity(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,29 @@ import com.google.android.ground.persistence.local.room.dao.TaskDao
import com.google.android.ground.persistence.local.room.dao.TileSourceDao
import com.google.android.ground.persistence.local.room.dao.insertOrUpdate
import com.google.android.ground.persistence.local.stores.LocalSurveyStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

/** Manages access to [Survey] objects persisted in local storage. */
@Singleton
class RoomSurveyStore @Inject internal constructor() : LocalSurveyStore {
@Inject
lateinit var optionDao: OptionDao
@Inject lateinit var optionDao: OptionDao

@Inject
lateinit var multipleChoiceDao: MultipleChoiceDao
@Inject lateinit var multipleChoiceDao: MultipleChoiceDao

@Inject
lateinit var taskDao: TaskDao
@Inject lateinit var taskDao: TaskDao

@Inject
lateinit var jobDao: JobDao
@Inject lateinit var jobDao: JobDao

@Inject
lateinit var surveyDao: SurveyDao
@Inject lateinit var surveyDao: SurveyDao

@Inject
lateinit var tileSourceDao: TileSourceDao
@Inject lateinit var tileSourceDao: TileSourceDao

@Inject
lateinit var conditionDao: ConditionDao
@Inject lateinit var conditionDao: ConditionDao

@Inject
lateinit var expressionDao: ExpressionDao
@Inject lateinit var expressionDao: ExpressionDao

override val surveys: Flow<List<Survey>>
get() = surveyDao.getAll().map { surveyEntities -> surveyEntities.map { it.toModelObject() } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ internal object ConditionConverter {
"MATCH_ANY" -> Condition.MatchType.MATCH_ANY
"MATCH_ALL" -> Condition.MatchType.MATCH_ALL
"MATCH_ONE" -> Condition.MatchType.MATCH_ONE
else -> Condition.MatchType.UNKNOWN
else -> {
Timber.v("Unknown MatchType received: $typeStr")
Condition.MatchType.UNKNOWN
}
}.exhaustive

private fun toExpressions(expressions: List<ExpressionNestedObject>): List<Expression> =
Expand Down Expand Up @@ -72,6 +75,9 @@ internal object ConditionConverter {
"ANY_OF_SELECTED" -> Expression.ExpressionType.ANY_OF_SELECTED
"ALL_OF_SELECTED" -> Expression.ExpressionType.ALL_OF_SELECTED
"ONE_OF_SELECTED" -> Expression.ExpressionType.ONE_OF_SELECTED
else -> Expression.ExpressionType.UNKNOWN
else -> {
Timber.v("Unknown ExpressionType received: $typeStr")
Expression.ExpressionType.UNKNOWN
}
}.exhaustive
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,15 @@ import com.google.android.ground.ui.common.AbstractFragment
import com.google.android.ground.ui.common.BackPressListener
import com.google.android.ground.ui.common.Navigator
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlinx.coroutines.launch

/** Fragment allowing the user to collect data to complete a task. */
@AndroidEntryPoint
class DataCollectionFragment : AbstractFragment(), BackPressListener {
@Inject
lateinit var navigator: Navigator
@Inject lateinit var navigator: Navigator

@Inject
lateinit var viewPagerAdapterFactory: DataCollectionViewPagerAdapterFactory
@Inject lateinit var viewPagerAdapterFactory: DataCollectionViewPagerAdapterFactory

private val viewModel: DataCollectionViewModel by hiltNavGraphViewModels(R.id.data_collection)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ import com.google.android.ground.ui.datacollection.tasks.text.TextTaskViewModel
import com.google.android.ground.ui.datacollection.tasks.time.TimeTaskViewModel
import com.google.android.ground.ui.home.HomeScreenFragmentDirections
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import javax.inject.Provider
import kotlin.collections.component1
import kotlin.collections.component2
import kotlin.collections.set
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -57,11 +62,6 @@ import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Provider
import kotlin.collections.component1
import kotlin.collections.component2
import kotlin.collections.set

/** View model for the Data Collection fragment. */
@HiltViewModel
Expand Down Expand Up @@ -100,12 +100,12 @@ internal constructor(
MutableStateFlow(job.name ?: "").stateIn(viewModelScope, SharingStarted.Lazily, "")
val loiName: StateFlow<String> =
(if (loiId == null) flowOf("")
else
flow {
val loi = locationOfInterestRepository.getOfflineLoi(surveyId, loiId)
val label = locationOfInterestHelper.getLabel(loi)
emit(label)
})
else
flow {
val loi = locationOfInterestRepository.getOfflineLoi(surveyId, loiId)
val label = locationOfInterestHelper.getLabel(loi)
emit(label)
})
.stateIn(viewModelScope, SharingStarted.Lazily, "")

private val taskViewModels: MutableStateFlow<MutableList<AbstractTaskViewModel>> =
Expand Down Expand Up @@ -219,10 +219,10 @@ internal constructor(
private fun getTaskSequence(startId: String? = null, preceding: Boolean = false): Sequence<Task> {
val startIndex = tasks.indexOf(tasks.first { it.id == (startId ?: tasks[0].id) })
return if (preceding) {
tasks.subList(0, startIndex + 1).reversed()
} else {
tasks.subList(startIndex, tasks.size)
}
tasks.subList(0, startIndex + 1).reversed()
} else {
tasks.subList(startIndex, tasks.size)
}
.let { tasks ->
tasks.asSequence().filter { it.condition == null || evaluateCondition(it.condition) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ import com.google.android.ground.ui.datacollection.components.ButtonAction
import com.google.android.ground.ui.datacollection.components.TaskButton
import com.google.android.ground.ui.datacollection.components.TaskButtonFactory
import com.google.android.ground.ui.datacollection.components.TaskView
import kotlinx.coroutines.launch
import org.jetbrains.annotations.TestOnly
import java.util.EnumMap
import kotlin.properties.Delegates
import kotlinx.coroutines.launch
import org.jetbrains.annotations.TestOnly

abstract class AbstractTaskFragment<T : AbstractTaskViewModel> : AbstractFragment() {

protected val dataCollectionViewModel: DataCollectionViewModel by
hiltNavGraphViewModels(R.id.data_collection)
hiltNavGraphViewModels(R.id.data_collection)

private val buttons: EnumMap<ButtonAction, TaskButton> = EnumMap(ButtonAction::class.java)
private val buttonsIndex: MutableMap<Int, ButtonAction> = mutableMapOf()
Expand Down Expand Up @@ -178,11 +178,9 @@ abstract class AbstractTaskFragment<T : AbstractTaskViewModel> : AbstractFragmen
private fun ButtonAction.shouldReplaceWithDoneButton() =
this == ButtonAction.NEXT && dataCollectionViewModel.isLastPosition(position)

@TestOnly
fun getButtons() = buttons
@TestOnly fun getButtons() = buttons

@TestOnly
fun getButtonsIndex() = buttonsIndex
@TestOnly fun getButtonsIndex() = buttonsIndex

companion object {
/** Key used to store the position of the task in the Job's sorted tasklist. */
Expand Down
Loading

0 comments on commit e9e07d3

Please sign in to comment.