mirror of
https://github.com/lux-tel/session-ios.git
synced 2026-10-30 20:38:32 +00:00
The `SessionThreadViewModel` has been renamed to `ConversationInfoViewModel` and is now a simple struct (as opposed to a FetchableRecord with a handful of behemoth database queries) The `ConversationInfoViewModel` and `MessageViewModel` now get initialised with a `ConversationDataCache` which is an object that holds a bunch of [ID -> Data] stores for various types that are needed to construct the aforementioned view models. The `ConversationDataCache` will generally be populated via the `ConversationDataHelper` which, when combined with our `ObservableEvent` system, is where the majority of the work for this refactoring has gone. This helper essentially has 4 parts: - `determineFetchRequirements`: Reviews any events which have come through and determines whether anything needs to be fetched from the database - `applyNonDatabaseEvents`: Updates the cached data directly based on delta information included in the `ObservableEvent` instances - `fetchFromDatabase`: Fetches any data from the database based on the info returned from `determineFetchRequirements`, this function will also resolve any dependant data that newly fetched objects need if they aren't already in the cache - `fetchFromLibSession`: Retrieves requested data from libSession Using the above setup all screens which contain conversation or message data now use these two view models, and can share the state of the view models between each other to reduce the need to refetch data when entering a new screen Some additional changes included due to the above are: • No longer need to pass `displayNameRetriever` throughout • Removed deprecated display name retrieval functions • Removed some duplicate code • Removed some synchronous database query hacks we needed until refactoring had been completed • Refactored mentions to be handled by tags • Refactored and optimised the searching logic (also added some caching) • Refactored the ThreadSettingsViewModel to use the new ObservationBuilder system • Refactored the ThreadPickerViewModel to use the new ObservationBuilder system • Refactored the HomeViewModel to retrieve data using the `ConversationDataHelper` • Refactored the ConversationViewModel to retrieve data using the `ConversationDataHelper` • Fixed an issue where communities weren't getting the correct "joined" timestamp
141 lines
4.9 KiB
Swift
141 lines
4.9 KiB
Swift
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
|
|
|
import Foundation
|
|
import GRDB
|
|
import DifferenceKit
|
|
import SessionMessagingKit
|
|
import SessionUtilitiesKit
|
|
|
|
public struct EmojiWithSkinTones: Hashable, Equatable, ContentEquatable, ContentIdentifiable {
|
|
let baseEmoji: Emoji?
|
|
let skinTones: [Emoji.SkinTone]?
|
|
let unsupportedValue: String?
|
|
|
|
init(baseEmoji: Emoji, skinTones: [Emoji.SkinTone]? = nil) {
|
|
self.baseEmoji = baseEmoji
|
|
|
|
// Deduplicate skin tones, while preserving order. This allows for
|
|
// multi-skin tone emoji, where if you have for example the permutation
|
|
// [.dark, .dark], it is consolidated to just [.dark], to be initialized
|
|
// with either variant and result in the correct emoji.
|
|
self.skinTones = skinTones?.reduce(into: [Emoji.SkinTone]()) { result, skinTone in
|
|
guard !result.contains(skinTone) else { return }
|
|
result.append(skinTone)
|
|
}
|
|
self.unsupportedValue = nil
|
|
}
|
|
|
|
init(unsupportedValue: String) {
|
|
self.unsupportedValue = unsupportedValue
|
|
self.baseEmoji = nil
|
|
self.skinTones = nil
|
|
}
|
|
|
|
var rawValue: String {
|
|
if let baseEmoji = baseEmoji {
|
|
if let skinTones = skinTones {
|
|
return baseEmoji.emojiPerSkinTonePermutation?[skinTones] ?? baseEmoji.rawValue
|
|
} else {
|
|
return baseEmoji.rawValue
|
|
}
|
|
}
|
|
if let unsupportedValue = unsupportedValue {
|
|
return unsupportedValue
|
|
}
|
|
return "" // Should not happen
|
|
}
|
|
|
|
var normalized: EmojiWithSkinTones {
|
|
if let baseEmoji = baseEmoji, baseEmoji.normalized != baseEmoji {
|
|
return EmojiWithSkinTones(baseEmoji: baseEmoji.normalized)
|
|
}
|
|
return self
|
|
}
|
|
|
|
var isNormalized: Bool { self == normalized }
|
|
}
|
|
|
|
extension Emoji {
|
|
static func getRecent(_ db: ObservingDatabase, withDefaultEmoji: Bool) throws -> [String] {
|
|
let recentReactionEmoji: [String] = (db[.recentReactionEmoji]?
|
|
.components(separatedBy: ","))
|
|
.defaulting(to: [])
|
|
|
|
// No need to continue if we don't want the default emoji to pad out the list
|
|
guard withDefaultEmoji else { return recentReactionEmoji }
|
|
|
|
// Add in our default emoji if desired
|
|
let defaultEmoji = ["😂", "🥰", "😢", "😡", "😮", "😈"] // stringlint:ignore
|
|
.filter { !recentReactionEmoji.contains($0) }
|
|
|
|
return Array(recentReactionEmoji
|
|
.appending(contentsOf: defaultEmoji)
|
|
.prefix(6))
|
|
}
|
|
|
|
static func addRecent(_ db: ObservingDatabase, emoji: String) {
|
|
// Add/move the emoji to the start of the most recent list
|
|
db[.recentReactionEmoji] = (db[.recentReactionEmoji]?
|
|
.components(separatedBy: ","))
|
|
.defaulting(to: [])
|
|
.filter { $0 != emoji }
|
|
.inserting(emoji, at: 0)
|
|
.prefix(6)
|
|
.joined(separator: ",")
|
|
db.addEvent(.recentReactionsUpdated)
|
|
}
|
|
|
|
static func allSendableEmojiByCategoryWithPreferredSkinTones(_ db: ObservingDatabase) -> [Category: [EmojiWithSkinTones]] {
|
|
return Category.allCases
|
|
.reduce(into: [Category: [EmojiWithSkinTones]]()) { result, category in
|
|
result[category] = category.normalizedEmoji
|
|
.filter { $0.available }
|
|
.map { $0.withPreferredSkinTones(db) }
|
|
}
|
|
}
|
|
|
|
private func withPreferredSkinTones(_ db: ObservingDatabase) -> EmojiWithSkinTones {
|
|
guard let rawSkinTones: String = db[.emojiPreferredSkinTones(emoji: rawValue)] else {
|
|
return EmojiWithSkinTones(baseEmoji: self, skinTones: nil)
|
|
}
|
|
|
|
return EmojiWithSkinTones(
|
|
baseEmoji: self,
|
|
skinTones: rawSkinTones
|
|
.split(separator: ",")
|
|
.compactMap { SkinTone(rawValue: String($0)) }
|
|
)
|
|
}
|
|
|
|
func setPreferredSkinTones(_ db: ObservingDatabase, preferredSkinTonePermutation: [SkinTone]?) {
|
|
db[.emojiPreferredSkinTones(emoji: rawValue)] = preferredSkinTonePermutation
|
|
.map { preferredSkinTonePermutation in
|
|
preferredSkinTonePermutation
|
|
.map { $0.rawValue }
|
|
.joined(separator: ",")
|
|
}
|
|
}
|
|
|
|
init?(_ string: String) {
|
|
guard let emojiWithSkinTonePermutation = EmojiWithSkinTones(rawValue: string) else { return nil }
|
|
if let baseEmoji = emojiWithSkinTonePermutation.baseEmoji {
|
|
self = baseEmoji
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: -
|
|
|
|
extension String {
|
|
// This is slightly more accurate than String.isSingleEmoji,
|
|
// but slower.
|
|
//
|
|
// * This will reject "lone modifiers".
|
|
// * This will reject certain edge cases such as 🌈️.
|
|
var isSingleEmojiUsingEmojiWithSkinTones: Bool {
|
|
EmojiWithSkinTones(rawValue: self) != nil
|
|
}
|
|
}
|