fix self rating

This commit is contained in:
cheykrym 2025-12-13 05:58:50 +03:00
parent fa10e389cd
commit 492d997892
2 changed files with 54 additions and 1 deletions

View File

@ -11,6 +11,7 @@ struct ProfileDataPayload: Decodable {
let isVerified: Bool
let stories: [JSONValue]
let profilePermissions: ProfilePermissionsPayload
let rating: Double?
private enum CodingKeys: String, CodingKey {
case userId
@ -23,6 +24,7 @@ struct ProfileDataPayload: Decodable {
case isVerified
case stories
case profilePermissions
case rating
}
init(from decoder: Decoder) throws {
@ -37,6 +39,39 @@ struct ProfileDataPayload: Decodable {
self.isVerified = try container.decode(Bool.self, forKey: .isVerified)
self.stories = try container.decodeIfPresent([JSONValue].self, forKey: .stories) ?? []
self.profilePermissions = try container.decode(ProfilePermissionsPayload.self, forKey: .profilePermissions)
let ratingPayload = try container.decodeIfPresent(ProfileRatingPayload.self, forKey: .rating)
self.rating = ratingPayload?.resolvedRating
}
}
private struct ProfileRatingPayload: Decodable {
let status: String?
let rating: Double?
private enum CodingKeys: String, CodingKey {
case rating
case status
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
status = try container.decodeIfPresent(String.self, forKey: .status)
if let doubleValue = try? container.decode(Double.self, forKey: .rating) {
rating = doubleValue
} else if let stringValue = try? container.decode(String.self, forKey: .rating),
let doubleValue = Double(stringValue) {
rating = doubleValue
} else if let intValue = try? container.decode(Int.self, forKey: .rating) {
rating = Double(intValue)
} else {
rating = nil
}
}
var resolvedRating: Double? {
guard status?.lowercased() == "fine" else { return nil }
return rating
}
}

View File

@ -376,6 +376,14 @@ struct SettingsView: View {
return formatter
}()
private static let ratingFormatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.minimumFractionDigits = 1
formatter.maximumFractionDigits = 1
return formatter
}()
@ViewBuilder
private var aboutSection: some View {
if let _ = messengerProfile {
@ -472,7 +480,17 @@ struct SettingsView: View {
}
private var ratingDisplayValue: String {
NSLocalizedString("Недоступно", comment: "Messenger settings rating unavailable")
guard let rating = messengerProfile?.rating else {
return NSLocalizedString("Недоступно", comment: "Messenger settings rating unavailable")
}
let clamped = max(0, min(5, rating))
let formatted = SettingsView.ratingFormatter.string(from: NSNumber(value: clamped))
?? String(format: "%.1f", clamped)
return String(
format: NSLocalizedString("%@ из 5", comment: "Message profile rating format"),
formatted
)
}
}