Skip to content

Commit

Permalink
add useAbsoluteTime option
Browse files Browse the repository at this point in the history
  • Loading branch information
max-potapov committed May 27, 2023
1 parent 9367cc8 commit fb6a36a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
9 changes: 9 additions & 0 deletions iLepra/Extras/LepraFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,14 @@ final class LepraFormatter {
return formatter
}()

lazy var absoluteDateTimeFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar.timeZone = .current
formatter.locale = .init(identifier: "ru")
formatter.dateStyle = .short
formatter.timeStyle = .short
return formatter
}()

private init() {}
}
6 changes: 4 additions & 2 deletions iLepra/Models/API/LepraUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ struct LepraUser: Codable, Identifiable, Hashable {
return "\(prefix)чётни\(suffix)"
}

func wroteOnceText(when: Date) -> AttributedString {
func wroteOnceText(when: Date, useAbsoluteTime: Bool) -> AttributedString {
let text = [
gender == .male ? "Написал" : "Написала",
rank,
deleted ? "~~`\(login)`~~" : "`\(login)`",
LepraFormatter.shared.relativeDateTimeFormatter.localizedString(for: when, relativeTo: .now),
useAbsoluteTime
? LepraFormatter.shared.absoluteDateTimeFormatter.string(from: when)
: LepraFormatter.shared.relativeDateTimeFormatter.localizedString(for: when, relativeTo: .now),
]
.compactMap { $0 }
.joined(separator: " ")
Expand Down
12 changes: 9 additions & 3 deletions iLepra/Screens/Comments/LepraCommentsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SwiftUI

struct LepraCommentsView: View {
@Environment(\.openWindow) var openWindow
@AppStorage("useAbsoluteTime") private var useAbsoluteTime: Bool?

@StateObject private var viewModel: LepraCommentsViewModel = .init()
@StateObject private var votesDetailsViewModel: LepraVotesDetailsViewModel = .init()
Expand Down Expand Up @@ -63,9 +64,14 @@ struct LepraCommentsView: View {
} showAction: { id in
showVotes(for: id)
}
Text("\(comment.user.wroteOnceText(when: comment.created))")
.font(.footnote)
.frame(maxWidth: .infinity, alignment: .leading)
Text(
comment.user.wroteOnceText(
when: comment.created,
useAbsoluteTime: useAbsoluteTime ?? false
)
)
.font(.footnote)
.frame(maxWidth: .infinity, alignment: .leading)
}
}
.badge(comment.unread ? "" : "")
Expand Down
3 changes: 2 additions & 1 deletion iLepra/Views/LepraPostSectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import SwiftUI

struct LepraPostSectionView: View {
@AppStorage("useAbsoluteTime") private var useAbsoluteTime: Bool?
@StateObject private var viewModel: LepraVotesDetailsViewModel = .init()
@Environment(\.dynamicTypeSize) private var dynamicTypeSize
@Binding var navigationPath: NavigationPath
Expand Down Expand Up @@ -38,7 +39,7 @@ struct LepraPostSectionView: View {

Divider()

Text("\(post.user.wroteOnceText(when: post.created))")
Text(post.user.wroteOnceText(when: post.created, useAbsoluteTime: useAbsoluteTime ?? false))
.font(.footnote)

layout {
Expand Down

0 comments on commit fb6a36a

Please sign in to comment.