Skip to content

Instantly share code, notes, and snippets.

View michzio's full-sized avatar

Michał Ziobro michzio

  • Cracow
View GitHub Profile
extension CollectionView {
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIViewController(context: Context) -> CollectionViewController<Section, Item> {
print("CollectionView.make()")
let controller = CollectionViewController<Section, Item>()
controller.layout = layout
extension CollectionView {
private func snapshotForCurrentState() -> NSDiffableDataSourceSnapshot<Section, Item> {
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections(sections)
for section in sections {
snapshot.appendItems(items[section]!, toSection: section)
}
extension CollectionView {
class Coordinator: NSObject, UICollectionViewDelegate {
// MARK: - Properties
let parent: CollectionView
var dataSource: UICollectionViewDiffableDataSource<Section, Item>?
// MARK: - Init
init(_ parent: CollectionView) {
struct StaticView: View {
@State var value1: String = ""
@State var value2: String = ""
@ObservedObject var avoider = KeyboardAvoider()
var body: some View {
KeyboardAvoiding(with: avoider) {
struct ScrollableView: View {
@State var value1: String = ""
@State var value2: String = ""
@ObservedObject var avoider = KeyboardAvoider()
var body: some View {
NavigationView {
struct KeyboardAvoiding<Content>: View where Content: View{
@ObservedObject private var avoider : KeyboardAvoider
let content: () -> Content
let offset: CGFloat
init(with avoider: KeyboardAvoider, offset: CGFloat = 0, @ViewBuilder content: @escaping () -> Content) {
self.content = content
self.avoider = avoider
extension View {
func registerKeyboardAvoider(_ avoider: KeyboardAvoider) -> some View {
self.onPreferenceChange(KeyboardAvoiderPreferenceKey.self) { prefs in
prefs.forEach { pref in
print("Avoider Rect: \(pref.rect)")
avoider.rects[pref.tag] = pref.rect
}
extension View {
func avoidKeyboard(tag: Int) -> some View {
self.modifier(KeyboardAvoiderPreferenceReader(tag: tag))
}
}
struct KeyboardAvoiderPreferenceReader: ViewModifier {
let tag: Int
func body(content: Content) -> some View {
content
.background(
GeometryReader { geometry in
Rectangle()
struct KeyboardAvoiderPreference: Equatable {
let tag: Int
let rect: CGRect
static func == (lhs: KeyboardAvoiderPreference, rhs: KeyboardAvoiderPreference) -> Bool {
print("y: \(lhs.rect.minY) vs \(rhs.rect.minY)")
return lhs.tag == rhs.tag && (lhs.rect.minY == rhs.rect.minY)
}
}