@@ -19,7 +19,6 @@ import com.elyeproj.loaderviewlibrary.LoaderImageView
1919import com.nextcloud.android.common.ui.theme.utils.ColorRole
2020import com.nextcloud.utils.OCFileUtils
2121import com.nextcloud.utils.extensions.makeRounded
22- import com.nextcloud.utils.extensions.mediaSize
2322import com.nextcloud.utils.extensions.setVisibleIf
2423import com.owncloud.android.R
2524import com.owncloud.android.databinding.GalleryRowBinding
@@ -68,7 +67,7 @@ class GalleryRowHolder(
6867 // Only rebuild if file count changed
6968 if (lastFileCount != requiredCount) {
7069 binding.rowLayout.removeAllViews()
71- for (file in row.files) {
70+ row.files.forEach { file ->
7271 binding.rowLayout.addView(getRowLayout(file))
7372 }
7473 lastFileCount = requiredCount
@@ -81,11 +80,11 @@ class GalleryRowHolder(
8180 }
8281 }
8382
84- fun updateRowVisuals () {
85- bind(currentRow)
86- }
83+ fun updateRowVisuals () = bind(currentRow)
8784
8885 private fun getRowLayout (file : OCFile ): FrameLayout {
86+ val (width, height) = OCFileUtils .getImageSize(file, defaultThumbnailSize)
87+
8988 val checkbox = ImageView (context).apply {
9089 visibility = View .GONE
9190 layoutParams = FrameLayout .LayoutParams (
@@ -98,16 +97,13 @@ class GalleryRowHolder(
9897 }
9998 }
10099
101- val mediaSize = file.mediaSize(defaultThumbnailSize)
102- val (width, height) = mediaSize
103-
104100 val shimmer = LoaderImageView (context).apply {
105101 setImageResource(R .drawable.background)
106102 resetLoader()
107103 layoutParams = FrameLayout .LayoutParams (width, height)
108104 }
109105
110- val drawable = OCFileUtils .getMediaPlaceholder(file, mediaSize )
106+ val drawable = OCFileUtils .getMediaPlaceholder(file, width to height )
111107 val rowCellImageView = ImageView (context).apply {
112108 setImageDrawable(drawable)
113109 adjustViewBounds = true
@@ -122,63 +118,60 @@ class GalleryRowHolder(
122118 }
123119 }
124120
121+ private fun getDimensions (row : GalleryRow ): List <Pair <Int , Int >> {
122+ val screenWidthPx = context.resources.displayMetrics.widthPixels.toFloat()
123+ val marginPx = smallMargin.toFloat()
124+ val totalMargins = marginPx * (row.files.size - 1 )
125+ val availableWidth = screenWidthPx - totalMargins
126+
127+ val aspectRatios = row.files.map { file ->
128+ val (w, h) = OCFileUtils .getImageSize(file, defaultThumbnailSize)
129+ if (h > 0 ) w.toFloat() / h else 1.0f
130+ }
131+
132+ val sumAspectRatios = aspectRatios.sum()
133+
134+ // calculate row height based on aspect ratios
135+ val rowHeightFloat = if (sumAspectRatios > 0 ) availableWidth / sumAspectRatios else defaultThumbnailSize
136+ val finalHeight = rowHeightFloat.toInt()
137+
138+ // for each aspect ratio calculate widths
139+ val finalWidths = aspectRatios.map { ratio -> (rowHeightFloat * ratio).toInt() }.toMutableList()
140+ val usedWidth = finalWidths.sum()
141+
142+ // based on screen width get remaining pixels
143+ val remainingPixels = (availableWidth - usedWidth).toInt()
144+
145+ // add to remaining pixels to last image
146+ if (remainingPixels > 0 && finalWidths.isNotEmpty()) {
147+ val lastIndex = finalWidths.lastIndex
148+ finalWidths[lastIndex] = finalWidths[lastIndex] + remainingPixels
149+ }
150+
151+ return finalWidths.map { w -> w to finalHeight }
152+ }
153+
125154 private fun adjustFile (index : Int , file : OCFile , dims : Pair <Int , Int >, row : GalleryRow ) {
126155 val (width, height) = dims
127-
128156 val frameLayout = binding.rowLayout[index] as FrameLayout
129157 val shimmer = frameLayout[0 ] as LoaderImageView
130158 val thumbnail = frameLayout[1 ] as ImageView
131159 val checkbox = frameLayout[2 ] as ImageView
132160
133161 val isChecked = ocFileListDelegate.isCheckedFile(file)
134-
135162 adjustRowCell(thumbnail, isChecked)
136163 adjustCheckBox(checkbox, isChecked)
137164
138165 ocFileListDelegate.bindGalleryRow(shimmer, thumbnail, file, this , dims)
139166
140167 val endMargin = if (index < row.files.size - 1 ) smallMargin else zero
141-
142168 thumbnail.layoutParams = FrameLayout .LayoutParams (width, height).apply {
143169 setMargins(0 , 0 , endMargin, smallMargin)
144170 }
145171 shimmer.layoutParams = FrameLayout .LayoutParams (width, height)
146-
147172 frameLayout.requestLayout()
148173 }
149174
150- private fun getDimensions (row : GalleryRow ): List <Pair <Int , Int >> {
151- val screenWidthPx = context.resources.displayMetrics.widthPixels.toFloat()
152- val marginPx = smallMargin.toFloat()
153-
154- val aspectRatios = row.files.map { file ->
155- val (w, h) = OCFileUtils .getImageSize(file, defaultThumbnailSize)
156- if (w > 0 && h > 0 ) w.toFloat() / h.toFloat() else 1f
157- }
158-
159- // Start with max height based on largest image
160- val targetHeight = row.getMaxHeight().coerceAtLeast(1f )
161-
162- val unscaledWidths = aspectRatios.map { ar -> targetHeight * ar }
163-
164- // margins
165- val totalUnscaled = unscaledWidths.sum()
166- val totalMargins = marginPx * (row.files.size - 1 )
167- val available = screenWidthPx - totalMargins
168-
169- // calculate shrink ratio
170- val shrink = available / totalUnscaled
171-
172- // prepare scaled dimensions
173- val dims = unscaledWidths.map { w ->
174- val finalW = (w * shrink).toInt()
175- val finalH = (targetHeight * shrink).toInt()
176- finalW to finalH
177- }
178-
179- return dims
180- }
181-
182175 @Suppress(" MagicNumber" )
183176 private fun adjustRowCell (imageView : ImageView , isChecked : Boolean ) {
184177 val scale = if (isChecked) 0.8f else 1.0f
0 commit comments