Skip to content

Commit 4cb3409

Browse files
committed
chore: cleanup
1 parent 2f1f40a commit 4cb3409

File tree

1 file changed

+68
-62
lines changed

1 file changed

+68
-62
lines changed

lib/commands/widget.ts

+68-62
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class WidgetCommand implements ICommand {
1717
protected $projectData: IProjectData,
1818
protected $projectConfigService: IProjectConfigService,
1919
protected $logger: ILogger,
20-
protected $errors: IErrors
20+
protected $errors: IErrors,
2121
) {
2222
this.$projectData.initializeProjectData();
2323
}
@@ -46,7 +46,7 @@ export class WidgetIOSCommand extends WidgetCommand {
4646
$projectData: IProjectData,
4747
$projectConfigService: IProjectConfigService,
4848
$logger: ILogger,
49-
$errors: IErrors
49+
$errors: IErrors,
5050
) {
5151
super($projectData, $projectConfigService, $logger, $errors);
5252
}
@@ -81,7 +81,7 @@ export class WidgetIOSCommand extends WidgetCommand {
8181
{
8282
title: "Live Activity with Home Screen Widget",
8383
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.",
8585
value: 1,
8686
},
8787
{
@@ -95,27 +95,25 @@ export class WidgetIOSCommand extends WidgetCommand {
9595

9696
const bundleId = this.$projectConfigService.getValue(`id`, "");
9797

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+
);
118104
}
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+
);
119117
}
120118

