Skip to content

Commit

Permalink
feat(MD-3363): total rows cound ios
Browse files Browse the repository at this point in the history
  • Loading branch information
vcellu committed Jul 29, 2022
1 parent d0455a8 commit fa0bbbd
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 4 deletions.
26 changes: 24 additions & 2 deletions ios/ContainerView.swift
Expand Up @@ -23,9 +23,11 @@ class ContainerView: UIView {
weak var rootView: UIView?
weak var overlayView: OverlayView?
weak var footerView: FooterView?
weak var totalCellsView: TotalCellsView?

@objc var onEndReached: RCTDirectEventBlock?
@objc var onColumnsResized: RCTDirectEventBlock?
@objc var onVerticalScrollEnded: RCTDirectEventBlock?
@objc var onHeaderPressed: RCTDirectEventBlock?
@objc var onDoubleTap: RCTDirectEventBlock?
@objc var containerWidth: NSNumber?
Expand Down Expand Up @@ -166,6 +168,7 @@ class ContainerView: UIView {
createFooterView()
createDataCollectionView()
createGrabbers()
createTotalCellsView()
}

func calculateDefaultColWidth() {
Expand Down Expand Up @@ -242,7 +245,7 @@ class ContainerView: UIView {
fileprivate func createFooterView() {
if let totals = totals {
guard let height = tableTheme?.headerHeight else { return }
let frame = CGRect(x: 0, y: self.frame.height - CGFloat(height), width: headerView?.frame.width ?? self.frame.width, height: CGFloat(height))
let frame = CGRect(x: 0, y: self.frame.height - CGFloat(height * 2), width: headerView?.frame.width ?? self.frame.width, height: CGFloat(height))
let footerView = FooterView(frame: frame, withTotals: totals, dataColumns: dataColumns!, theme: tableTheme!, cellStyle: cellStyle!)
footerView.backgroundColor = ColorParser().fromCSS(cssString: tableTheme?.headerBackgroundColor ?? "white" )
rootView?.addSubview(footerView)
Expand All @@ -255,7 +258,7 @@ class ContainerView: UIView {
if collectionView == nil {
let width = Int(headerView?.frame.width ?? frame.width)
let height = tableTheme?.headerHeight ?? 54
var totalHeight = self.frame.height - CGFloat(height)
var totalHeight = self.frame.height - CGFloat(height * 2) // 2 is header + totals
if footerView != nil {
totalHeight -= CGFloat(height)
}
Expand All @@ -271,6 +274,24 @@ class ContainerView: UIView {
rootView!.addSubview(dataCollectionView)
}
}

fileprivate func createTotalCellsView() {
if(totalCellsView == nil) {
guard let height = tableTheme?.headerHeight else { return }
let frame = CGRect(x: 0, y: self.frame.height - CGFloat(height), width: self.frame.width, height: CGFloat(height))
let view = TotalCellsView(frame: frame)
view.backgroundColor = .white
view.bounds = frame.insetBy(dx: 8, dy: 8)
view.createTextView()
addSubview(view)
self.totalCellsView = view
collectionView?.totalCellsView = view
if let dataSize = dataSize {
view.totalRows = dataSize.qcy ?? 0
}
}

}

fileprivate func createGrabbers() {
if needsGrabbers {
Expand All @@ -289,6 +310,7 @@ class ContainerView: UIView {
grabber.overlayView = self.overlayView
grabber.footerView = self.footerView
grabber.scrollView = self.scrollView
grabber.trim = CGFloat(tableTheme.headerHeight ?? 54)
overlayView!.addSubview(grabber)
startX += col.width!
colIdx += 1
Expand Down
27 changes: 26 additions & 1 deletion ios/DataCollectionView.swift
Expand Up @@ -7,7 +7,7 @@

import Foundation
import UIKit
class DataCollectionView: UIView, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
class DataCollectionView: UIView, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate {
enum DataCollectionViewError: Error {
case noCellForIdentifier
}
Expand All @@ -24,6 +24,7 @@ class DataCollectionView: UIView, UICollectionViewDataSource, UICollectionViewDe
let reuseIdentifier = "CellIdentifier"
var cellColor = UIColor.black
var cellStyle = CellContentStyle()
weak var totalCellsView: TotalCellsView?

init(frame: CGRect, withRows rows: [DataRow], andColumns cols: [DataColumn], theme: TableTheme, selectionsEngine: SelectionsEngine, cellStyle: CellContentStyle) {
super.init(frame: frame)
Expand Down Expand Up @@ -103,6 +104,7 @@ class DataCollectionView: UIView, UICollectionViewDataSource, UICollectionViewDe
uiCollectionView.register(DataCellView.self, forCellWithReuseIdentifier: reuseIdentifier)
uiCollectionView.delegate = self
uiCollectionView.dataSource = self
// uiCollectionView.sco
uiCollectionView.indicatorStyle = .black
uiCollectionView.backgroundColor = .white
childCollectionView = uiCollectionView
Expand Down Expand Up @@ -175,4 +177,27 @@ class DataCollectionView: UIView, UICollectionViewDataSource, UICollectionViewDe
}
}
}

public func scrollViewDidScroll(_ scrollView: UIScrollView) {
signalVisibleRows()
}

public func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
signalVisibleRows();
}

public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
signalVisibleRows();
}

public func signalVisibleRows() {
if let childCollectionView = childCollectionView {
let arrayOfVisibleItems = childCollectionView.indexPathsForVisibleItems.sorted()
let firstItem = arrayOfVisibleItems.first;
let lastItem = arrayOfVisibleItems.last;
if let totalCellsView = totalCellsView, let first = firstItem, let last = lastItem {
totalCellsView.updateTotals(first: first, last: last)
}
}
}
}
3 changes: 2 additions & 1 deletion ios/GrabberView.swift
Expand Up @@ -19,6 +19,7 @@ class GrabberView: UIView {
weak var footerView: FooterView?
weak var scrollView: UIScrollView?
var pressed = false
var trim:CGFloat = 0;

var linePath = UIBezierPath()
var colIdx = 0
Expand Down Expand Up @@ -156,7 +157,7 @@ class GrabberView: UIView {
super.draw(rect)
let x = rect.origin.x + rect.width / 2
linePath.move(to: CGPoint(x: x, y: 0))
linePath.addLine(to: CGPoint(x: x, y: rect.height))
linePath.addLine(to: CGPoint(x: x, y: rect.height - trim))
linePath.lineWidth = 1

let color = pressed ? .black : borderColor
Expand Down
46 changes: 46 additions & 0 deletions ios/TotalCellsView.swift
@@ -0,0 +1,46 @@
//
// TotalCellsView.swift
// react-native-simple-grid
//
// Created by Vittorio Cellucci on 2022-07-29.
//

import Foundation
class TotalCellsView : UIView {
weak var textView: UITextView?
var totalRows = 0
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = .lightGray

}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func addBorder() {
let topBorder = UIView(frame: CGRect(x: self.bounds.origin.x, y: self.bounds.origin.y, width: self.bounds.width, height: 1))
topBorder.backgroundColor = .lightGray
addSubview(topBorder)

let bottomBorder = UIView(frame: CGRect(x: self.bounds.origin.x, y: self.bounds.origin.y + self.bounds.height, width: self.bounds.width, height: 1))
bottomBorder.backgroundColor = .lightGray
addSubview(bottomBorder)
}

func createTextView() {
let view = UITextView(frame: self.bounds)
view.text = "NA"
view.textAlignment = .right
addSubview(view)
self.textView = view;
}
func updateTotals(first:IndexPath, last: IndexPath) {
if let textView = textView {
if let f = first.last, let l = last.last {
textView.text = " \(f) - \(l) of \(totalRows)"
}
}
}
}

0 comments on commit fa0bbbd

Please sign in to comment.