fix self rating
This commit is contained in:
parent
fa10e389cd
commit
492d997892
@ -11,6 +11,7 @@ struct ProfileDataPayload: Decodable {
|
|||||||
let isVerified: Bool
|
let isVerified: Bool
|
||||||
let stories: [JSONValue]
|
let stories: [JSONValue]
|
||||||
let profilePermissions: ProfilePermissionsPayload
|
let profilePermissions: ProfilePermissionsPayload
|
||||||
|
let rating: Double?
|
||||||
|
|
||||||
private enum CodingKeys: String, CodingKey {
|
private enum CodingKeys: String, CodingKey {
|
||||||
case userId
|
case userId
|
||||||
@ -23,6 +24,7 @@ struct ProfileDataPayload: Decodable {
|
|||||||
case isVerified
|
case isVerified
|
||||||
case stories
|
case stories
|
||||||
case profilePermissions
|
case profilePermissions
|
||||||
|
case rating
|
||||||
}
|
}
|
||||||
|
|
||||||
init(from decoder: Decoder) throws {
|
init(from decoder: Decoder) throws {
|
||||||
@ -37,6 +39,39 @@ struct ProfileDataPayload: Decodable {
|
|||||||
self.isVerified = try container.decode(Bool.self, forKey: .isVerified)
|
self.isVerified = try container.decode(Bool.self, forKey: .isVerified)
|
||||||
self.stories = try container.decodeIfPresent([JSONValue].self, forKey: .stories) ?? []
|
self.stories = try container.decodeIfPresent([JSONValue].self, forKey: .stories) ?? []
|
||||||
self.profilePermissions = try container.decode(ProfilePermissionsPayload.self, forKey: .profilePermissions)
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -376,6 +376,14 @@ struct SettingsView: View {
|
|||||||
return formatter
|
return formatter
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
private static let ratingFormatter: NumberFormatter = {
|
||||||
|
let formatter = NumberFormatter()
|
||||||
|
formatter.numberStyle = .decimal
|
||||||
|
formatter.minimumFractionDigits = 1
|
||||||
|
formatter.maximumFractionDigits = 1
|
||||||
|
return formatter
|
||||||
|
}()
|
||||||
|
|
||||||
@ViewBuilder
|
@ViewBuilder
|
||||||
private var aboutSection: some View {
|
private var aboutSection: some View {
|
||||||
if let _ = messengerProfile {
|
if let _ = messengerProfile {
|
||||||
@ -472,7 +480,17 @@ struct SettingsView: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private var ratingDisplayValue: String {
|
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
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user