cleaning things up and getting it working with the header background

This commit is contained in:
talksik
2021-12-14 17:23:08 -08:00
parent 08458a764b
commit 532f8cd50a
2 changed files with 15 additions and 7 deletions
+9 -2
View File
@@ -77,14 +77,21 @@ struct RemoteImage_Previews: PreviewProvider {
} }
class NirvanaImage { class NirvanaImage {
static func getAverageColor(url:URL) -> UIColor? { static func getAverageColor(url:String) -> UIColor? {
if let data = try? Data(contentsOf: url) { guard let parsedURL = URL(string: url) else {
fatalError("Invalid URL: \(url)")
}
if let data = try? Data(contentsOf: parsedURL) {
if let image = UIImage(data: data) { if let image = UIImage(data: data) {
print("got the average color of image \(image.averageColor)")
return image.averageColor return image.averageColor
} else { } else {
print("failed to get image average color")
return nil return nil
} }
} }
print("failed to get image average color")
return nil return nil
} }
} }
+6 -5
View File
@@ -9,7 +9,7 @@ import SwiftUI
struct HeaderView: View { struct HeaderView: View {
@EnvironmentObject var authStore:AuthSessionStore @EnvironmentObject var authStore:AuthSessionStore
@State var averageColor: UIColor? = UIColor(NirvanaColor.teal) @State var averageColor: UIColor = UIColor(NirvanaColor.teal)
var body: some View { var body: some View {
HStack(alignment:.center) { HStack(alignment:.center) {
@@ -21,7 +21,7 @@ struct HeaderView: View {
Spacer() Spacer()
if self.authStore.sessionState == SessionState.isAuthenticated { if self.authStore.sessionState == SessionState.isAuthenticated {
RemoteImage(url: "https://avatars.githubusercontent.com/u/41487836") RemoteImage(url: self.authStore.user?.profilePictureUrl?.absoluteString ?? "https://avatars.githubusercontent.com/u/41487836")
.background(NirvanaColor.teal) .background(NirvanaColor.teal)
.aspectRatio(contentMode: .fill) .aspectRatio(contentMode: .fill)
.frame(width: 40, height: 40) .frame(width: 40, height: 40)
@@ -30,15 +30,16 @@ struct HeaderView: View {
.shadow(radius:5) .shadow(radius:5)
} }
} }
.background(Color(self.averageColor!)) .background(Color(self.averageColor))
.frame(maxWidth: .infinity) .frame(maxWidth: .infinity)
.onAppear{ .onAppear{
// set background color based on profile picture tint // set background color based on profile picture tint
if authStore.sessionState == SessionState.isAuthenticated { if authStore.sessionState == SessionState.isAuthenticated {
if let userPicUrl = authStore.user?.profilePictureUrl { if let userPicUrl = authStore.user?.profilePictureUrl {
self.averageColor = NirvanaImage.getAverageColor(url: userPicUrl) if let avgColor = NirvanaImage.getAverageColor(url: userPicUrl.absoluteString) {
self.averageColor = avgColor
}
} }
// self.averageColor = NirvanaImage.getAverageColor(url: "https://avatars.githubusercontent.com/u/41487836")
} }
} }
} }