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

[AVFoundation] Add a few strongly typed enums #18022

Merged

Conversation

rolfbjarne
Copy link
Member

@rolfbjarne rolfbjarne commented Apr 11, 2023

Add strongly typed enums for:

  • AVAudioSessionCategory
  • AVAudioSessionMode

This makes it possible to delete a good chunk of manual binding code.

Also add a few extra convenience overloads of AVAudioSession.SetCategory.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

…essed by the generator.

This eliminates the need for a good chunk of manual code.

Also add a few extra convenience overloads of AVAudioSession.SetCategory.
@rolfbjarne rolfbjarne force-pushed the strong-enum-avaudiosessioncategory branch from 0a209ae to 1b79bce Compare April 12, 2023 06:12
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne rolfbjarne marked this pull request as ready for review April 12, 2023 13:44
@rolfbjarne rolfbjarne changed the title [AVFoundation] Make AVAudioSessionCategory a strongly typed enum processed by the generator. [AVFoundation] Add a few strongly typed enums Apr 12, 2023
Copy link
Contributor

@haritha-mohan haritha-mohan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rolfbjarne Just to make sure I'm following:

  • By implementing strongly typed enums vs. regular enums (like before), we eliminate the need for the switch case to map the enum item to the actual value because the underlying value of strongly typed enums is the value we set it to be (in this case the AVAudioSession category/mode) as opposed to being backed by a int value in a regular enum.
  • Another thing that caught my attention is that before the methods for SetActive/SetCategory were implemented but now they are not? My current understanding is that again since before we were using a regular enum, there were not appropriate methods to be called upon the regular enum values directly. But now with the strongly typed enum present, we get the appropriate data type immediately so there is no need for a buffer between the enum and calling the selector?

[Watch (5, 0), TV (12, 0), iOS (12, 0)]
[MacCatalyst (13, 1)]
[Field ("AVAudioSessionModeVoicePrompt")]
VoiceePrompt,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
VoiceePrompt,
VoicePrompt,

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@haritha-mohan good catch, thanks! And fixed :)

Copy link
Contributor

@tj-devel709 tj-devel709 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After Haritha's suggestion

@rolfbjarne
Copy link
Member Author

@rolfbjarne Just to make sure I'm following:

  • By implementing strongly typed enums vs. regular enums (like before), we eliminate the need for the switch case to map the enum item to the actual value because the underlying value of strongly typed enums is the value we set it to be (in this case the AVAudioSession category/mode) as opposed to being backed by a int value in a regular enum.

Not quite, the generator will generate an equivalent switch case for strong enums, with extension methods to convert between NSString and the enum.

You can see this in the API diff: https://vsdrop.corp.microsoft.com/file/v1/xamarin-macios/detected-changes/20230412.6/7617061-1/;/previous-api-comparison/diff/dotnet/Microsoft.iOS.Ref/ref/net7.0/Microsoft.iOS.html

New Type AVFoundation.AVAudioSessionCategoryExtensions
public static class AVAudioSessionCategoryExtensions {
	// methods
	public static Foundation.NSString GetConstant (this AVAudioSessionCategory self);
	public static AVAudioSessionCategory GetValue (Foundation.NSString constant);
}

These are the extension methods to convert between the AVAudioSessionCategory enum and NSString.

  • Another thing that caught my attention is that before the methods for SetActive/SetCategory were implemented but now they are not? My current understanding is that again since before we were using a regular enum, there were not appropriate methods to be called upon the regular enum values directly. But now with the strongly typed enum present, we get the appropriate data type immediately so there is no need for a buffer between the enum and calling the selector?

Not quite either: the SetActive/SetCategory methods are now generated by the generator, in the changes for src/avfoundation.cs there are new SetActive/SetCategory members that match the old ones in src/AVFoundation/AVAudioSession.cs (and use the GetConstant extension method mentioned in the previous post to convert from the enum in the managed method signature to the NSString in the native method signature).

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ API diff for current PR / commit

Legacy Xamarin (No breaking changes)
.NET (No breaking changes)

✅ API diff vs stable

Legacy Xamarin (No breaking changes)
.NET (No breaking changes)
Legacy Xamarin (stable) vs .NET

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: d70f39c21f9682d8ebfd3abf461f1620cc46b1f4 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [PR Build] Tests on macOS M1 - Mac Ventura (13.0) passed 💻

All tests on macOS M1 - Mac Ventura (13.0) passed.

Pipeline on Agent
Hash: d70f39c21f9682d8ebfd3abf461f1620cc46b1f4 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build] Windows Integration Tests passed 💻

All Windows Integration Tests passed.

Pipeline on Agent
Hash: d70f39c21f9682d8ebfd3abf461f1620cc46b1f4 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [PR Build] Tests on macOS M1 - Mac Big Sur (11.5) passed 💻

All tests on macOS M1 - Mac Big Sur (11.5) passed.

Pipeline on Agent
Hash: d70f39c21f9682d8ebfd3abf461f1620cc46b1f4 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

📚 [PR Build] Artifacts 📚

Packages generated

View packages

Pipeline on Agent XAMBOT-1033.Ventura
Hash: d70f39c21f9682d8ebfd3abf461f1620cc46b1f4 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Collaborator

🚀 [CI Build] Test results 🚀

Test results

✅ All tests passed on VSTS: simulator tests.

🎉 All 225 tests passed 🎉

Tests counts

✅ bcl: All 69 tests passed. Html Report (VSDrops) Download
✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests: All 1 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 7 tests passed. Html Report (VSDrops) Download
✅ framework: All 8 tests passed. Html Report (VSDrops) Download
✅ generator: All 2 tests passed. Html Report (VSDrops) Download
✅ interdependent_binding_projects: All 7 tests passed. Html Report (VSDrops) Download
✅ install_source: All 1 tests passed. Html Report (VSDrops) Download
✅ introspection: All 8 tests passed. Html Report (VSDrops) Download
✅ linker: All 65 tests passed. Html Report (VSDrops) Download
✅ mac_binding_project: All 1 tests passed. Html Report (VSDrops) Download
✅ mmp: All 2 tests passed. Html Report (VSDrops) Download
✅ mononative: All 12 tests passed. Html Report (VSDrops) Download
✅ monotouch: All 25 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ mtouch: All 1 tests passed. Html Report (VSDrops) Download
✅ xammac: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 8 tests passed. Html Report (VSDrops) Download
✅ xtro: All 2 tests passed. Html Report (VSDrops) Download

Pipeline on Agent
Hash: d70f39c21f9682d8ebfd3abf461f1620cc46b1f4 [PR build]

@rolfbjarne rolfbjarne merged commit 3f49d0c into xamarin:main Apr 17, 2023
@rolfbjarne rolfbjarne deleted the strong-enum-avaudiosessioncategory branch April 17, 2023 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants