Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Modules/Sources/AppFeature/AppFeature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import ToastClient
import Combine
import SearchResultFeature
import CacheClient
import QMSClient
import DeviceSpecificationsFeature
import DeviceTypeFeature
import MoreFeature
Expand Down Expand Up @@ -176,6 +177,8 @@ public struct AppFeature: Reducer, Sendable {
@Dependency(\.logger[.app]) private var logger
@Dependency(\.apiClient) private var apiClient
@Dependency(\.cacheClient) private var cacheClient
@Dependency(\.qmsStore) private var qmsStore
@Dependency(\.qmsListStore) private var qmsListStore
@Dependency(\.toastClient) private var toastClient
@Dependency(\.hapticClient) private var hapticClient
@Dependency(\.analyticsClient) private var analyticsClient
Expand Down Expand Up @@ -403,6 +406,8 @@ public struct AppFeature: Reducer, Sendable {
state.favoritesBadges = 0
state.profileBadges = 0
return .run { _ in
await qmsStore.removeAll()
await qmsListStore.removeAll()
notificationsClient.setNotificationContext(context: nil)
await notificationsClient.removeNotifications(
categories: [.qmsMessage, .newTopic, .newPost, .forumMention, .siteMention]
Expand Down
7 changes: 5 additions & 2 deletions Modules/Sources/BBPanelFeature/BBPanelView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ public struct BBPanelView: View {

@State private var selectedColor: Color = .clear

private let backgroundColor: Color?

// MARK: - Init

public init(store: StoreOf<BBPanelFeature>) {
public init(store: StoreOf<BBPanelFeature>, backgroundColor: Color? = nil) {
self.store = store
self.backgroundColor = backgroundColor
}

// MARK: - Body
Expand Down Expand Up @@ -77,7 +80,7 @@ public struct BBPanelView: View {
}
.background {
RoundedRectangle(cornerRadius: isLiquidGlass ? 28 : 14)
.fill(backgroundColors.primary)
.fill(backgroundColor ?? backgroundColors.primary)
}
.onAppear {
send(.onAppear)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ extension NotificationsClient: DependencyKey {
eventSubject.send(.qms(notification.id))

case 2:
// Last api request was the chat of this message
// Last api request was the chat of this message, so it's most likely open
// and only has to load the new message, no banner needed
eventSubject.send(.qms(notification.id))
// No need to mark it processed to avoid unread sync
return false

Expand Down
86 changes: 86 additions & 0 deletions Modules/Sources/ParsingClient/Parsers/QMS/JSONValue.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//
// JSONValue.swift
// ForPDA
//
// Created by va1mont on 05.12.2026.
//

import Foundation

/// Lets `Decodable` responses hand nested parts over to the older `[Any]` based parsers.
enum JSONValue: Decodable, Equatable, Sendable {
case null
case bool(Bool)
case int(Int)
case double(Double)
case string(String)
case array([JSONValue])
case object([String: JSONValue])

init(from decoder: any Decoder) throws {
let container = try decoder.singleValueContainer()
if container.decodeNil() {
self = .null
} else if let value = try? container.decode(Int.self) {
self = .int(value)
} else if let value = try? container.decode(Double.self) {
self = .double(value)
} else if let value = try? container.decode(Bool.self) {
self = .bool(value)
} else if let value = try? container.decode(String.self) {
self = .string(value)
} else if let value = try? container.decode([JSONValue].self) {
self = .array(value)
} else {
self = .object(try container.decode([String: JSONValue].self))
}
}

/// Same shape as `JSONSerialization` produces
var anyValue: Any {
switch self {
case .null: return NSNull()
case let .bool(value): return value
case let .int(value): return value
case let .double(value): return value
case let .string(value): return value
case let .array(value): return value.map(\.anyValue)
case let .object(value): return value.mapValues(\.anyValue)
}
}
}

struct SkippedValue: Decodable {
init(from decoder: any Decoder) throws {
_ = try decoder.singleValueContainer().decode(JSONValue.self)
}
}

extension UnkeyedDecodingContainer {
mutating func skip(_ count: Int = 1) throws {
for _ in 0..<count {
_ = try decode(SkippedValue.self)
}
}

mutating func decodeDate() throws -> Date {
Date(timeIntervalSince1970: try decode(TimeInterval.self))
}

mutating func decodeURL() throws -> URL? {
(try decodeIfPresent(String.self)).flatMap { URL(string: $0) }
}
}

extension JSONDecoder {
static func decodeResponse<T: Decodable>(_ type: T.Type, from string: String) throws -> T {
guard let data = string.data(using: .utf8) else {
throw ParsingError.failedToCreateDataFromString
}
do {
return try JSONDecoder().decode(type, from: data)
} catch {
throw ParsingError.failedToSerializeData(error)
}
}
}
143 changes: 81 additions & 62 deletions Modules/Sources/ParsingClient/Parsers/QMSListParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,88 +10,107 @@ import Models

public struct QMSUserParser {
public static func parse(from string: String) throws -> QMSUser {
let users = try QMSListParser.parseUserRows(from: string)
guard let user = users.first else {
let response = try JSONDecoder.decodeResponse(QMSUsersResponse.self, from: string)
guard let user = response.users.first else {
throw ParsingError.failedToCastFields
}
return try QMSListParser.parseUser(user, decodeName: false)
return user.makeUser(decodeName: false)
}
}

public struct QMSListParser {
public static func parse(from string: String) throws -> QMSList {
let rows = try parseUserRows(from: string)
let users = try rows.map { try parseUser($0, decodeName: true) }
return QMSList(users: users)
let response = try JSONDecoder.decodeResponse(QMSUsersResponse.self, from: string)
return QMSList(users: response.users.map { $0.makeUser(decodeName: true) })
}
}

fileprivate static func parseUserRows(from string: String) throws -> [[Any]] {
guard let data = string.data(using: .utf8) else {
throw ParsingError.failedToCreateDataFromString
}

let json: Any
do {
json = try JSONSerialization.jsonObject(with: data)
} catch {
throw ParsingError.failedToSerializeData(error)
}
/**
Dialogs list or single user info response
0. response header
1. response header
2. users, see `QMSUserResponse`
*/
struct QMSUsersResponse: Decodable {
let users: [QMSUserResponse]

guard let array = json as? [Any] else {
throw ParsingError.failedToCastDataToAny
}
guard let users = array[safe: 2] as? [[Any]] else {
throw ParsingError.failedToCastFields
}
return users
init(from decoder: any Decoder) throws {
var container = try decoder.unkeyedContainer()
try container.skip(2)
users = try container.decode([QMSUserResponse].self)
}
}

fileprivate static func parseUser(_ user: [Any], decodeName: Bool) throws -> QMSUser {
guard let userId = user[safe: 0] as? Int,
let name = user[safe: 1] as? String,
let flag = user[safe: 2] as? Int,
let avatar = user[safe: 3],
avatar is String || avatar is NSNull,
let lastSeenOnline = user[safe: 4] as? TimeInterval,
let lastMessageDate = user[safe: 5] as? TimeInterval,
let unreadCount = user[safe: 6] as? Int,
let chats = user[safe: 7] as? [[Any]] else {
throw ParsingError.failedToCastFields
}
/**
Dialog partner
0. user id
1. name
2. flag
3. avatar url, can be null
4. last seen online date
5. last message date
6. unread messages count
7. dialogs, see `QMSChatInfoResponse`
*/
struct QMSUserResponse: Decodable {
let userId: Int
let name: String
let flag: Int
let avatarUrl: URL?
let lastSeenOnline: Date
let lastMessageDate: Date
let unreadCount: Int
let chats: [QMSChatInfo]

init(from decoder: any Decoder) throws {
var container = try decoder.unkeyedContainer()
userId = try container.decode(Int.self)
name = try container.decode(String.self)
flag = try container.decode(Int.self)
avatarUrl = try container.decodeURL()
lastSeenOnline = try container.decodeDate()
lastMessageDate = try container.decodeDate()
unreadCount = try container.decode(Int.self)
chats = try container.decode([QMSChatInfoResponse].self).map(\.chat)
}

return QMSUser(
func makeUser(decodeName: Bool) -> QMSUser {
QMSUser(
userId: userId,
name: decodeName ? name.convertCodes() : name,
flag: flag,
avatarUrl: (avatar as? String).flatMap { URL(string: $0) },
lastSeenOnline: Date(timeIntervalSince1970: lastSeenOnline),
lastMessageDate: Date(timeIntervalSince1970: lastMessageDate),
avatarUrl: avatarUrl,
lastSeenOnline: lastSeenOnline,
lastMessageDate: lastMessageDate,
unreadCount: unreadCount,
chats: try parseChats(chats)
chats: chats
)
}
}

public static func parseChats(_ array: [[Any]]) throws -> [QMSChatInfo] {
return try array.map { chat in
guard let id = chat[safe: 0] as? Int,
let creationDate = chat[safe: 1] as? TimeInterval,
let lastMessageDate = chat[safe: 2] as? TimeInterval,
let name = chat[safe: 3] as? String,
let totalCount = chat[safe: 4] as? Int,
let unreadCount = chat[safe: 5] as? Int,
let lastMessageId = chat[safe: 6] as? Int else {
throw ParsingError.failedToCastFields
}
/**
Dialog in list
0. dialog id
1. creation date
2. last message date
3. name
4. total messages count
5. unread messages count
6. last message id
*/
struct QMSChatInfoResponse: Decodable {
let chat: QMSChatInfo

return QMSChatInfo(
id: id,
creationDate: Date(timeIntervalSince1970: creationDate),
lastMessageDate: Date(timeIntervalSince1970: lastMessageDate),
name: name,
totalCount: totalCount,
unreadCount: unreadCount,
lastMessageId: lastMessageId
)
}
init(from decoder: any Decoder) throws {
var container = try decoder.unkeyedContainer()
chat = QMSChatInfo(
id: try container.decode(Int.self),
creationDate: try container.decodeDate(),
lastMessageDate: try container.decodeDate(),
name: try container.decode(String.self),
totalCount: try container.decode(Int.self),
unreadCount: try container.decode(Int.self),
lastMessageId: try container.decode(Int.self)
)
}
}
Loading