preset advanced widget template profile
This commit is contained in:
1 parent
a03a50d238
commit
f260d75372
6 files changed
+64
-10
No files matched your search
@@ -10,6 +10,7 @@
|
||||
#import "OnScreenButtonState.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
#define DEFAULT_TEMPLATE_NAME @"高级控件模板(Advanced widget templates)"
|
||||
|
||||
/**
|
||||
This object contains information pertaining to any of the user created, custom on screen controller layout configurations, or 'profiles.' The object contains a 'name' property for easy reference, as well as an 'isSelected' property which is used to determine whether this particular custom OSC layout should show on screen during game stream view. Only one 'OSCProfile' is set to 'isSelected' at any given time. The object also contains an array of 'OnScreenButtonStates' which provides information that allows us to move and hide/unhide each of the 19 on screen buttons. Note that the 'buttonStates' property should contain an NSMutableArray of ENCODED 'OnScreenButtonState' objects. This allows us to save the 'OSCProfile' object to NSUserDefaults.
|
||||
|
||||
@@ -16,6 +16,7 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
Note that the implementation file contains a number of 'Helper' methods. These helper methods are only used in this class's implementation file and help to reduce re-writing large blocks of code that are called multiple times throughout the file
|
||||
*/
|
||||
@interface OSCProfilesManager : NSObject
|
||||
@property NSMutableArray <OSCProfile *> *currentProfiles;
|
||||
|
||||
|
||||
+ (OSCProfilesManager *) sharedManager:(CGRect)viewBounds;
|
||||
@@ -40,7 +41,7 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
- (NSMutableArray *) getEncodedProfiles;
|
||||
- (void) importEncodedProfiles:(NSMutableArray* )profilesEncoded;
|
||||
- (OnScreenButtonState *)unarchiveButtonStateEncoded:(NSData *)data;
|
||||
|
||||
- (void) importDefaultTemplates;
|
||||
|
||||
#pragma mark - Setters
|
||||
/**
|
||||
@@ -64,7 +65,7 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
* Lets the caller of this method know whether a profile with a given name already exists in persistent storage
|
||||
*/
|
||||
- (BOOL) profileNameAlreadyExist:(NSString*)name;
|
||||
|
||||
- (BOOL) profileName:(NSString*) name alreadyExistIn:(NSMutableArray*)profiles;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ static CGRect streamViewBounds;
|
||||
dispatch_once(&onceToken, ^{
|
||||
_sharedManager = [[self alloc] init];
|
||||
});
|
||||
// _sharedManager.currentProfiles = [_sharedManager getAllProfiles];
|
||||
return _sharedManager;
|
||||
}
|
||||
|
||||
@@ -137,14 +138,38 @@ static CGRect streamViewBounds;
|
||||
}
|
||||
|
||||
- (void) importEncodedProfiles:(NSMutableArray* )profilesEncoded {
|
||||
NSMutableArray* localEncodedPofiles = [self getEncodedProfiles];
|
||||
[localEncodedPofiles removeObjectAtIndex:0];
|
||||
NSMutableArray* targetProfiles = [_currentProfiles mutableCopy]; //_currentProfiles is availabled as long as getAllProfiles was called before calling this method (_currentProfiles avoids frequent accessing persisted data)
|
||||
if([self profileName:DEFAULT_TEMPLATE_NAME alreadyExistIn:targetProfiles] && targetProfiles.count > 1) [targetProfiles removeObjectAtIndex:1];
|
||||
if(targetProfiles.count > 0) [targetProfiles removeObjectAtIndex:0];
|
||||
NSMutableArray* localEncodedPofiles = [self encodedProfilesFromArray:targetProfiles];
|
||||
[profilesEncoded addObjectsFromArray:localEncodedPofiles];
|
||||
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:profilesEncoded requiringSecureCoding:YES error:nil];
|
||||
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"OSCProfiles"];
|
||||
[[NSUserDefaults standardUserDefaults] synchronize];
|
||||
}
|
||||
|
||||
- (void) importDefaultTemplates{
|
||||
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"widgetTemplates" ofType:@"bin"];
|
||||
if (filePath) {
|
||||
// 2. 读取二进制数据
|
||||
NSError *error;
|
||||
NSData *fileData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error];
|
||||
NSMutableArray *profilesEncoded;
|
||||
if (fileData && !error) {
|
||||
// 3. 成功读取
|
||||
NSSet *classes = [NSSet setWithObjects: [NSMutableData class], [NSMutableArray class], nil];
|
||||
if (@available(iOS 13.0, *)) {
|
||||
// 在 iOS 13 及以上使用 unarchivedObjectOfClasses
|
||||
profilesEncoded = [NSKeyedUnarchiver unarchivedObjectOfClasses:classes fromData:fileData error:&error];
|
||||
} else {
|
||||
// 在 iOS 12 及以下使用 unarchiveObjectWithData(旧方法)
|
||||
profilesEncoded = [NSKeyedUnarchiver unarchivedObjectOfClass:[NSMutableArray class] fromData:fileData error:&error];
|
||||
}
|
||||
[self importEncodedProfiles:profilesEncoded];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#pragma mark - Globally Accessible Methods
|
||||
|
||||
@@ -165,6 +190,7 @@ static CGRect streamViewBounds;
|
||||
profileDecoded = [NSKeyedUnarchiver unarchivedObjectOfClasses: classes fromData:profileEncoded error: nil];
|
||||
[profilesDecoded addObject: profileDecoded];
|
||||
}
|
||||
_currentProfiles = profilesDecoded;
|
||||
return profilesDecoded;
|
||||
}
|
||||
|
||||
@@ -352,15 +378,22 @@ static CGRect streamViewBounds;
|
||||
|
||||
- (BOOL) profileNameAlreadyExist:(NSString*)name {
|
||||
NSMutableArray *profiles = [self getAllProfiles];
|
||||
|
||||
/* Iterate through the decoded profiles and return 'YES' if one of the profiles' 'name' properties equals the 'name' passed into this method */
|
||||
for (OSCProfile *profile in profiles) {
|
||||
|
||||
if ([profile.name isEqualToString:name]) {
|
||||
return YES;
|
||||
}
|
||||
}
|
||||
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (BOOL) profileName:(NSString*) name alreadyExistIn:(NSMutableArray*)profiles {
|
||||
/* Iterate through the decoded profiles and return 'YES' if one of the profiles' 'name' properties equals the 'name' passed into this method */
|
||||
for (OSCProfile *profile in profiles) {
|
||||
if ([profile.name isEqualToString:name]) {
|
||||
return YES;
|
||||
}
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
@@ -373,7 +406,7 @@ static CGRect streamViewBounds;
|
||||
error:nil];
|
||||
} else {
|
||||
// 在 iOS 12 及以下使用 unarchiveObjectWithData(旧方法)
|
||||
buttonState = [NSKeyedUnarchiver unarchivedObjectOfClass: [OnScreenButtonState class] fromData:data error:nil];
|
||||
buttonState = [NSKeyedUnarchiver unarchivedObjectOfClass:[OnScreenButtonState class] fromData:data error:nil];
|
||||
}
|
||||
return buttonState;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user