Skip to content

Commit

Permalink
Discovery UI
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Dec 29, 2023
1 parent 1e51994 commit b2259ed
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ + (MatterError * _Nonnull)MatterErrorFromChipError:(CHIP_ERROR)chipError

+ (NSError * _Nonnull)NSErrorFromChipError:(CHIP_ERROR)chipError
{
return chipError == CHIP_NO_ERROR ? nil : [NSError errorWithDomain:@"com.matter.casting" code:chipError.AsInteger() userInfo:@{ NSUnderlyingErrorKey : self }];
return chipError == CHIP_NO_ERROR ? nil : [NSError errorWithDomain:@"com.matter.casting" code:chipError.AsInteger() userInfo:@{ NSUnderlyingErrorKey : [NSString stringWithUTF8String:chipError.AsString()] }];
}

+ (NSError * _Nonnull)NSErrorFromMatterError:(MatterError * _Nonnull)matterError
{
return matterError == MATTER_NO_ERROR ? nil : [NSError errorWithDomain:@"com.matter.casting" code:matterError.code userInfo:@{ NSUnderlyingErrorKey : self }];
return matterError == MATTER_NO_ERROR ? nil : [NSError errorWithDomain:@"com.matter.casting" code:matterError.code userInfo:@{ NSUnderlyingErrorKey : matterError.message }];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,30 @@ struct MTRDiscoveryExampleView: View {

var body: some View {
VStack(alignment: .leading) {

Button("Start Discovery", action: viewModel.startDiscovery)
.frame(width: 200, height: 30, alignment: .center)
.frame(width: 350, height: 30, alignment: .center)
.border(Color.black, width: 1)
.background(Color.blue)
.foregroundColor(Color.white)
.padding()
.padding(1)

Button("Stop Discovery", action: viewModel.stopDiscovery)
.frame(width: 200, height: 30, alignment: .center)
.frame(width: 350, height: 30, alignment: .center)
.border(Color.black, width: 1)
.background(Color.blue)
.foregroundColor(Color.white)
.padding()
.padding(1)

Button("Clear Results", action: viewModel.clearResults)
.frame(width: 200, height: 30, alignment: .center)
.frame(width: 350, height: 30, alignment: .center)
.border(Color.black, width: 1)
.background(Color.blue)
.foregroundColor(Color.white)
.padding()
.padding(1)

if(viewModel.startDiscoveryRequestStatus == false)
{
Text("Start Discovery request failed. Check logs for details")
}
else if(viewModel.stopDiscoveryRequestStatus == false)
if(viewModel.discoveryHasError)
{
Text("Stop Discovery request failed. Check logs for details")
Text("Discovery request failed. Check logs for details")
}
else if(!viewModel.displayedCastingPlayers.isEmpty)
{
Expand All @@ -80,9 +75,6 @@ struct MTRDiscoveryExampleView: View {
}
.navigationTitle("Casting Player Discovery")
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .top)
/*.onAppear(perform: {
viewModel.startDiscovery()
})*/
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,40 @@ class MTRDiscoveryExampleViewModel: ObservableObject {

@Published var displayedCastingPlayers: [MTRCastingPlayer] = []

@Published var startDiscoveryRequestStatus: Bool?;
@Published var stopDiscoveryRequestStatus: Bool?;
@Published var discoveryHasError: Bool = false;

func startDiscovery() {
Log.info("startDiscovery() called")
clearResults()

// add observers
NotificationCenter.default.addObserver(self, selector: #selector(self.didAddDiscoveredCastingPlayers), name: NSNotification.Name(ADD_CASTING_PLAYER_NOTIFICATION_NAME), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.didRemoveDiscoveredCastingPlayers), name: NSNotification.Name(REMOVE_CASTING_PLAYER_NOTIFICATION_NAME), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.didUpdateDiscoveredCastingPlayers), name: NSNotification.Name(UPDATE_CASTING_PLAYER_NOTIFICATION_NAME), object: nil)

if let err:Error = MTRCastingPlayerDiscovery.sharedInstance().start(UInt32(kTargetPlayerDeviceType))
{
Log.error("MTRCastingPlayerDiscovery.start failed with \(err)")
self.discoveryHasError = true
}
self.startDiscoveryRequestStatus = true
self.discoveryHasError = false
}

func stopDiscovery() {
Log.info("stopDiscovery() called")
if let err:Error = MTRCastingPlayerDiscovery.sharedInstance().stop()
{
Log.error("MTRCastingPlayerDiscovery.stop failed with \(err)")
self.discoveryHasError = true
}
// remove observers
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(ADD_CASTING_PLAYER_NOTIFICATION_NAME), object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(REMOVE_CASTING_PLAYER_NOTIFICATION_NAME), object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(UPDATE_CASTING_PLAYER_NOTIFICATION_NAME), object: nil)
self.stopDiscoveryRequestStatus = true
else
{
// remove observers
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(ADD_CASTING_PLAYER_NOTIFICATION_NAME), object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(REMOVE_CASTING_PLAYER_NOTIFICATION_NAME), object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(UPDATE_CASTING_PLAYER_NOTIFICATION_NAME), object: nil)
}
self.discoveryHasError = false
}

func clearResults() {
Expand Down

0 comments on commit b2259ed

Please sign in to comment.