cashapp / redwood

Multiplatform reactive UI for Android, iOS, and web using Kotlin and Jetpack Compose
https://cashapp.github.io/redwood/0.x/docs/
Apache License 2.0
1.62k stars 70 forks source link

size rows by their content. #1055

Closed github-actions[bot] closed 1 year ago

github-actions[bot] commented 1 year ago

https://github.com/cashapp/redwood/blob/424e2cafa59dad7edd9f1383ca19a69fe647f796/redwood-treehouse-lazylayout-uiview/src/commonMain/kotlin/app/cash/redwood/treehouse/lazylayout/uiview/UIViewLazyList.kt#L99


 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
@file:Suppress(
  "PARAMETER_NAME_CHANGED_ON_OVERRIDE",
  "RETURN_TYPE_MISMATCH_ON_OVERRIDE",
)

package app.cash.redwood.treehouse.lazylayout.uiview

import app.cash.redwood.LayoutModifier
import app.cash.redwood.treehouse.lazylayout.widget.LazyList
import app.cash.redwood.widget.Widget
import kotlinx.cinterop.ObjCClass
import platform.Foundation.NSIndexPath
import platform.Foundation.classForCoder
import platform.UIKit.UITableView
import platform.UIKit.UITableViewCell
import platform.UIKit.UITableViewCellStyle
import platform.UIKit.UITableViewCellStyle.UITableViewCellStyleDefault
import platform.UIKit.UITableViewDataSourceProtocol
import platform.UIKit.UITableViewDelegateProtocol
import platform.UIKit.UIView
import platform.UIKit.item
import platform.darwin.NSInteger
import platform.darwin.NSObject

private const val reuseIdentifier = "cell"

internal class UIViewLazyList : LazyList<UIView> {

  private val itemsList = mutableListOf<Widget<UIView>>()

  override val items: Widget.Children<UIView> = object : Widget.Children<UIView> {
    override fun insert(index: Int, widget: Widget<UIView>) {
      itemsList.add(index, widget)
      tableView.reloadData()
    }

    override fun move(fromIndex: Int, toIndex: Int, count: Int) {
      itemsList.move(fromIndex, toIndex, count)
      tableView.reloadData()
    }

    override fun remove(index: Int, count: Int) {
      itemsList.remove(index, count)
      tableView.reloadData()
    }

    override fun onLayoutModifierUpdated() {
    }
  }

  private val tableViewDelegate: UITableViewDelegateProtocol =
    object : NSObject(), UITableViewDelegateProtocol {
      override fun tableView(
        tableView: UITableView,
        willDisplayCell: UITableViewCell,
        forRowAtIndexPath: NSIndexPath,
      ) {
        val content = itemsList[forRowAtIndexPath.item.toInt()]
        (willDisplayCell as Cell).setView(content.value)
      }
    }

  private val tableViewDataSource: UITableViewDataSourceProtocol =
    object : NSObject(), UITableViewDataSourceProtocol {
      override fun numberOfSectionsInTableView(
        tableView: UITableView,
      ): NSInteger = 1L

      override fun tableView(
        tableView: UITableView,
        numberOfRowsInSection: NSInteger,
      ): NSInteger = itemsList.size.toLong()

      override fun tableView(
        tableView: UITableView,
        cellForRowAtIndexPath: NSIndexPath,
      ): UITableViewCell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as Cell
    }

  private val tableView = UITableView()
    .apply {
      dataSource = tableViewDataSource
      delegate = tableViewDelegate
      rowHeight = 64.0 // TODO: size rows by their content.
      prefetchingEnabled = false
      registerClass(
        Cell(UITableViewCellStyleDefault, reuseIdentifier).classForCoder() as ObjCClass?,
        forCellReuseIdentifier = reuseIdentifier,
      )
    }

  private lateinit var onPositionDisplayed: (Int) -> Unit

  override fun isVertical(isVertical: Boolean) {
    if (!isVertical) {
      // TODO UITableView only supports vertical scrolling. Switch to UICollectionView.
github-actions[bot] commented 1 year ago

Closed in a4bbf1c2c8e718becede7d49ab06acc9813b8f80