@@ -17,7 +17,7 @@ export class WidgetCommand implements ICommand {
17
17
protected $projectData : IProjectData ,
18
18
protected $projectConfigService : IProjectConfigService ,
19
19
protected $logger : ILogger ,
20
- protected $errors : IErrors
20
+ protected $errors : IErrors ,
21
21
) {
22
22
this . $projectData . initializeProjectData ( ) ;
23
23
}
@@ -46,7 +46,7 @@ export class WidgetIOSCommand extends WidgetCommand {
46
46
$projectData : IProjectData ,
47
47
$projectConfigService : IProjectConfigService ,
48
48
$logger : ILogger ,
49
- $errors : IErrors
49
+ $errors : IErrors ,
50
50
) {
51
51
super ( $projectData , $projectConfigService , $logger , $errors ) ;
52
52
}
@@ -81,7 +81,7 @@ export class WidgetIOSCommand extends WidgetCommand {
81
81
{
82
82
title : "Live Activity with Home Screen Widget" ,
83
83
description :
84
- "This will create a Live Activity that will display on the iOS Lock Screen with an optional Widget." ,
84
+ "This will create a Live Activity that will display on the iOS Lock Screen with ability to also display a Home Screen Widget." ,
85
85
value : 1 ,
86
86
} ,
87
87
{
@@ -95,27 +95,25 @@ export class WidgetIOSCommand extends WidgetCommand {
95
95
96
96
const bundleId = this . $projectConfigService . getValue ( `id` , "" ) ;
97
97
98
- switch ( result . value ) {
99
- case 0 :
100
- this . $logger . info ( "TODO" ) ;
101
- break ;
102
- case 1 :
103
- await this . generateSharedWidgetPackage (
104
- this . $projectData . projectDir ,
105
- name
106
- ) ;
107
- this . generateWidget (
108
- this . $projectData . projectDir ,
109
- name ,
110
- bundleId ,
111
- result . value
112
- ) ;
113
- this . generateAppleUtility ( this . $projectData . projectDir , name , bundleId ) ;
114
- break ;
115
- case 2 :
116
- this . $logger . info ( "TODO" ) ;
117
- break ;
98
+ if ( [ 0 , 1 ] . includes ( result . value ) ) {
99
+ // shared model only needed with live activities
100
+ await this . generateSharedWidgetPackage (
101
+ this . $projectData . projectDir ,
102
+ name ,
103
+ ) ;
118
104
}
105
+ this . generateWidget (
106
+ this . $projectData . projectDir ,
107
+ name ,
108
+ bundleId ,
109
+ result . value ,
110
+ ) ;
111
+ this . generateAppleUtility (
112
+ this . $projectData . projectDir ,
113
+ name ,
114
+ bundleId ,
115
+ result . value ,
116
+ ) ;
119
117
}
120
118
121
119
private async generateSharedWidgetPackage ( projectDir : string , name : string ) {
@@ -124,11 +122,11 @@ export class WidgetIOSCommand extends WidgetCommand {
124
122
const sharedWidgetSourceDir = "Sources/SharedWidget" ;
125
123
const sharedWidgetPackagePath = path . join (
126
124
projectDir ,
127
- `${ sharedWidgetDir } /Package.swift`
125
+ `${ sharedWidgetDir } /Package.swift` ,
128
126
) ;
129
127
const sharedWidgetSourcePath = path . join (
130
128
sharedWidgetPath ,
131
- `${ sharedWidgetSourceDir } /${ capitalizeFirstLetter ( name ) } Model.swift`
129
+ `${ sharedWidgetSourceDir } /${ capitalizeFirstLetter ( name ) } Model.swift` ,
132
130
) ;
133
131
const gitIgnorePath = path . join ( projectDir , ".gitignore" ) ;
134
132
@@ -203,7 +201,7 @@ public struct ${capitalizeFirstLetter(name)}Model: ActivityAttributes {
203
201
}
204
202
const spmPackages = configData . ios . SPMPackages ;
205
203
const sharedWidgetPackage = spmPackages ?. find (
206
- ( p ) => p . name === "SharedWidget"
204
+ ( p ) => p . name === "SharedWidget" ,
207
205
) ;
208
206
if ( ! sharedWidgetPackage ) {
209
207
spmPackages . push ( {
@@ -223,7 +221,7 @@ public struct ${capitalizeFirstLetter(name)}Model: ActivityAttributes {
223
221
configData . ios . SPMPackages = spmPackages ;
224
222
await this . $projectConfigService . setValue (
225
223
"" , // root
226
- configData as { [ key : string ] : SupportedConfigValues }
224
+ configData as { [ key : string ] : SupportedConfigValues } ,
227
225
) ;
228
226
229
227
if ( fs . existsSync ( gitIgnorePath ) ) {
@@ -247,7 +245,7 @@ public struct ${capitalizeFirstLetter(name)}Model: ActivityAttributes {
247
245
projectDir : string ,
248
246
name : string ,
249
247
bundleId : string ,
250
- type : number
248
+ type : number ,
251
249
) : void {
252
250
const appResourcePath = this . $projectData . appResourcesDirectoryPath ;
253
251
const capitalName = capitalizeFirstLetter ( name ) ;
@@ -258,21 +256,21 @@ public struct ${capitalizeFirstLetter(name)}Model: ActivityAttributes {
258
256
const extensionsInfoPath = path . join ( widgetPath , `Info.plist` ) ;
259
257
const extensionsPrivacyPath = path . join (
260
258
widgetPath ,
261
- `PrivacyInfo.xcprivacy`
259
+ `PrivacyInfo.xcprivacy` ,
262
260
) ;
263
261
const extensionsConfigPath = path . join ( widgetPath , `extension.json` ) ;
264
262
const entitlementsPath = path . join ( widgetPath , `${ name } .entitlements` ) ;
265
263
const widgetBundlePath = path . join (
266
264
widgetPath ,
267
- `${ capitalName } Bundle.swift`
265
+ `${ capitalName } Bundle.swift` ,
268
266
) ;
269
267
const widgetHomeScreenPath = path . join (
270
268
widgetPath ,
271
- `${ capitalName } HomeScreenWidget.swift`
269
+ `${ capitalName } HomeScreenWidget.swift` ,
272
270
) ;
273
271
const widgetLiveActivityPath = path . join (
274
272
widgetPath ,
275
- `${ capitalName } LiveActivity.swift`
273
+ `${ capitalName } LiveActivity.swift` ,
276
274
) ;
277
275
// const appIntentPath = path.join(widgetPath, `AppIntent.swift`);
278
276
// const widgetLockScreenControlPath = path.join(
@@ -282,7 +280,7 @@ public struct ${capitalizeFirstLetter(name)}Model: ActivityAttributes {
282
280
const appEntitlementsPath = path . join (
283
281
appResourcePath ,
284
282
"iOS" ,
285
- "app.entitlements"
283
+ "app.entitlements" ,
286
284
) ;
287
285
288
286
if ( ! fs . existsSync ( extensionsConfigPath ) ) {
@@ -664,7 +662,7 @@ struct ${capitalName}HomeScreenWidget: Widget {
664
662
const appInfoPlist = plist . parse (
665
663
fs . readFileSync ( appInfoPlistPath , {
666
664
encoding : "utf-8" ,
667
- } )
665
+ } ) ,
668
666
) as plist . PlistObject ;
669
667
670
668
if ( ! appInfoPlist [ appSupportLiveActivity ] ) {
@@ -680,7 +678,7 @@ struct ${capitalName}HomeScreenWidget: Widget {
680
678
const appEntitlementsPlist = plist . parse (
681
679
fs . readFileSync ( appEntitlementsPath , {
682
680
encoding : "utf-8" ,
683
- } )
681
+ } ) ,
684
682
) as plist . PlistObject ;
685
683
686
684
if ( ! appEntitlementsPlist [ appGroupKey ] ) {
@@ -758,20 +756,26 @@ struct ${capitalName}HomeScreenWidget: Widget {
758
756
fs . writeFileSync ( extensionsConfigPath , content ) ;
759
757
760
758
console . log (
761
- `🚀 Your widget is now ready to develop: App_Resources/iOS/extensions/${ name } .\n`
759
+ `🚀 Your widget is now ready to develop: App_Resources/iOS/extensions/${ name } .\n` ,
762
760
) ;
763
- console . log (
764
- `Followup steps:\n
765
- - Check App_Resources/iOS/build.xcconfig uses IPHONEOS_DEPLOYMENT_TARGET=17 or higher.
766
- - Update App_Resources/iOS/extensions/provisioning.json with your profile id.
767
- - Customize App_Resources/iOS/extensions/${ name } /${ capitalizeFirstLetter (
768
- name
769
- ) } LiveActivity.swift for your display.
770
- - Customize Shared_Resources/iOS/SharedWidget/Sources/SharedWidget/${ capitalizeFirstLetter (
771
- name
772
- ) } Model.swift for your data.
773
- `
761
+ const steps = [ "Followup steps:" ] ;
762
+ steps . push (
763
+ "- Check App_Resources/iOS/build.xcconfig uses IPHONEOS_DEPLOYMENT_TARGET = 17 or higher." ,
774
764
) ;
765
+ steps . push (
766
+ "- Update App_Resources/iOS/extensions/provisioning.json with your profile id." ,
767
+ ) ;
768
+ if ( [ 0 , 1 ] . includes ( type ) ) {
769
+ steps . push (
770
+ `- Customize App_Resources/iOS/extensions/${ name } /${ capitalizeFirstLetter ( name ) } LiveActivity.swift for your display.` ,
771
+ ) ;
772
+ steps . push (
773
+ `- Customize Shared_Resources/iOS/SharedWidget/Sources/SharedWidget/${ capitalizeFirstLetter (
774
+ name ,
775
+ ) } Model.swift for your data.`,
776
+ ) ;
777
+ }
778
+ console . log ( steps . join ( "\n" ) ) ;
775
779
}
776
780
777
781
// if (fs.existsSync(filePath)) {
@@ -783,14 +787,15 @@ struct ${capitalName}HomeScreenWidget: Widget {
783
787
private generateAppleUtility (
784
788
projectDir : string ,
785
789
name : string ,
786
- bundleId : string
790
+ bundleId : string ,
791
+ type : number ,
787
792
) : void {
788
793
const capitalName = capitalizeFirstLetter ( name ) ;
789
794
const appResourcePath = this . $projectData . appResourcesDirectoryPath ;
790
795
const appResourceSrcPath = path . join ( appResourcePath , "iOS" , "src" ) ;
791
796
const appleUtilityPath = path . join (
792
797
appResourceSrcPath ,
793
- `AppleWidgetUtils.swift`
798
+ `AppleWidgetUtils.swift` ,
794
799
) ;
795
800
const referenceTypesPath = path . join ( projectDir , "references.d.ts" ) ;
796
801
@@ -800,16 +805,7 @@ struct ${capitalName}HomeScreenWidget: Widget {
800
805
if ( ! fs . existsSync ( appleUtilityPath ) ) {
801
806
}
802
807
803
- let content = `import Foundation
804
- import UIKit
805
- import ActivityKit
806
- import WidgetKit
807
- import SharedWidget
808
-
809
- @objcMembers
810
- public class AppleWidgetUtils: NSObject {
811
-
812
- // Live Activity Handling
808
+ const liveActivityUtilities = `// Live Activity Handling
813
809
public static func startActivity(_ data: NSDictionary) {
814
810
if ActivityAuthorizationInfo().areActivitiesEnabled {
815
811
let numberOfPizzas = data.object(forKey: "numberOfPizzas") as! Int
@@ -861,7 +857,18 @@ public class AppleWidgetUtils: NSObject {
861
857
}
862
858
}
863
859
}
864
- }
860
+ }` ;
861
+
862
+ let content = `import Foundation
863
+ import UIKit
864
+ import ActivityKit
865
+ import WidgetKit
866
+ ${ [ 0 , 1 ] . includes ( type ) ? "import SharedWidget" : "" }
867
+
868
+ @objcMembers
869
+ public class AppleWidgetUtils: NSObject {
870
+ ${ [ 0 , 1 ] . includes ( type ) ? liveActivityUtilities : "" }
871
+ // Shared App Group Data
865
872
public static func getData(key: String) -> String? {
866
873
guard let sharedDefaults = UserDefaults(suiteName: "group.${ bundleId } ") else {
867
874
return nil
@@ -882,7 +889,6 @@ public class AppleWidgetUtils: NSObject {
882
889
sharedDefaults.removeObject(forKey: key)
883
890
sharedDefaults.synchronize()
884
891
}
885
-
886
892
// Home Screen Widget Handling
887
893
public static func updateWidget() {
888
894
if #available(iOS 14.0, *) {
@@ -901,7 +907,7 @@ public class AppleWidgetUtils: NSObject {
901
907
declare interface AppleWidgetModelData {
902
908
numberOfPizzas: number;
903
909
totalAmount: string;
904
- driverName : string;
910
+ message : string;
905
911
deliveryTime: number;
906
912
}
907
913
declare class AppleWidgetUtils extends NSObject {
0 commit comments