121119
private async generateSharedWidgetPackage(projectDir: string, name: string) {
@@ -124,11 +122,11 @@ export class WidgetIOSCommand extends WidgetCommand {
124122
const sharedWidgetSourceDir = "Sources/SharedWidget";
125123
const sharedWidgetPackagePath = path.join(
126124
projectDir,
127-
`${sharedWidgetDir}/Package.swift`
125+
`${sharedWidgetDir}/Package.swift`,
128126
);
129127
const sharedWidgetSourcePath = path.join(
130128
sharedWidgetPath,
131-
`${sharedWidgetSourceDir}/${capitalizeFirstLetter(name)}Model.swift`
129+
`${sharedWidgetSourceDir}/${capitalizeFirstLetter(name)}Model.swift`,
132130
);
133131
const gitIgnorePath = path.join(projectDir, ".gitignore");
134132

@@ -203,7 +201,7 @@ public struct ${capitalizeFirstLetter(name)}Model: ActivityAttributes {
203201
}
204202
const spmPackages = configData.ios.SPMPackages;
205203
const sharedWidgetPackage = spmPackages?.find(
206-
(p) => p.name === "SharedWidget"
204+
(p) => p.name === "SharedWidget",
207205
);
208206
if (!sharedWidgetPackage) {
209207
spmPackages.push({
@@ -223,7 +221,7 @@ public struct ${capitalizeFirstLetter(name)}Model: ActivityAttributes {
223221
configData.ios.SPMPackages = spmPackages;
224222
await this.$projectConfigService.setValue(
225223
"", // root
226-
configData as { [key: string]: SupportedConfigValues }
224+
configData as { [key: string]: SupportedConfigValues },
227225
);
228226

229227
if (fs.existsSync(gitIgnorePath)) {
@@ -247,7 +245,7 @@ public struct ${capitalizeFirstLetter(name)}Model: ActivityAttributes {
247245
projectDir: string,
248246
name: string,
249247
bundleId: string,
250-
type: number
248+
type: number,
251249
): void {
252250
const appResourcePath = this.$projectData.appResourcesDirectoryPath;
253251
const capitalName = capitalizeFirstLetter(name);
@@ -258,21 +256,21 @@ public struct ${capitalizeFirstLetter(name)}Model: ActivityAttributes {
258256
const extensionsInfoPath = path.join(widgetPath, `Info.plist`);
259257
const extensionsPrivacyPath = path.join(
260258
widgetPath,
261-
`PrivacyInfo.xcprivacy`
259+
`PrivacyInfo.xcprivacy`,
262260
);
263261
const extensionsConfigPath = path.join(widgetPath, `extension.json`);
264262
const entitlementsPath = path.join(widgetPath, `${name}.entitlements`);
265263
const widgetBundlePath = path.join(
266264
widgetPath,
267-
`${capitalName}Bundle.swift`
265+
`${capitalName}Bundle.swift`,
268266
);
269267
const widgetHomeScreenPath = path.join(
270268
widgetPath,
271-
`${capitalName}HomeScreenWidget.swift`
269+
`${capitalName}HomeScreenWidget.swift`,
272270
);
273271
const widgetLiveActivityPath = path.join(
274272
widgetPath,
275-
`${capitalName}LiveActivity.swift`
273+
`${capitalName}LiveActivity.swift`,
276274
);
277275
// const appIntentPath = path.join(widgetPath, `AppIntent.swift`);
278276
// const widgetLockScreenControlPath = path.join(
@@ -282,7 +280,7 @@ public struct ${capitalizeFirstLetter(name)}Model: ActivityAttributes {
282280
const appEntitlementsPath = path.join(
283281
appResourcePath,
284282
"iOS",
285-
"app.entitlements"
283+
"app.entitlements",
286284
);
287285

288286
if (!fs.existsSync(extensionsConfigPath)) {
@@ -664,7 +662,7 @@ struct ${capitalName}HomeScreenWidget: Widget {
664662
const appInfoPlist = plist.parse(
665663
fs.readFileSync(appInfoPlistPath, {
666664
encoding: "utf-8",
667-
})
665+
}),
668666
) as plist.PlistObject;
669667

670668
if (!appInfoPlist[appSupportLiveActivity]) {
@@ -680,7 +678,7 @@ struct ${capitalName}HomeScreenWidget: Widget {
680678
const appEntitlementsPlist = plist.parse(
681679
fs.readFileSync(appEntitlementsPath, {
682680
encoding: "utf-8",
683-
})
681+
}),
684682
) as plist.PlistObject;
685683

686684
if (!appEntitlementsPlist[appGroupKey]) {
@@ -758,20 +756,26 @@ struct ${capitalName}HomeScreenWidget: Widget {
758756
fs.writeFileSync(extensionsConfigPath, content);
759757

760758
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`,
762760
);
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.",
774764
);
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"));
775779
}
776780

777781
// if (fs.existsSync(filePath)) {
@@ -783,14 +787,15 @@ struct ${capitalName}HomeScreenWidget: Widget {
783787
private generateAppleUtility(
784788
projectDir: string,
785789
name: string,
786-
bundleId: string
790+
bundleId: string,
791+
type: number,
787792
): void {
788793
const capitalName = capitalizeFirstLetter(name);
789794
const appResourcePath = this.$projectData.appResourcesDirectoryPath;
790795
const appResourceSrcPath = path.join(appResourcePath, "iOS", "src");
791796
const appleUtilityPath = path.join(
792797
appResourceSrcPath,
793-
`AppleWidgetUtils.swift`
798+
`AppleWidgetUtils.swift`,
794799
);
795800
const referenceTypesPath = path.join(projectDir, "references.d.ts");
796801

@@ -800,16 +805,7 @@ struct ${capitalName}HomeScreenWidget: Widget {
800805
if (!fs.existsSync(appleUtilityPath)) {
801806
}
802807

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
813809
public static func startActivity(_ data: NSDictionary) {
814810
if ActivityAuthorizationInfo().areActivitiesEnabled {
815811
let numberOfPizzas = data.object(forKey: "numberOfPizzas") as! Int
@@ -861,7 +857,18 @@ public class AppleWidgetUtils: NSObject {
861857
}
862858
}
863859
}
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
865872
public static func getData(key: String) -> String? {
866873
guard let sharedDefaults = UserDefaults(suiteName: "group.${bundleId}") else {
867874
return nil
@@ -882,7 +889,6 @@ public class AppleWidgetUtils: NSObject {
882889
sharedDefaults.removeObject(forKey: key)
883890
sharedDefaults.synchronize()
884891
}
885-
886892
// Home Screen Widget Handling
887893
public static func updateWidget() {
888894
if #available(iOS 14.0, *) {
@@ -901,7 +907,7 @@ public class AppleWidgetUtils: NSObject {
901907
declare interface AppleWidgetModelData {
902908
numberOfPizzas: number;
903909
totalAmount: string;
904-
driverName: string;
910+
message: string;
905911
deliveryTime: number;
906912
}
907913
declare class AppleWidgetUtils extends NSObject {

0 commit comments

Comments
 (0)