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

Update stale #77

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
</div>
<br />

A Flutter plugin to use iOS 16.1+ **Live Activities** & iPhone 14 Pro **Dynamic Island** features.
A Flutter plugin to use iOS 16.2+ **Live Activities** & iPhone 14 Pro **Dynamic Island** features.

## 🧐 What is it ?

This plugin uses the [iOS ActivityKit API](https://developer.apple.com/documentation/activitykit/displaying-live-data-with-live-activities).

**live_activities** can be used to show **dynamic live notification** & implement **dynamic island** feature on iPhones that support it 🏝️

> ⚠️ **live_activities** is only intended to use with **iOS 16.1+** !
> It will simply do nothing on other platform & < iOS 16.1
> ⚠️ **live_activities** is only intended to use with **iOS 16.2+** !
> It will simply do nothing on other platform & < iOS 16.2

<br />
<div align="center" style="display: flex;align-items: center;justify-content: center;">
Expand Down
8 changes: 4 additions & 4 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 1410;
LastUpgradeCheck = 1430;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = "";
TargetAttributes = {
27C0558128E42F5700BA744B = {
Expand Down Expand Up @@ -510,7 +510,7 @@
INFOPLIST_FILE = "extension-example/Info.plist";
INFOPLIST_KEY_CFBundleDisplayName = "extension-example";
INFOPLIST_KEY_NSHumanReadableCopyright = "";
IPHONEOS_DEPLOYMENT_TARGET = 16.1;
IPHONEOS_DEPLOYMENT_TARGET = 16.2;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -550,7 +550,7 @@
INFOPLIST_FILE = "extension-example/Info.plist";
INFOPLIST_KEY_CFBundleDisplayName = "extension-example";
INFOPLIST_KEY_NSHumanReadableCopyright = "";
IPHONEOS_DEPLOYMENT_TARGET = 16.1;
IPHONEOS_DEPLOYMENT_TARGET = 16.2;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -587,7 +587,7 @@
INFOPLIST_FILE = "extension-example/Info.plist";
INFOPLIST_KEY_CFBundleDisplayName = "extension-example";
INFOPLIST_KEY_NSHumanReadableCopyright = "";
IPHONEOS_DEPLOYMENT_TARGET = 16.1;
IPHONEOS_DEPLOYMENT_TARGET = 16.2;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1430"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
4 changes: 2 additions & 2 deletions example/ios/extension-example/extension_example.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import SwiftUI
@main
struct Widgets: WidgetBundle {
var body: some Widget {
if #available(iOS 16.1, *) {
if #available(iOS 16.2, *) {
FootballMatchApp()
}
}
Expand All @@ -30,7 +30,7 @@ struct LiveActivitiesAppAttributes: ActivityAttributes, Identifiable {
// Create shared default with custom group
let sharedDefault = UserDefaults(suiteName: "group.dimitridessus.liveactivities")!

@available(iOSApplicationExtension 16.1, *)
@available(iOSApplicationExtension 16.2, *)
struct FootballMatchApp: Widget {
var body: some WidgetConfiguration {
ActivityConfiguration(for: LiveActivitiesAppAttributes.self) { context in
Expand Down
50 changes: 41 additions & 9 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,30 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.6.7"
leak_tracker:
dependency: transitive
description:
name: leak_tracker
sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa"
url: "https://pub.dev"
source: hosted
version: "10.0.0"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: b46c5e37c19120a8a01918cfaf293547f47269f7cb4b0058f21531c2465d6ef0
url: "https://pub.dev"
source: hosted
version: "2.0.1"
leak_tracker_testing:
dependency: transitive
description:
name: leak_tracker_testing
sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47
url: "https://pub.dev"
source: hosted
version: "2.0.1"
lints:
dependency: transitive
description:
Expand All @@ -174,39 +198,39 @@ packages:
path: ".."
relative: true
source: path
version: "1.9.1"
version: "1.9.1+1"
matcher:
dependency: transitive
description:
name: matcher
sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e"
sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb
url: "https://pub.dev"
source: hosted
version: "0.12.16"
version: "0.12.16+1"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
url: "https://pub.dev"
source: hosted
version: "0.5.0"
version: "0.8.0"
meta:
dependency: transitive
description:
name: meta
sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e
sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04
url: "https://pub.dev"
source: hosted
version: "1.10.0"
version: "1.11.0"
path:
dependency: transitive
description:
name: path
sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917"
sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af"
url: "https://pub.dev"
source: hosted
version: "1.8.3"
version: "1.9.0"
path_provider:
dependency: transitive
description:
Expand Down Expand Up @@ -332,6 +356,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.4"
vm_service:
dependency: transitive
description:
name: vm_service
sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957
url: "https://pub.dev"
source: hosted
version: "13.0.0"
web:
dependency: transitive
description:
Expand Down
88 changes: 51 additions & 37 deletions ios/Classes/SwiftLiveActivitiesPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ActivityKit
import Flutter
import UIKit

@available(iOS 16.1, *)
@available(iOS 16.2, *)
class FlutterAlertConfig {
let _title:String
let _body:String
Expand Down Expand Up @@ -76,15 +76,15 @@ public class SwiftLiveActivitiesPlugin: NSObject, FlutterPlugin, FlutterStreamHa

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
if (call.method == "areActivitiesEnabled") {
if #available(iOS 16.1, *) {
if #available(iOS 16.2, *) {
result(ActivityAuthorizationInfo().areActivitiesEnabled)
} else {
result(false)
}
return
}

if #available(iOS 16.1, *) {
if #available(iOS 16.2, *) {
switch call.method {
case "init":
guard let args = call.arguments as? [String: Any] else {
Expand Down Expand Up @@ -124,14 +124,15 @@ public class SwiftLiveActivitiesPlugin: NSObject, FlutterPlugin, FlutterStreamHa
return
}
if let activityId = args["activityId"] as? String, let data = args["data"] as? [String: Any] {
let staleIn = args["staleIn"] as? Int? ?? nil
let alertConfigMap = args["alertConfig"] as? [String:String?];
let alertTitle = alertConfigMap?["title"] as? String;
let alertBody = alertConfigMap?["body"] as? String;
let alertSound = alertConfigMap?["sound"] as? String;

let alertConfig = (alertTitle == nil || alertBody == nil) ? nil : FlutterAlertConfig(title: alertTitle!, body: alertBody!, sound: alertSound);

updateActivity(activityId: activityId, data: data, alertConfig: alertConfig, result: result)
updateActivity(activityId: activityId, data: data, alertConfig: alertConfig, staleIn: staleIn, result: result)
} else {
result(FlutterError(code: "WRONG_ARGS", message: "argument are not valid, check if 'activityId', 'data' are valid", details: nil))
}
Expand Down Expand Up @@ -182,7 +183,7 @@ public class SwiftLiveActivitiesPlugin: NSObject, FlutterPlugin, FlutterStreamHa
}
}

@available(iOS 16.1, *)
@available(iOS 16.2, *)
func createActivity(data: [String: Any], removeWhenAppIsKilled: Bool, staleIn: Int?, result: @escaping FlutterResult) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
Expand All @@ -191,7 +192,6 @@ public class SwiftLiveActivitiesPlugin: NSObject, FlutterPlugin, FlutterStreamHa
result(FlutterError(code: "AUTHORIZATION_ERROR", message: "authorization error", details: error.localizedDescription))
}
}


let liveDeliveryAttributes = LiveActivitiesAppAttributes()
let initialContentState = LiveActivitiesAppAttributes.LiveDeliveryData(appGroupId: appGroupId!)
Expand All @@ -205,7 +205,7 @@ public class SwiftLiveActivitiesPlugin: NSObject, FlutterPlugin, FlutterStreamHa
if #available(iOS 16.2, *){
let activityContent = ActivityContent(
state: initialContentState,
staleDate: staleIn != nil ? Calendar.current.date(byAdding: .minute, value: staleIn!, to: Date.now) : nil)
staleDate: getStaleDate(staleIn))
do {
deliveryActivity = try Activity.request(
attributes: liveDeliveryAttributes,
Expand Down Expand Up @@ -234,8 +234,8 @@ public class SwiftLiveActivitiesPlugin: NSObject, FlutterPlugin, FlutterStreamHa
}
}

@available(iOS 16.1, *)
func updateActivity(activityId: String, data: [String: Any?], alertConfig: FlutterAlertConfig?, result: @escaping FlutterResult) {
@available(iOS 16.2, *)
func updateActivity(activityId: String, data: [String: Any?], alertConfig: FlutterAlertConfig?, staleIn: Int?, result: @escaping FlutterResult) {
Task {
for activity in Activity<LiveActivitiesAppAttributes>.activities {
if activityId == activity.id {
Expand All @@ -250,7 +250,7 @@ public class SwiftLiveActivitiesPlugin: NSObject, FlutterPlugin, FlutterStreamHa
}

let updatedStatus = LiveActivitiesAppAttributes.LiveDeliveryData(appGroupId: self.appGroupId!)
await activity.update(using: updatedStatus, alertConfiguration: alertConfig?.getAlertConfig())
await activity.update(ActivityContent<LiveActivitiesAppAttributes.ContentState>(state: updatedStatus, staleDate: getStaleDate(staleIn)), alertConfiguration: alertConfig?.getAlertConfig())
break;
}
}
Expand All @@ -259,7 +259,7 @@ public class SwiftLiveActivitiesPlugin: NSObject, FlutterPlugin, FlutterStreamHa
}


@available(iOS 16.1, *)
@available(iOS 16.2, *)
func getActivityState(activityId: String, result: @escaping FlutterResult) {
Task {
if let matchingActivity = Activity<LiveActivitiesAppAttributes>.activities.first(where: { $0.id == activityId }) {
Expand All @@ -282,7 +282,7 @@ public class SwiftLiveActivitiesPlugin: NSObject, FlutterPlugin, FlutterStreamHa
}
}

@available(iOS 16.1, *)
@available(iOS 16.2, *)
func getPushToken(activityId: String, result: @escaping FlutterResult) {
Task {
var pushToken: String?;
Expand All @@ -297,7 +297,7 @@ public class SwiftLiveActivitiesPlugin: NSObject, FlutterPlugin, FlutterStreamHa
}
}

@available(iOS 16.1, *)
@available(iOS 16.2, *)
func endActivity(activityId: String, result: @escaping FlutterResult) {
appLifecycleLifeActiviyIds.removeAll { $0 == activityId }
Task {
Expand All @@ -306,7 +306,7 @@ public class SwiftLiveActivitiesPlugin: NSObject, FlutterPlugin, FlutterStreamHa
}
}

@available(iOS 16.1, *)
@available(iOS 16.2, *)
func endAllActivities(result: @escaping FlutterResult) {
Task {
for activity in Activity<LiveActivitiesAppAttributes>.activities {
Expand All @@ -317,7 +317,7 @@ public class SwiftLiveActivitiesPlugin: NSObject, FlutterPlugin, FlutterStreamHa
}
}

@available(iOS 16.1, *)
@available(iOS 16.2, *)
func getAllActivitiesIds(result: @escaping FlutterResult) {
var activitiesId: [String] = []
for activity in Activity<LiveActivitiesAppAttributes>.activities {
Expand All @@ -327,7 +327,7 @@ public class SwiftLiveActivitiesPlugin: NSObject, FlutterPlugin, FlutterStreamHa
result(activitiesId)
}

@available(iOS 16.1, *)
@available(iOS 16.2, *)
private func endActivitiesWithId(activityIds: [String]) async {
for activity in Activity<LiveActivitiesAppAttributes>.activities {
if (activityIds.contains { $0 == activity.id }) {
Expand Down Expand Up @@ -359,7 +359,7 @@ public class SwiftLiveActivitiesPlugin: NSObject, FlutterPlugin, FlutterStreamHa
}

public func applicationWillTerminate(_ application: UIApplication) {
if #available(iOS 16.1, *) {
if #available(iOS 16.2, *) {
Task {
await self.endActivitiesWithId(activityIds: self.appLifecycleLifeActiviyIds)
}
Expand All @@ -376,30 +376,36 @@ public class SwiftLiveActivitiesPlugin: NSObject, FlutterPlugin, FlutterStreamHa
var id = UUID()
}

@available(iOS 16.1, *)
@available(iOS 16.2, *)
private func monitorLiveActivity<T : ActivityAttributes>(_ activity: Activity<T>) {
Task {
for await state in activity.activityStateUpdates {
var response: Dictionary<String, Any> = Dictionary()
response["activityId"] = activity.id
switch state {
case .active:
monitorTokenChanges(activity)
case .dismissed, .ended:
response["status"] = "ended"
activityEventSink?.self(response)
case .stale:
response["status"] = "stale"
activityEventSink?.self(response)
@unknown default:
response["status"] = "unknown"
activityEventSink?.self(response)
}
do {
try
Task {
for await state in activity.activityStateUpdates {
var response: Dictionary<String, Any> = Dictionary()
response["activityId"] = activity.id
switch state {
case .active:
monitorTokenChanges(activity)
case .dismissed, .ended:
response["status"] = "ended"
activityEventSink?.self(response)
case .stale:
response["status"] = "stale"
activityEventSink?.self(response)
@unknown default:
response["status"] = "unknown"
activityEventSink?.self(response)
}
}
}
} catch {
print("error: ");
print(error);
}
}
}

@available(iOS 16.1, *)
@available(iOS 16.2, *)
private func monitorTokenChanges<T: ActivityAttributes>(_ activity: Activity<T>) {
Task {
for await data in activity.pushTokenUpdates {
Expand All @@ -412,4 +418,12 @@ public class SwiftLiveActivitiesPlugin: NSObject, FlutterPlugin, FlutterStreamHa
}
}
}

@available(iOS 16.2, *)
func getStaleDate(_ staleIn: Int?) -> Date? {
if staleIn != nil {
return Calendar.current.date(byAdding: .minute, value: staleIn!, to: Date.now)
}
return nil
}
}
Loading