diff --git a/Vernissage/CoreData/ApplicationSettingsHandler.swift b/Vernissage/CoreData/ApplicationSettingsHandler.swift index 8222bc1..413a51c 100644 --- a/Vernissage/CoreData/ApplicationSettingsHandler.swift +++ b/Vernissage/CoreData/ApplicationSettingsHandler.swift @@ -31,6 +31,12 @@ class ApplicationSettingsHandler { return settings } } + + func setAccountAsDefault(accountData: AccountData) { + let defaultSettings = self.getDefaultSettings() + defaultSettings.currentAccount = accountData.id + CoreDataHandler.shared.save() + } private func createApplicationSettingsEntity() -> ApplicationSettings { let context = CoreDataHandler.shared.container.viewContext diff --git a/Vernissage/Views/HomeFeedView.swift b/Vernissage/Views/HomeFeedView.swift index 252266f..3bbf467 100644 --- a/Vernissage/Views/HomeFeedView.swift +++ b/Vernissage/Views/HomeFeedView.swift @@ -33,7 +33,7 @@ struct HomeFeedView: View { imageWidth: item.attachments().first?.metaImageWidth, imageHeight: item.attachments().first?.metaImageHeight) .environmentObject(applicationState)) { - ImageRow(status: item) + ImageRow(statusData: item) } .buttonStyle(EmptyButtonStyle()) } diff --git a/Vernissage/Views/MainView.swift b/Vernissage/Views/MainView.swift index b633364..c511562 100644 --- a/Vernissage/Views/MainView.swift +++ b/Vernissage/Views/MainView.swift @@ -135,6 +135,7 @@ struct MainView: View { ForEach(self.dbAccounts) { account in Button { self.applicationState.accountData = account + ApplicationSettingsHandler.shared.setAccountAsDefault(accountData: account) } label: { if self.applicationState.accountData?.id == account.id { Label(account.displayName ?? account.acct, systemImage: "checkmark") diff --git a/Vernissage/Widgets/ImageRow.swift b/Vernissage/Widgets/ImageRow.swift index 559fe30..c8d4105 100644 --- a/Vernissage/Widgets/ImageRow.swift +++ b/Vernissage/Widgets/ImageRow.swift @@ -8,17 +8,39 @@ import SwiftUI struct ImageRow: View { @State public var status: StatusData + + @State private var imageHeight: Double + @State private var imageWidth: Double - @State private var imageHeight = UIScreen.main.bounds.width - @State private var imageWidth = UIScreen.main.bounds.width + private let uiImage:UIImage? + private let attachmentData: AttachmentData? + + init(statusData: StatusData) { + self.status = statusData + self.attachmentData = statusData.attachments().first + + if let attachmenData = self.attachmentData, let uiImage = UIImage(data: attachmenData.data) { + self.uiImage = uiImage + + let imgHeight = uiImage.size.height + let imgWidth = uiImage.size.width + let divider = imgWidth / UIScreen.main.bounds.size.width + let calculatedHeight = imgHeight / divider + + self.imageWidth = imgWidth + self.imageHeight = (calculatedHeight > 0 && calculatedHeight < .infinity) ? calculatedHeight : UIScreen.main.bounds.width + } else { + self.uiImage = nil + self.imageHeight = UIScreen.main.bounds.width + self.imageWidth = UIScreen.main.bounds.width + } + } var body: some View { - if let attachmenData = self.status.attachments().first, - let uiImage = UIImage(data: attachmenData.data) { - + if let uiImage, let attachmentData { ZStack { if self.status.sensitive { - ContentWarning(blurhash: attachmenData.blurhash, spoilerText: self.status.spoilerText) { + ContentWarning(blurhash: attachmentData.blurhash, spoilerText: self.status.spoilerText) { Image(uiImage: uiImage) .resizable() .aspectRatio(contentMode: .fit) @@ -42,23 +64,8 @@ struct ImageRow: View { } } .frame(width: self.imageWidth, height: self.imageHeight) - .onAppear { - self.recalculateSizeOfDownloadedImage(uiImage: uiImage) - } } } - - private func recalculateSizeOfDownloadedImage(uiImage: UIImage) { - let imgHeight = uiImage.size.height - let imgWidth = uiImage.size.width - let calculatedHeight = self.calculateHeight(width: imgWidth, height: imgHeight) - self.imageHeight = (calculatedHeight > 0 && calculatedHeight < .infinity) ? calculatedHeight : UIScreen.main.bounds.width - } - - private func calculateHeight(width: Double, height: Double) -> CGFloat { - let divider = width / UIScreen.main.bounds.size.width - return height / divider - } } struct ImageRow_Previews: PreviewProvider {