-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for Live activties in Advanced sample app
- Loading branch information
1 parent
b892c74
commit b149866
Showing
15 changed files
with
1,076 additions
and
103 deletions.
There are no files selected for viewing
424 changes: 418 additions & 6 deletions
424
FirebaseMessaging/Apps/AdvancedSample/AdvancedSample.xcodeproj/project.pbxproj
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
FirebaseMessaging/Apps/AdvancedSample/SampleLiveActivity/AppIntent.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import WidgetKit | ||
import AppIntents | ||
|
||
struct ConfigurationAppIntent: WidgetConfigurationIntent { | ||
static var title: LocalizedStringResource = "Configuration" | ||
static var description = IntentDescription("This is an example widget.") | ||
|
||
// An example configurable parameter. | ||
@Parameter(title: "Favorite Emoji", default: "😃") | ||
var favoriteEmoji: String | ||
} |
11 changes: 11 additions & 0 deletions
11
...Apps/AdvancedSample/SampleLiveActivity/Assets.xcassets/AccentColor.colorset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"colors" : [ | ||
{ | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...g/Apps/AdvancedSample/SampleLiveActivity/Assets.xcassets/AppIcon.appiconset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"platform" : "ios", | ||
"size" : "1024x1024" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
FirebaseMessaging/Apps/AdvancedSample/SampleLiveActivity/Assets.xcassets/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...AdvancedSample/SampleLiveActivity/Assets.xcassets/WidgetBackground.colorset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"colors" : [ | ||
{ | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
FirebaseMessaging/Apps/AdvancedSample/SampleLiveActivity/Info.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>NSExtension</key> | ||
<dict> | ||
<key>NSExtensionPointIdentifier</key> | ||
<string>com.apple.widgetkit-extension</string> | ||
</dict> | ||
</dict> | ||
</plist> |
91 changes: 91 additions & 0 deletions
91
FirebaseMessaging/Apps/AdvancedSample/SampleLiveActivity/SampleLiveActivity.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import WidgetKit | ||
import SwiftUI | ||
|
||
struct Provider: AppIntentTimelineProvider { | ||
func placeholder(in context: Context) -> SimpleEntry { | ||
SimpleEntry(date: Date(), configuration: ConfigurationAppIntent()) | ||
} | ||
|
||
func snapshot(for configuration: ConfigurationAppIntent, in context: Context) async -> SimpleEntry { | ||
SimpleEntry(date: Date(), configuration: configuration) | ||
} | ||
|
||
func timeline(for configuration: ConfigurationAppIntent, in context: Context) async -> Timeline<SimpleEntry> { | ||
var entries: [SimpleEntry] = [] | ||
|
||
// Generate a timeline consisting of five entries an hour apart, starting from the current date. | ||
let currentDate = Date() | ||
for hourOffset in 0 ..< 5 { | ||
let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)! | ||
let entry = SimpleEntry(date: entryDate, configuration: configuration) | ||
entries.append(entry) | ||
} | ||
|
||
return Timeline(entries: entries, policy: .atEnd) | ||
} | ||
} | ||
|
||
struct SimpleEntry: TimelineEntry { | ||
let date: Date | ||
let configuration: ConfigurationAppIntent | ||
} | ||
|
||
struct SampleLiveActivityEntryView : View { | ||
var entry: Provider.Entry | ||
|
||
var body: some View { | ||
VStack { | ||
Text("Time:") | ||
Text(entry.date, style: .time) | ||
|
||
Text("Favorite Emoji:") | ||
Text(entry.configuration.favoriteEmoji) | ||
} | ||
} | ||
} | ||
|
||
struct SampleLiveActivity: Widget { | ||
let kind: String = "SampleLiveActivity" | ||
|
||
var body: some WidgetConfiguration { | ||
AppIntentConfiguration(kind: kind, intent: ConfigurationAppIntent.self, provider: Provider()) { entry in | ||
SampleLiveActivityEntryView(entry: entry) | ||
.containerBackground(.fill.tertiary, for: .widget) | ||
} | ||
} | ||
} | ||
|
||
extension ConfigurationAppIntent { | ||
fileprivate static var smiley: ConfigurationAppIntent { | ||
let intent = ConfigurationAppIntent() | ||
intent.favoriteEmoji = "😀" | ||
return intent | ||
} | ||
|
||
fileprivate static var starEyes: ConfigurationAppIntent { | ||
let intent = ConfigurationAppIntent() | ||
intent.favoriteEmoji = "🤩" | ||
return intent | ||
} | ||
} | ||
|
||
#Preview(as: .systemSmall) { | ||
SampleLiveActivity() | ||
} timeline: { | ||
SimpleEntry(date: .now, configuration: .smiley) | ||
SimpleEntry(date: .now, configuration: .starEyes) | ||
} |
24 changes: 24 additions & 0 deletions
24
FirebaseMessaging/Apps/AdvancedSample/SampleLiveActivity/SampleLiveActivityBundle.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import WidgetKit | ||
import SwiftUI | ||
|
||
@main | ||
struct SampleLiveActivityBundle: WidgetBundle { | ||
var body: some Widget { | ||
SampleLiveActivity() | ||
SampleLiveActivityLiveActivity() | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
...baseMessaging/Apps/AdvancedSample/SampleLiveActivity/SampleLiveActivityLiveActivity.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import ActivityKit | ||
import WidgetKit | ||
import SwiftUI | ||
|
||
struct SampleLiveActivityAttributes: ActivityAttributes { | ||
public struct ContentState: Codable, Hashable { | ||
enum LVStatus: Float, Codable, Hashable { | ||
case started = 0 | ||
case inProgress = 1 | ||
case completed = 2 | ||
|
||
var description: String { | ||
switch self { | ||
case .started: | ||
return "Your Live Activity is started!" | ||
case .inProgress: | ||
return "Your Live Activity is in progress!" | ||
case .completed: | ||
return "Your Live activity is completed!" | ||
} | ||
} | ||
} | ||
|
||
let status: LVStatus | ||
} | ||
|
||
// Fixed non-changing properties about your activity go here! | ||
var lvInstanceNumber: Double | ||
} | ||
|
||
struct SampleLiveActivityLiveActivity: Widget { | ||
var body: some WidgetConfiguration { | ||
ActivityConfiguration(for: SampleLiveActivityAttributes.self) { context in | ||
// Lock screen/banner UI goes here | ||
VStack { | ||
SampleLiveActivityView(context: context) | ||
} | ||
.activityBackgroundTint(Color.cyan) | ||
.activitySystemActionForegroundColor(Color.black) | ||
|
||
} dynamicIsland: { context in | ||
DynamicIsland { | ||
// Expanded UI goes here. Compose the expanded UI through | ||
// various regions, like leading/trailing/center/bottom | ||
DynamicIslandExpandedRegion(.leading) { | ||
Text("Leading") | ||
} | ||
DynamicIslandExpandedRegion(.trailing) { | ||
Text("Trailing") | ||
} | ||
DynamicIslandExpandedRegion(.bottom) { | ||
Text("Bottom") | ||
// more content | ||
} | ||
} compactLeading: { | ||
Text("L") | ||
} compactTrailing: { | ||
Text("T") | ||
} minimal: { | ||
Text("M") | ||
} | ||
.widgetURL(URL(string: "http://www.apple.com")) | ||
.keylineTint(Color.red) | ||
} | ||
} | ||
} | ||
|
||
extension SampleLiveActivityAttributes { | ||
fileprivate static var preview: SampleLiveActivityAttributes { | ||
SampleLiveActivityAttributes(lvInstanceNumber: 1) | ||
} | ||
} | ||
|
||
extension SampleLiveActivityAttributes.ContentState { | ||
fileprivate static var stateStarted: SampleLiveActivityAttributes.ContentState { | ||
SampleLiveActivityAttributes.ContentState(status:LVStatus.started) | ||
} | ||
|
||
fileprivate static var stateInProgress: SampleLiveActivityAttributes.ContentState { | ||
SampleLiveActivityAttributes.ContentState(status:LVStatus.inProgress) | ||
} | ||
|
||
fileprivate static var stateCompleted: SampleLiveActivityAttributes.ContentState { | ||
SampleLiveActivityAttributes.ContentState(status:LVStatus.completed) | ||
} | ||
} | ||
|
||
#Preview("Notification", as: .content, using: SampleLiveActivityAttributes.preview) { | ||
SampleLiveActivityLiveActivity() | ||
} contentStates: { | ||
SampleLiveActivityAttributes.ContentState.stateStarted | ||
SampleLiveActivityAttributes.ContentState.stateInProgress | ||
SampleLiveActivityAttributes.ContentState.stateCompleted | ||
} |
43 changes: 43 additions & 0 deletions
43
FirebaseMessaging/Apps/AdvancedSample/SampleLiveActivity/SampleLiveActivityView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import SwiftUI | ||
import WidgetKit | ||
|
||
struct SampleLiveActivityView: View { | ||
|
||
let context: ActivityViewContext< SampleLiveActivityAttributes> | ||
|
||
var body: some View { | ||
VStack { | ||
HStack { | ||
Image(systemName: "cup.and.saucer") | ||
ProgressView(value: context.state.status.rawValue, total: 2) | ||
.tint(.black) | ||
.background(Color.brown) | ||
Image(systemName: "cup.and.saucer.fill") | ||
} | ||
.padding(16) | ||
|
||
Text("\(context.state.status.description)") | ||
.font(.system(size: 18, weight: .semibold)) | ||
.padding(.bottom) | ||
Text("Instance No: " + String(context.attributes.lvInstanceNumber)) | ||
.font(.system(size: 18, weight: .semibold)) | ||
.padding(.bottom) | ||
Spacer() | ||
} | ||
.background(Color.brown.opacity(0.6)) | ||
} | ||
} |
Oops, something went wrong.