Use databinding for some widgets

This commit is contained in:
Luke Hubmayer-Werner 2024-08-27 16:25:17 +09:30
parent 9ae2b36a3c
commit 53e3bd6229
3 changed files with 12 additions and 10 deletions

View File

@ -34,6 +34,7 @@ android {
jvmTarget = "1.8" jvmTarget = "1.8"
} }
buildFeatures { buildFeatures {
dataBinding = true
viewBinding = true viewBinding = true
} }
} }

View File

@ -33,6 +33,7 @@ class GalleryFragment : Fragment() {
// val galleryViewModel = ViewModelProvider(this)[GalleryViewModel::class.java] // val galleryViewModel = ViewModelProvider(this)[GalleryViewModel::class.java]
_binding = FragmentGalleryBinding.inflate(inflater, container, false) _binding = FragmentGalleryBinding.inflate(inflater, container, false)
binding.viewModel = galleryViewModel
val root: View = binding.root val root: View = binding.root
val chipGroupPages = binding.chipGroupPages val chipGroupPages = binding.chipGroupPages
@ -45,19 +46,12 @@ class GalleryFragment : Fragment() {
pageZoomLevelChips[pages] = chip pageZoomLevelChips[pages] = chip
} }
pageZoomLevelChips[galleryViewModel.pagesPerLandscape.value]?.isChecked = true pageZoomLevelChips[galleryViewModel.pagesPerLandscape.value]?.isChecked = true
binding.chipAutoCrop.isChecked = galleryViewModel.renderAutoCrop.value!!
binding.chipUsePdfBox.isChecked = galleryViewModel.usePdfBox.value!!
galleryViewModel.textDisplays.observe(viewLifecycleOwner) { binding.textDisplays.text = it } galleryViewModel.textDisplays.observe(viewLifecycleOwner) { binding.textDisplays.text = it } // TODO: Investigate why the binding, even when set @=, in the XML is insufficient for live updates
galleryViewModel.usePdfBox.observe(viewLifecycleOwner) { binding.chipUsePdfBox.isChecked = it }
galleryViewModel.renderAutoCrop.observe(viewLifecycleOwner) { binding.chipAutoCrop.isChecked = it }
galleryViewModel.pagesPerLandscape.observe(viewLifecycleOwner) { galleryViewModel.pagesPerLandscape.observe(viewLifecycleOwner) {
pageZoomLevelChips[it]?.isChecked = true pageZoomLevelChips[it]?.isChecked = true
} }
binding.chipAutoCrop.setOnCheckedChangeListener { _, checked -> galleryViewModel.renderAutoCrop.value = checked }
binding.chipUsePdfBox.setOnCheckedChangeListener { _, checked -> galleryViewModel.usePdfBox.value = checked }
galleryViewModel.pageThumbnails.observe(viewLifecycleOwner, ::populateThumbnails) galleryViewModel.pageThumbnails.observe(viewLifecycleOwner, ::populateThumbnails)
val container = binding.thumbnailsLayout val container = binding.thumbnailsLayout

View File

@ -1,4 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable name="viewModel" type="com.lhw.pdf.ui.gallery.GalleryViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
@ -45,6 +49,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="8dp" android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" android:layout_marginEnd="8dp"
android:text="@{viewModel.textDisplays}"
android:textAlignment="center" android:textAlignment="center"
android:textSize="20sp" /> android:textSize="20sp" />
@ -66,7 +71,7 @@
<com.google.android.material.chip.Chip <com.google.android.material.chip.Chip
android:id="@+id/chip_auto_crop" android:id="@+id/chip_auto_crop"
android:checkable="true" android:checkable="true"
android:checked="true" android:checked="@={viewModel.renderAutoCrop}"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Auto Crop" /> android:text="Auto Crop" />
@ -74,10 +79,12 @@
<com.google.android.material.chip.Chip <com.google.android.material.chip.Chip
android:id="@+id/chip_use_pdf_box" android:id="@+id/chip_use_pdf_box"
android:checkable="true" android:checkable="true"
android:checked="@={viewModel.usePdfBox}"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Use PdfBox renderer (slower)" /> android:text="Use PdfBox renderer (slower)" />
</com.google.android.material.chip.ChipGroup> </com.google.android.material.chip.ChipGroup>
</LinearLayout> </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
</layout>