Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for UICollectionView introspection #169

Merged
merged 6 commits into from
Feb 10, 2023

Conversation

chrismaddern
Copy link
Contributor

@chrismaddern chrismaddern commented Sep 28, 2022

iOS 16 has migrated List to use UICollectionView under the hood (at least in some cases.)

This is a very simple addition to add support for UICollectionView introspection.

@chrismaddern chrismaddern changed the title Add support for UICollectionView Add support for UICollectionView introspection Sep 28, 2022
@davdroman
Copy link
Collaborator

davdroman commented Feb 10, 2023

Adding some tests. Will merge shortly.

@chrismaddern
Copy link
Contributor Author

oh, amazing! 🥳

glad to be of help... and thrilled to be back on main

@X901
Copy link

X901 commented Feb 15, 2023

if my app support ios15 and ios16
will it be use UITableView for ios15 and UICollectionView for ios16 ?
or I should add if condition ?

@davdroman
Copy link
Collaborator

davdroman commented Feb 15, 2023

@X901 yes, you will need to introspect them both separatedly.

For this particular case, I don't think you'll need to add an #if available condition, as it's almost always going to be one or the other. In the cases where you do need to distinguish between platforms, here's a quick workaround you can use for now:

// add this to your codebase
extension View {
	func availability<Transformed: View>(
		@ViewBuilder _ transform: (Self) -> Transformed
	) -> Transformed {
		transform(self)
	}
}

[...]

struct YourView {
	var body: some View {
		List {
			[...]
		}
		.availability {
			if #available(iOS 16, *) {
				$0.introspectCollectionView { collectionView in
					[...]
				}
			} else {
				$0.introspectTableView { tableView in
					[...]
				}
			}
		}
	}
}

I have some ideas on how to improve the ergonomics of introspecting the same SwiftUI component in different OS versions, but I'm yet to explore them when I get some spare time.

@X901
Copy link

X901 commented Feb 15, 2023

@X901 yes, you will need to introspect them both separatedly.

For this particular case, I don't think you'll need to add an #if available condition, as it's almost always going to be one or the other. In the cases where you do need to distinguish between platforms, here's a quick workaround you can use for now:

// add this to your codebase
extension View {
	func availability<Transformed: View>(
		@ViewBuilder _ transform: (Self) -> Transformed
	) -> Transformed {
		transform(self)
	}
}

[...]

struct YourView {
	var body: some View {
		List {
			[...]
		}
		.availability {
			if #available(iOS 16, *) {
				$0.introspectCollectionView { collectionView in
					[...]
				}
			} else {
				$0.introspectTableView { tableView in
					[...]
				}
			}
		}
	}
}

I have some ideas on how to improve the ergonomics of introspecting the same SwiftUI component in different OS versions, but I'm yet to explore them when I get some spare time.

Thank you, you give me A great idea 👍

@fascinated
Copy link

thanks for sharing this snippet, really helped me get this working with Lists in 15 and 16. <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants