Browse Source

【功能】默认选择主题

xingcheng02 1 year ago
parent
commit
2c527017ba

+ 6 - 2
XCTheme/Classes/Impl/XCThemeManager.swift

@@ -18,6 +18,8 @@ public class XCThemeManager: NSObject {
     
     public static let shared = XCThemeManager()
     
+    public var defaultThemeBlock: (() -> String?)?
+    
     public var selectedTheme: XCThemeSpec? {
         didSet {
             self.forceThemeInterfaceStyleForWindows()
@@ -64,8 +66,10 @@ public class XCThemeManager: NSObject {
             }
         }
         
-        // TODO: Mocked
-        self.selectedTheme = self.theme(forId: "0.system_auto")
+        if let defaultThemeBlock = self.defaultThemeBlock,
+            let defaultTheme = defaultThemeBlock() {
+            self.selectedTheme = self.theme(forId: defaultTheme)
+        }
         
         let originalMethod = class_getInstanceMethod(UIViewController.classForCoder(), #selector(UIViewController.viewDidLoad))!
         let newMethod = class_getInstanceMethod(UIViewController.classForCoder(), #selector(UIViewController.XCThemeManagerSwizzledViewDidLoad))!

+ 8 - 0
XCTheme/Classes/Impl/XCThemeService.m

@@ -26,6 +26,14 @@
 }
 
 #pragma mark - Theme Download
+- (NSString * _Nonnull (^)(void))defaultThemeBlock {
+    return XCThemeManager.shared.defaultThemeBlock;
+}
+
+- (void)setDefaultThemeBlock:(NSString * _Nullable (^)(void))defaultThemeBlock {
+    XCThemeManager.shared.defaultThemeBlock = defaultThemeBlock;
+}
+
 - (NSArray<XCThemeSpecModel *> *)fetchBundleSpecs {
     NSString *path = [NSBundle.mainBundle.resourcePath stringByAppendingString:@"/ThemeResources"];
     NSArray *items = [[NSFileManager.defaultManager contentsOfDirectoryAtPath:path error:nil] sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) {

+ 4 - 0
XCTheme/Classes/Module/XCThemeProtocol.h

@@ -30,6 +30,10 @@ NS_ASSUME_NONNULL_BEGIN
 /// @discussion themeId may not the key for storage system, the interface layer should fetch the themeId-downloadURL pairs from server first, then call fetchThemeData method to start download contents. Must be settled before call fetchThemeData:completion:
 @property (nonatomic, copy, nullable) void (^downloadBlock)(NSString *themeId, NSString *writeToPath, void (^)(BOOL success, NSString *reason));
 
+/// block will called when theme engine loaded, should return a default theme identifier
+/// @discussion should be set on `+[NSObject load]`, the block will called after main framework prepared
+@property (nonatomic, copy, nullable) NSString * _Nullable (^defaultThemeBlock)(void);
+
 /// Fetch specs from bundle, not mutable
 - (NSArray<XCThemeSpecModel *> *)fetchBundleSpecs;