Skip to content

Commit

Permalink
Notifications: Camera Aspect Ratio (#650)
Browse files Browse the repository at this point in the history
- Adjusts the constraints on the preview image view while it vends so it isn't stretched. Fixes #513 / #167.
- Adds a sample .apns which can be used to trigger a Camera notification more easily.

Looking through the server code, it looks like we can also grab an HLS stream, which is what the frontend is using for previewing the camera. Refs #596. Created a ticket remember this as #649.
  • Loading branch information
zacwest committed Jun 15, 2020
1 parent 767724f commit c1ed1e3
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
10 changes: 10 additions & 0 deletions HomeAssistant.xcodeproj/project.pbxproj
Expand Up @@ -715,6 +715,7 @@
/* Begin PBXFileReference section */
09D3A786745B7C6D0D364BF0 /* Pods-Shared-watchOS.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Shared-watchOS.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Shared-watchOS/Pods-Shared-watchOS.debug.xcconfig"; sourceTree = "<group>"; };
0CE51F0910A76C0B0D8B795E /* Pods-Shared-watchOS-metadata.plist */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.plist.xml; name = "Pods-Shared-watchOS-metadata.plist"; path = "Pods/Pods-Shared-watchOS-metadata.plist"; sourceTree = "<group>"; };
1100D52024974D6700B1073C /* camera_notification.apns */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = camera_notification.apns; sourceTree = "<group>"; };
1100D51C2496AECE00B1073C /* PermissionStatusRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PermissionStatusRow.swift; sourceTree = "<group>"; };
113D29DD24946ED90014067C /* CLLocationManager+OneShotLocation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CLLocationManager+OneShotLocation.swift"; sourceTree = "<group>"; };
113D29E024946EE50014067C /* CLLocationManager+OneShotLocationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CLLocationManager+OneShotLocationTests.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1444,6 +1445,14 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
1100D5222497578800B1073C /* TestNotifications */ = {
isa = PBXGroup;
children = (
1100D52024974D6700B1073C /* camera_notification.apns */,
);
path = TestNotifications;
sourceTree = "<group>";
};
11F3D74F2495433800C05BBA /* Sensors */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1742,6 +1751,7 @@
B627CB0C1D83C87B0057173E /* NotificationContentExtension */ = {
isa = PBXGroup;
children = (
1100D5222497578800B1073C /* TestNotifications */,
B678DB351EA9999C0045312F /* MainInterface.storyboard */,
B627CB121D83C87B0057173E /* Info.plist */,
B689EBCD1D8B91CE0000AECE /* NotificationContentExtension.entitlements */,
Expand Down
Expand Up @@ -2,7 +2,7 @@
<Scheme
LastUpgradeVersion = "1120"
wasCreatedForAppExtension = "YES"
version = "2.0">
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
Expand Down Expand Up @@ -59,13 +59,12 @@
selectedDebuggerIdentifier = ""
selectedLauncherIdentifier = "Xcode.IDEFoundation.Launcher.PosixSpawn"
launchStyle = "0"
askForAppToLaunch = "Yes"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES"
launchAutomaticallySubstyle = "2">
notificationPayloadFile = "Configuration/push.apns">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
Expand Down
39 changes: 35 additions & 4 deletions NotificationContentExtension/Camera.swift
Expand Up @@ -45,10 +45,30 @@ class CameraViewController: UIView, NotificationCategory {
}

let imageView = UIImageView()
imageView.frame = vc.view.frame

var aspectRatioConstraint: NSLayoutConstraint?

func updateAspectRatioConstraint(size: CGSize) {
guard size.height > 0 else {
return
}

let ratio = size.width/size.height

guard aspectRatioConstraint?.multiplier != ratio else {
return
}

let constraint = imageView.widthAnchor.constraint(equalTo: imageView.heightAnchor, multiplier: ratio)
constraint.isActive = true
aspectRatioConstraint = constraint
}

// assume we're going to be playing a 16:9 video, and adjust
updateAspectRatioConstraint(size: CGSize(width: 16.0, height: 9.0))

imageView.contentMode = .scaleAspectFit
imageView.accessibilityIdentifier = "camera_notification_imageview"
// imageView.accessibilityLabel = "camera_notification_imageview"
imageView.contentMode = .redraw

var frameCount = 0

Expand Down Expand Up @@ -88,7 +108,16 @@ class CameraViewController: UIView, NotificationCategory {
hud.hide(animated: true)
})

vc.view.addSubview(imageView)
if imageView.superview == nil {
vc.view.addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
imageView.topAnchor.constraint(equalTo: vc.view.topAnchor),
imageView.leadingAnchor.constraint(equalTo: vc.view.leadingAnchor),
imageView.trailingAnchor.constraint(equalTo: vc.view.trailingAnchor),
imageView.bottomAnchor.constraint(equalTo: vc.view.bottomAnchor)
])
}

extensionContext?.mediaPlayingStarted()
}
Expand All @@ -97,6 +126,8 @@ class CameraViewController: UIView, NotificationCategory {
image.accessibilityIdentifier = "camera_notification_image"
imageView.image = image
imageView.image?.accessibilityIdentifier = image.accessibilityIdentifier

updateAspectRatioConstraint(size: image.size)
}
}

Expand Down
@@ -0,0 +1,11 @@
{
"aps": {
"alert": {
"title": "title here",
"body": "body here"
},
"sound": "default",
"category": "camera"
},
"entity_id": "camera.outside"
}

0 comments on commit c1ed1e3

Please sign in to comment.