Skip to content

Commit

Permalink
Replace findViewById in comment dialog code
Browse files Browse the repository at this point in the history
  • Loading branch information
chrgernoe committed Jan 24, 2025
1 parent 4cc6c5c commit 069b19a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 46 deletions.
41 changes: 24 additions & 17 deletions app/src/main/java/com/yacgroup/yacguide/TableActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ abstract class TableActivity<ViewBindingType: ViewBinding> : BaseNavigationActiv
Comment(
text = it.text.orEmpty(),
properties = listOf(
CommentProperty(R.id.qualityLayout, R.string.relevance, RegionComment.QUALITY_MAP, it.qualityId)
CommentProperty(R.string.relevance, RegionComment.QUALITY_MAP, it.qualityId)
)
)
})
Expand All @@ -78,7 +78,7 @@ abstract class TableActivity<ViewBindingType: ViewBinding> : BaseNavigationActiv
Comment(
text = it.text.orEmpty(),
properties = listOf(
CommentProperty(R.id.qualityLayout, R.string.relevance, SectorComment.QUALITY_MAP, it.qualityId)
CommentProperty(R.string.relevance, SectorComment.QUALITY_MAP, it.qualityId)
)
)
})
Expand All @@ -89,7 +89,7 @@ abstract class TableActivity<ViewBindingType: ViewBinding> : BaseNavigationActiv
Comment(
text = it.text.orEmpty(),
properties = listOf(
CommentProperty(R.id.qualityLayout, R.string.nature, RockComment.RELEVANCE_MAP, it.qualityId)
CommentProperty(R.string.nature, RockComment.RELEVANCE_MAP, it.qualityId)
)
)
})
Expand All @@ -100,10 +100,10 @@ abstract class TableActivity<ViewBindingType: ViewBinding> : BaseNavigationActiv
Comment(
text = it.text.orEmpty(),
properties = listOf(
CommentProperty(R.id.qualityLayout, R.string.route_quality, RouteComment.QUALITY_MAP, it.qualityId),
CommentProperty(R.id.gradeLayout, R.string.grade, RouteComment.GRADE_MAP, it.gradeId),
CommentProperty(R.id.protectionLayout, R.string.protection, RouteComment.PROTECTION_MAP, it.securityId),
CommentProperty(R.id.dryingLayout, R.string.drying, RouteComment.DRYING_MAP, it.wetnessId)
CommentProperty(R.string.route_quality, RouteComment.QUALITY_MAP, it.qualityId),
CommentProperty(R.string.grade, RouteComment.GRADE_MAP, it.gradeId),
CommentProperty(R.string.protection, RouteComment.PROTECTION_MAP, it.securityId),
CommentProperty(R.string.drying, RouteComment.DRYING_MAP, it.wetnessId)
)
)
})
Expand All @@ -121,24 +121,31 @@ abstract class TableActivity<ViewBindingType: ViewBinding> : BaseNavigationActiv
val commentDialogBinding = CommentDialogBinding.inflate(dialog.layoutInflater)
dialog.setView(commentDialogBinding.root)

// FIXME: Why is this divider code necessary?
val dividerResource = TypedValue()
theme.resolveAttribute(android.R.attr.listDivider, dividerResource, true)
comments.forEach { comment ->
CommentBinding.inflate(dialog.layoutInflater, commentDialogBinding.commentLayout, false).let { view ->
CommentBinding.inflate(dialog.layoutInflater).let { view ->
view.commentDivider.setBackgroundResource(dividerResource.resourceId)
comment.properties.forEach { prop ->
_property2String(prop.qualityMap, prop.qualityId)?.let {
view.root.findViewById<ConstraintLayout>(prop.layoutResource).apply {
CommentPropertyBinding.inflate(dialog.layoutInflater).apply {
propertyNameTextView.setText(prop.nameResource)
propertyValueTextView.text = it
}
visibility = View.VISIBLE
comment.properties.forEachIndexed { viewIndex, prop ->
_property2String(prop.qualityMap, prop.qualityId)?.let { propValue ->
val propBinding = CommentPropertyBinding.inflate(dialog.layoutInflater).apply {
propertyNameTextView.setText(prop.nameResource)
propertyValueTextView.text = propValue
}
// The layout margins are not considered, if the view is add programmatically.
propBinding.root.layoutParams = ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.MATCH_PARENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
).apply {
bottomMargin = 10
}
// +1 because the divider is the first element in the linear view
view.root.addView(propBinding.root, viewIndex + 1)
}
}
view.commentTextView.text = comment.text
commentDialogBinding.commentLayout.addView(view.singleCommentLayout)
commentDialogBinding.commentLayout.addView(view.root)
}
}
dialog.show()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class Comment(
)

class CommentProperty(
val layoutResource: Int,
val nameResource: Int,
val qualityMap: Map<Int, String>,
val qualityId: Int
)
)
14 changes: 5 additions & 9 deletions app/src/main/res/layout/comment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,17 @@
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/singleCommentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">

<View android:id="@+id/commentDivider" android:layout_marginBottom="10dp" style="@style/ThinDivider"/>
<View
android:id="@+id/commentDivider"
android:layout_marginBottom="10dp"
style="@style/ThinDivider"/>

<include android:id="@+id/qualityLayout" layout="@layout/comment_property" android:visibility="gone"/>

<include android:id="@+id/gradeLayout" layout="@layout/comment_property" android:visibility="gone"/>

<include android:id="@+id/protectionLayout" layout="@layout/comment_property" android:visibility="gone"/>

<include android:id="@+id/dryingLayout" layout="@layout/comment_property" android:visibility="gone"/>
<!-- Add this position, the comment property layouts are added programmatically. -->

<TextView
android:id="@+id/commentTextView"
Expand Down
24 changes: 9 additions & 15 deletions app/src/main/res/layout/comment_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,20 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
-->

<LinearLayout
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
android:layout_gravity="center"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin">

<ScrollView
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin">
android:id="@+id/commentLayout"/>

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/commentLayout"/>

</ScrollView>

</LinearLayout>
</ScrollView>
5 changes: 2 additions & 3 deletions app/src/main/res/layout/comment_property.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp">
android:layout_height="wrap_content">

<TextView
android:id="@+id/propertyNameTextView"
Expand All @@ -38,4 +37,4 @@
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 069b19a

Please sign in to comment.