Skip to content

Commit

Permalink
Cache main color
Browse files Browse the repository at this point in the history
  • Loading branch information
hugeBlack committed Oct 23, 2024
1 parent 8978b6b commit 9842e52
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 13 deletions.
31 changes: 18 additions & 13 deletions LiveContainerSwiftUI/LCAppBanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct LCAppBanner : View {
@State private var errorShow = false
@State private var errorInfo = ""
@AppStorage("dynamicColors") var dynamicColors = true
@State private var mainColor : Color

@EnvironmentObject private var sharedModel : SharedModel

Expand All @@ -45,6 +46,8 @@ struct LCAppBanner : View {
self.delegate = delegate

_model = ObservedObject(wrappedValue: appModel)
_mainColor = State(initialValue: Color.clear)
_mainColor = State(initialValue: extractMainHueColor())
}
@State private var mainHueColor: CGFloat? = nil

Expand Down Expand Up @@ -84,8 +87,8 @@ struct LCAppBanner : View {
}
}

Text("\(appInfo.version()) - \(appInfo.bundleIdentifier())").font(.system(size: 12)).foregroundColor(dynamicColors ? extractMainHueColor(from: appInfo.icon()) : Color("FontColor"))
Text(LocalizedStringKey(model.uiDataFolder == nil ? "lc.appBanner.noDataFolder".loc : model.uiDataFolder!)).font(.system(size: 8)).foregroundColor(dynamicColors ? extractMainHueColor(from: appInfo.icon()) : Color("FontColor"))
Text("\(appInfo.version()) - \(appInfo.bundleIdentifier())").font(.system(size: 12)).foregroundColor(dynamicColors ? mainColor : Color("FontColor"))
Text(LocalizedStringKey(model.uiDataFolder == nil ? "lc.appBanner.noDataFolder".loc : model.uiDataFolder!)).font(.system(size: 8)).foregroundColor(dynamicColors ? mainColor : Color("FontColor"))
})
}
Spacer()
Expand All @@ -105,14 +108,14 @@ struct LCAppBanner : View {
.fixedSize()
.background(GeometryReader { g in
if !model.isSigningInProgress {
Capsule().fill(dynamicColors ? extractMainHueColor(from: appInfo.icon()) : Color("FontColor"))
Capsule().fill(dynamicColors ? mainColor : Color("FontColor"))
} else {
let w = g.size.width
let h = g.size.height
Capsule()
.fill(dynamicColors ? extractMainHueColor(from: appInfo.icon()) : Color("FontColor")).opacity(0.2)
.fill(dynamicColors ? mainColor : Color("FontColor")).opacity(0.2)
Circle()
.fill(dynamicColors ? extractMainHueColor(from: appInfo.icon()) : Color("FontColor"))
.fill(dynamicColors ? mainColor : Color("FontColor"))
.frame(width: w * 2, height: w * 2)
.offset(x: (model.signProgress - 2) * w, y: h/2-w)
}
Expand All @@ -124,7 +127,7 @@ struct LCAppBanner : View {
}
.padding()
.frame(height: 88)
.background(RoundedRectangle(cornerSize: CGSize(width:22, height: 22)).fill(dynamicColors ? extractMainHueColor(from: appInfo.icon()).opacity(0.5) : Color("AppBannerBG")))
.background(RoundedRectangle(cornerSize: CGSize(width:22, height: 22)).fill(dynamicColors ? mainColor.opacity(0.5) : Color("AppBannerBG")))
.onAppear() {
handleOnAppear()
}
Expand Down Expand Up @@ -340,8 +343,11 @@ struct LCAppBanner : View {
self.saveIconExporterShow = true
}

func extractMainHueColor(from image: UIImage) -> Color {
guard let cgImage = image.cgImage else { return Color.clear }
func extractMainHueColor() -> Color {
if let cachedColor = appInfo.cachedColor {
return Color(uiColor: cachedColor)
}
guard let cgImage = appInfo.icon().cgImage else { return Color.clear }

let width = 1
let height = 1
Expand Down Expand Up @@ -375,13 +381,12 @@ struct LCAppBanner : View {
brightness = 0.3
}

return Color(hue: hue, saturation: saturation, brightness: brightness)
let ans = Color(hue: hue, saturation: saturation, brightness: brightness)
appInfo.cachedColor = UIColor(ans)

return Color.red
}





}


Expand Down
18 changes: 18 additions & 0 deletions LiveContainerSwiftUI/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -1655,6 +1655,12 @@
"state" : "translated",
"value" : "Dynamic colors"
}
},
"zh_CN" : {
"stringUnit" : {
"state" : "translated",
"value" : "动态颜色"
}
}
}
},
Expand All @@ -1666,6 +1672,12 @@
"state" : "translated",
"value" : "Enable dynamic colors for app boxes"
}
},
"zh_CN" : {
"stringUnit" : {
"state" : "translated",
"value" : "App横幅的颜色随图标动态变化"
}
}
}
},
Expand Down Expand Up @@ -1796,6 +1808,12 @@
"state" : "translated",
"value" : "interface"
}
},
"zh_CN" : {
"stringUnit" : {
"state" : "translated",
"value" : "外观"
}
}
}
},
Expand Down
1 change: 1 addition & 0 deletions LiveContainerUI/LCAppInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@property bool isHidden;
@property bool doSymlinkInbox;
@property bool bypassAssertBarrierOnQueue;
@property UIColor* cachedColor;

- (void)setBundlePath:(NSString*)newBundlePath;
- (NSMutableDictionary*)info;
Expand Down
32 changes: 32 additions & 0 deletions LiveContainerUI/LCAppInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,36 @@ - (void)setBypassAssertBarrierOnQueue:(bool)enabled {
[self save];

}

- (UIColor*)cachedColor {
if(_info[@"cachedColor"] != nil) {
NSData *colorData = _info[@"cachedColor"];
NSError* error;
UIColor *color = [NSKeyedUnarchiver unarchivedObjectOfClass:UIColor.class fromData:colorData error:&error];
if (!error) {
return color;
} else {
NSLog(@"[LC] failed to get color %@", error);
return nil;
}
} else {
return nil;
}
}

- (void)setCachedColor:(UIColor*) color {
if(color == nil) {
_info[@"cachedColor"] = nil;
} else {
NSError* error;
NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject:color requiringSecureCoding:YES error:&error];
[_info setObject:colorData forKey:@"cachedColor"];
if(error) {
NSLog(@"[LC] failed to set color %@", error);
}

}
[self save];
}

@end
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,5 @@ To install tweaks, you can use the built-in tweak manager in LiveContainer, whic
- @Vishram1123 for the initial shortcut implementation.
- @hugeBlack for SwiftUI contribution
- @Staubgeborener for automatic AltStore/SideStore source updater
- @fkunn1326 for improved app hiding
- @slds1 for dynamic color frature

0 comments on commit 9842e52

Please sign in to comment.