-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use frequent used emojis instead of a fixed set
Signed-off-by: Marcel Mรผller <[email protected]>
- Loading branch information
1 parent
ceba1a9
commit ba43555
Showing
8 changed files
with
116 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
// | ||
|
||
import Foundation | ||
|
||
@objc public extension NCDatabaseManager { | ||
|
||
func increaseEmojiUsage(forEmoji emojiString: String, forAccount accountId: String) { | ||
guard let account = NCDatabaseManager.sharedInstance().talkAccount(forAccountId: accountId) else { return } | ||
var newData: [String: Int]? | ||
|
||
if let data = account.frequentlyUsedEmojisJSONString.data(using: .utf8), | ||
var emojiData = try? JSONSerialization.jsonObject(with: data) as? [String: Int] { | ||
|
||
if let currentEmojiCount = emojiData[emojiString] { | ||
emojiData[emojiString] = currentEmojiCount + 1 | ||
} else { | ||
emojiData[emojiString] = 1 | ||
} | ||
|
||
newData = emojiData | ||
} else { | ||
// No existing data, start new | ||
newData = [emojiString: 1] | ||
} | ||
|
||
guard let newData, let jsonData = try? JSONSerialization.data(withJSONObject: newData), | ||
let jsonString = String(data: jsonData, encoding: .utf8) | ||
else { return } | ||
|
||
let realm = RLMRealm.default() | ||
|
||
try? realm.transaction { | ||
if let managedTalkAccount = TalkAccount.objects(where: "accountId = %@", account.accountId).firstObject() as? TalkAccount { | ||
managedTalkAccount.frequentlyUsedEmojisJSONString = jsonString | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
// | ||
|
||
import Foundation | ||
import SwiftyAttributes | ||
|
||
@objc extension TalkAccount { | ||
|
||
public var defaultEmojis: [String] { | ||
return ["๐", "โค๏ธ", "๐", "๐ "] | ||
} | ||
|
||
public var frequentlyUsedEmojis: [String] { | ||
guard let data = self.frequentlyUsedEmojisJSONString.data(using: .utf8), | ||
let jsonData = try? JSONSerialization.jsonObject(with: data) as? [String: Int] | ||
else { return defaultEmojis } | ||
|
||
// First sort by value (the amount), then by key (the emoji) | ||
var emojis = jsonData.sorted(by: { | ||
$0.value != $1.value ? | ||
$0.value > $1.value : | ||
$0.key < $1.key | ||
}).prefix(4).map({ $0.key }) | ||
|
||
if emojis.count < 4 { | ||
// Fill up to 4 emojis | ||
emojis.append(contentsOf: defaultEmojis.prefix(4 - emojis.count)) | ||
} | ||
|
||
return emojis | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters