Add saving information about account change.

This commit is contained in:
Marcin Czachursk 2023-01-11 14:31:31 +01:00
parent ea2eb0a972
commit 3638d36d78
4 changed files with 36 additions and 22 deletions

View File

@ -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

View File

@ -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())
}

View File

@ -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")

View File

@ -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 {