cleaning things up and getting it working with the header background
This commit is contained in:
@@ -77,14 +77,21 @@ struct RemoteImage_Previews: PreviewProvider {
|
||||
}
|
||||
|
||||
class NirvanaImage {
|
||||
static func getAverageColor(url:URL) -> UIColor? {
|
||||
if let data = try? Data(contentsOf: url) {
|
||||
static func getAverageColor(url:String) -> UIColor? {
|
||||
guard let parsedURL = URL(string: url) else {
|
||||
fatalError("Invalid URL: \(url)")
|
||||
}
|
||||
|
||||
if let data = try? Data(contentsOf: parsedURL) {
|
||||
if let image = UIImage(data: data) {
|
||||
print("got the average color of image \(image.averageColor)")
|
||||
return image.averageColor
|
||||
} else {
|
||||
print("failed to get image average color")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
print("failed to get image average color")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import SwiftUI
|
||||
|
||||
struct HeaderView: View {
|
||||
@EnvironmentObject var authStore:AuthSessionStore
|
||||
@State var averageColor: UIColor? = UIColor(NirvanaColor.teal)
|
||||
@State var averageColor: UIColor = UIColor(NirvanaColor.teal)
|
||||
|
||||
var body: some View {
|
||||
HStack(alignment:.center) {
|
||||
@@ -21,7 +21,7 @@ struct HeaderView: View {
|
||||
Spacer()
|
||||
|
||||
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)
|
||||
.aspectRatio(contentMode: .fill)
|
||||
.frame(width: 40, height: 40)
|
||||
@@ -30,15 +30,16 @@ struct HeaderView: View {
|
||||
.shadow(radius:5)
|
||||
}
|
||||
}
|
||||
.background(Color(self.averageColor!))
|
||||
.background(Color(self.averageColor))
|
||||
.frame(maxWidth: .infinity)
|
||||
.onAppear{
|
||||
// set background color based on profile picture tint
|
||||
if authStore.sessionState == SessionState.isAuthenticated {
|
||||
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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user