From 549b2126a91250a76e74a7206625fb43c0bd5dcf Mon Sep 17 00:00:00 2001 From: cheykrym Date: Thu, 11 Dec 2025 00:44:39 +0300 Subject: [PATCH] add rating --- yobble/Network/ChatModels.swift | 18 ++++++++++-------- yobble/Resources/Localizable.xcstrings | 6 +++--- yobble/Views/Chat/MessageProfileView.swift | 14 ++++++++++++-- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/yobble/Network/ChatModels.swift b/yobble/Network/ChatModels.swift index 3166def..52076b1 100644 --- a/yobble/Network/ChatModels.swift +++ b/yobble/Network/ChatModels.swift @@ -172,7 +172,7 @@ struct ChatProfile: Decodable { let permissions: ChatPermissions? let profilePermissions: ChatProfilePermissions? let relationship: RelationshipStatus? - let rating: Int? + let rating: Double? let isOfficial: Bool private enum CodingKeys: String, CodingKey { @@ -217,7 +217,7 @@ struct ChatProfile: Decodable { private struct ChatProfileRatingPayload: Decodable { let status: String? - let rating: Int? + let rating: Double? private enum CodingKeys: String, CodingKey { case rating @@ -228,17 +228,19 @@ private struct ChatProfileRatingPayload: Decodable { let container = try decoder.container(keyedBy: CodingKeys.self) status = try container.decodeIfPresent(String.self, forKey: .status) - if let intValue = try? container.decode(Int.self, forKey: .rating) { - rating = intValue + if let doubleValue = try? container.decode(Double.self, forKey: .rating) { + rating = doubleValue } else if let stringValue = try? container.decode(String.self, forKey: .rating), - let intValue = Int(stringValue) { - rating = intValue + 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: Int? { + var resolvedRating: Double? { guard status?.lowercased() == "fine" else { return nil } return rating } @@ -258,7 +260,7 @@ extension ChatProfile { permissions: ChatPermissions? = nil, profilePermissions: ChatProfilePermissions? = nil, relationship: RelationshipStatus? = nil, - rating: Int? = nil, + rating: Double? = nil, isOfficial: Bool = false ) { self.userId = userId diff --git a/yobble/Resources/Localizable.xcstrings b/yobble/Resources/Localizable.xcstrings index 0d3d713..0780033 100644 --- a/yobble/Resources/Localizable.xcstrings +++ b/yobble/Resources/Localizable.xcstrings @@ -33,6 +33,9 @@ } } }, + "%@ из 5" : { + "comment" : "Message profile rating format" + }, "%@: %@" : { "localizations" : { "ru" : { @@ -43,9 +46,6 @@ } } }, - "%d из 5" : { - "comment" : "Message profile rating format" - }, "%d символов" : { "comment" : "feedback: character count", "localizations" : { diff --git a/yobble/Views/Chat/MessageProfileView.swift b/yobble/Views/Chat/MessageProfileView.swift index df8f838..b8d7aa2 100644 --- a/yobble/Views/Chat/MessageProfileView.swift +++ b/yobble/Views/Chat/MessageProfileView.swift @@ -631,9 +631,11 @@ struct MessageProfileView: View { } let safeRating = max(0, min(5, rating)) + let formatted = MessageProfileView.ratingFormatter.string(from: NSNumber(value: safeRating)) + ?? String(format: "%.1f", safeRating) return String( - format: NSLocalizedString("%d из 5", comment: "Message profile rating format"), - safeRating + format: NSLocalizedString("%@ из 5", comment: "Message profile rating format"), + formatted ) } @@ -938,6 +940,14 @@ struct MessageProfileView: View { formatter.timeStyle = .none return formatter }() + + private static let ratingFormatter: NumberFormatter = { + let formatter = NumberFormatter() + formatter.numberStyle = .decimal + formatter.minimumFractionDigits = 1 + formatter.maximumFractionDigits = 1 + return formatter + }() private func bioFirstLines(_ text: String, count: Int) -> String { let lines = text.split(separator: "\n", omittingEmptySubsequences: false)