Skip to content

Commit 64b7525

Browse files
authored
Merge pull request #1809 from nextcloud/frequent-emoji-usage
Use frequent used emojis instead of a fixed set
2 parents 3e9a014 + 98f99df commit 64b7525

File tree

8 files changed

+116
-2
lines changed

8 files changed

+116
-2
lines changed

โ€ŽNextcloudTalk.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@
261261
1FD6F83E2B87B712004048AB /* NCUserStatusExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FD6F83D2B87B712004048AB /* NCUserStatusExtensions.swift */; };
262262
1FD8AE6B2A3A216300787C16 /* UIRoomTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FD8AD8C2A3A162100787C16 /* UIRoomTest.swift */; };
263263
1FD9182928C55A73009092AB /* BGTaskHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FD9182828C55A73009092AB /* BGTaskHelper.swift */; };
264+
1FDB47F62C9C71CE00D6F423 /* TalkAccount.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FDB47F52C9C71CE00D6F423 /* TalkAccount.swift */; };
265+
1FDB47F82C9C7E3F00D6F423 /* NCDatabaseManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FDB47F72C9C7E3F00D6F423 /* NCDatabaseManager.swift */; };
264266
1FDCC3D429EBF6E700DEB39B /* AvatarImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FDCC3D329EBF6E700DEB39B /* AvatarImageView.swift */; };
265267
1FDCC3E329EC787400DEB39B /* AvatarManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F90DA0329E9A28E00E81E3D /* AvatarManager.swift */; };
266268
1FDCC3ED29EC7E6700DEB39B /* AvatarImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FDCC3D329EBF6E700DEB39B /* AvatarImageView.swift */; };
@@ -793,6 +795,8 @@
793795
1FD8AD8A2A3A162100787C16 /* NextcloudTalkUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = NextcloudTalkUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
794796
1FD8AD8C2A3A162100787C16 /* UIRoomTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIRoomTest.swift; sourceTree = "<group>"; };
795797
1FD9182828C55A73009092AB /* BGTaskHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BGTaskHelper.swift; sourceTree = "<group>"; };
798+
1FDB47F52C9C71CE00D6F423 /* TalkAccount.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TalkAccount.swift; sourceTree = "<group>"; };
799+
1FDB47F72C9C7E3F00D6F423 /* NCDatabaseManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCDatabaseManager.swift; sourceTree = "<group>"; };
796800
1FDCC3D329EBF6E700DEB39B /* AvatarImageView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AvatarImageView.swift; sourceTree = "<group>"; };
797801
1FDCC3EC29EC7DD400DEB39B /* NextcloudTalk-Bridging-Header-Extensions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NextcloudTalk-Bridging-Header-Extensions.h"; sourceTree = "<group>"; };
798802
1FDCC3EF29ECB4CE00DEB39B /* AvatarButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AvatarButton.swift; sourceTree = "<group>"; };
@@ -1636,8 +1640,10 @@
16361640
children = (
16371641
2C40281322832EED0000DDFC /* NCDatabaseManager.h */,
16381642
2C40281422832EED0000DDFC /* NCDatabaseManager.m */,
1643+
1FDB47F72C9C7E3F00D6F423 /* NCDatabaseManager.swift */,
16391644
2C4446D12658147900DF1DBC /* TalkAccount.h */,
16401645
2C4446D22658147900DF1DBC /* TalkAccount.m */,
1646+
1FDB47F52C9C71CE00D6F423 /* TalkAccount.swift */,
16411647
1F1B50452B90CDE600B0F2F4 /* TalkCapabilities.h */,
16421648
1F1B50462B90CDF800B0F2F4 /* TalkCapabilities.m */,
16431649
2C4446D6265814D100DF1DBC /* ServerCapabilities.h */,
@@ -2942,6 +2948,7 @@
29422948
2C444706265E59B100DF1DBC /* ShareConfirmationCollectionViewCell.m in Sources */,
29432949
1FCE3D552C9C189D009C68A9 /* NCChatFileControllerWrapper.swift in Sources */,
29442950
2C78EF991F80F81E008AFA74 /* NCSignalingController.m in Sources */,
2951+
1FDB47F82C9C7E3F00D6F423 /* NCDatabaseManager.swift in Sources */,
29452952
2CB304202264775E0053078A /* UIResponder+SLKAdditions.m in Sources */,
29462953
2C8E2A1B232174C20022BFC9 /* MessageSeparatorTableViewCell.m in Sources */,
29472954
1FAB2EEE2AD1BC1B001214EB /* UIControlExtensions.swift in Sources */,
@@ -2975,6 +2982,7 @@
29752982
1FE7DE302BB4598F0040EE12 /* RoomInvitationViewCell.swift in Sources */,
29762983
1F77A6222AB9EB06007B6037 /* SocketConnection.m in Sources */,
29772984
1FF136152BFB74C3006A6101 /* NCChatMessage.swift in Sources */,
2985+
1FDB47F62C9C71CE00D6F423 /* TalkAccount.swift in Sources */,
29782986
2CC1C38829C0945700C8436B /* DRCellSlideActionView.m in Sources */,
29792987
1FA732FC2966CBB7003D2103 /* CallFlowLayout.swift in Sources */,
29802988
2C78EF951F7E70EB008AFA74 /* NCPeerConnection.m in Sources */,

โ€ŽNextcloudTalk/BaseChatViewController.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2358,6 +2358,8 @@ import QuickLook
23582358
let activeAccount = NCDatabaseManager.sharedInstance().activeAccount()
23592359
self.setTemporaryReaction(reaction: reaction, withState: .adding, toMessage: message)
23602360

2361+
NCDatabaseManager.sharedInstance().increaseEmojiUsage(forEmoji: reaction, forAccount: activeAccount.accountId)
2362+
23612363
NCAPIController.sharedInstance().addReaction(reaction, toMessage: message.messageId, inRoom: self.room.token, for: activeAccount) { _, error, _ in
23622364
if error != nil {
23632365
NotificationPresenter.shared().present(text: NSLocalizedString("An error occurred while adding a reaction to a message", comment: ""), dismissAfterDelay: 5.0, includedStyle: .error)

โ€ŽNextcloudTalk/ChatViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1487,7 +1487,7 @@ import UIKit
14871487
let reactionViewPadding = 10
14881488
let emojiButtonPadding = 10
14891489
let emojiButtonSize = 48
1490-
let frequentlyUsedEmojis = ["๐Ÿ‘", "โค๏ธ", "๐Ÿ˜‚", "๐Ÿ˜…"]
1490+
let frequentlyUsedEmojis = NCDatabaseManager.sharedInstance().activeAccount().frequentlyUsedEmojis
14911491

14921492
let totalEmojiButtonWidth = frequentlyUsedEmojis.count * emojiButtonSize
14931493
let totalEmojiButtonPadding = frequentlyUsedEmojis.count * emojiButtonPadding

โ€ŽNextcloudTalk/NCDatabaseManager.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
NSString *const kTalkDatabaseFolder = @"Library/Application Support/Talk";
1818
NSString *const kTalkDatabaseFileName = @"talk.realm";
19-
uint64_t const kTalkDatabaseSchemaVersion = 68;
19+
uint64_t const kTalkDatabaseSchemaVersion = 69;
2020

2121
NSString * const kCapabilitySystemMessages = @"system-messages";
2222
NSString * const kCapabilityNotificationLevels = @"notification-levels";
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
3+
// SPDX-License-Identifier: GPL-3.0-or-later
4+
//
5+
6+
import Foundation
7+
8+
@objc public extension NCDatabaseManager {
9+
10+
func increaseEmojiUsage(forEmoji emojiString: String, forAccount accountId: String) {
11+
guard let account = NCDatabaseManager.sharedInstance().talkAccount(forAccountId: accountId) else { return }
12+
var newData: [String: Int]?
13+
14+
if let data = account.frequentlyUsedEmojisJSONString.data(using: .utf8),
15+
var emojiData = try? JSONSerialization.jsonObject(with: data) as? [String: Int] {
16+
17+
if let currentEmojiCount = emojiData[emojiString] {
18+
emojiData[emojiString] = currentEmojiCount + 1
19+
} else {
20+
emojiData[emojiString] = 1
21+
}
22+
23+
newData = emojiData
24+
} else {
25+
// No existing data, start new
26+
newData = [emojiString: 1]
27+
}
28+
29+
guard let newData, let jsonData = try? JSONSerialization.data(withJSONObject: newData),
30+
let jsonString = String(data: jsonData, encoding: .utf8)
31+
else { return }
32+
33+
let realm = RLMRealm.default()
34+
35+
try? realm.transaction {
36+
if let managedTalkAccount = TalkAccount.objects(where: "accountId = %@", account.accountId).firstObject() as? TalkAccount {
37+
managedTalkAccount.frequentlyUsedEmojisJSONString = jsonString
38+
}
39+
}
40+
}
41+
}

โ€ŽNextcloudTalk/TalkAccount.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ NS_ASSUME_NONNULL_BEGIN
4343
@property NSString *lastNotificationETag;
4444
@property NSInteger lastPendingFederationInvitationFetch;
4545
@property NSInteger pendingFederationInvitations;
46+
@property NSString *frequentlyUsedEmojisJSONString;
4647

4748
@end
4849

โ€ŽNextcloudTalk/TalkAccount.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
3+
// SPDX-License-Identifier: GPL-3.0-or-later
4+
//
5+
6+
import Foundation
7+
import SwiftyAttributes
8+
9+
@objc extension TalkAccount {
10+
11+
public var defaultEmojis: [String] {
12+
return ["๐Ÿ‘", "โค๏ธ", "๐Ÿ˜‚", "๐Ÿ˜…"]
13+
}
14+
15+
public var frequentlyUsedEmojis: [String] {
16+
guard let data = self.frequentlyUsedEmojisJSONString.data(using: .utf8),
17+
let jsonData = try? JSONSerialization.jsonObject(with: data) as? [String: Int]
18+
else { return defaultEmojis }
19+
20+
// First sort by value (the amount), then by key (the emoji)
21+
var emojis = jsonData.sorted(by: {
22+
$0.value != $1.value ?
23+
$0.value > $1.value :
24+
$0.key < $1.key
25+
}).prefix(4).map({ $0.key })
26+
27+
if emojis.count < 4 {
28+
// Fill up to 4 emojis
29+
emojis.append(contentsOf: defaultEmojis.prefix(4 - emojis.count))
30+
}
31+
32+
return emojis
33+
}
34+
}

โ€ŽNextcloudTalkTests/Unit/Chat/UnitChatViewControllerTest.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,32 @@ final class UnitChatViewControllerTest: TestBaseRealm {
6868

6969
XCTAssertEqual(NCChatMessage.allObjects().count, 0)
7070
}
71+
72+
func testFrequentlyEmojis() throws {
73+
var activeAccount = NCDatabaseManager.sharedInstance().activeAccount()
74+
XCTAssertEqual(activeAccount.frequentlyUsedEmojis, ["๐Ÿ‘", "โค๏ธ", "๐Ÿ˜‚", "๐Ÿ˜…"])
75+
76+
NCDatabaseManager.sharedInstance().increaseEmojiUsage(forEmoji: "๐Ÿ™ˆ", forAccount: activeAccount.accountId)
77+
NCDatabaseManager.sharedInstance().increaseEmojiUsage(forEmoji: "๐Ÿ™ˆ", forAccount: activeAccount.accountId)
78+
NCDatabaseManager.sharedInstance().increaseEmojiUsage(forEmoji: "๐Ÿ™ˆ", forAccount: activeAccount.accountId)
79+
activeAccount = NCDatabaseManager.sharedInstance().activeAccount()
80+
XCTAssertEqual(activeAccount.frequentlyUsedEmojis, ["๐Ÿ™ˆ", "๐Ÿ‘", "โค๏ธ", "๐Ÿ˜‚"])
81+
82+
NCDatabaseManager.sharedInstance().increaseEmojiUsage(forEmoji: "๐Ÿ‡ซ๐Ÿ‡ฎ", forAccount: activeAccount.accountId)
83+
activeAccount = NCDatabaseManager.sharedInstance().activeAccount()
84+
XCTAssertEqual(activeAccount.frequentlyUsedEmojis, ["๐Ÿ™ˆ", "๐Ÿ‡ซ๐Ÿ‡ฎ", "๐Ÿ‘", "โค๏ธ"])
85+
86+
NCDatabaseManager.sharedInstance().increaseEmojiUsage(forEmoji: "๐Ÿ‡ซ๐Ÿ‡ฎ", forAccount: activeAccount.accountId)
87+
NCDatabaseManager.sharedInstance().increaseEmojiUsage(forEmoji: "๐Ÿ‡ซ๐Ÿ‡ฎ", forAccount: activeAccount.accountId)
88+
NCDatabaseManager.sharedInstance().increaseEmojiUsage(forEmoji: "๐Ÿ‡ซ๐Ÿ‡ฎ", forAccount: activeAccount.accountId)
89+
NCDatabaseManager.sharedInstance().increaseEmojiUsage(forEmoji: "๐Ÿ‡ซ๐Ÿ‡ฎ", forAccount: activeAccount.accountId)
90+
activeAccount = NCDatabaseManager.sharedInstance().activeAccount()
91+
XCTAssertEqual(activeAccount.frequentlyUsedEmojis, ["๐Ÿ‡ซ๐Ÿ‡ฎ", "๐Ÿ™ˆ", "๐Ÿ‘", "โค๏ธ"])
92+
93+
NCDatabaseManager.sharedInstance().increaseEmojiUsage(forEmoji: "๐Ÿ˜ตโ€๐Ÿ’ซ", forAccount: activeAccount.accountId)
94+
NCDatabaseManager.sharedInstance().increaseEmojiUsage(forEmoji: "๐Ÿ˜ตโ€๐Ÿ’ซ", forAccount: activeAccount.accountId)
95+
NCDatabaseManager.sharedInstance().increaseEmojiUsage(forEmoji: "๐Ÿคทโ€โ™‚๏ธ", forAccount: activeAccount.accountId)
96+
activeAccount = NCDatabaseManager.sharedInstance().activeAccount()
97+
XCTAssertEqual(activeAccount.frequentlyUsedEmojis, ["๐Ÿ‡ซ๐Ÿ‡ฎ", "๐Ÿ™ˆ", "๐Ÿ˜ตโ€๐Ÿ’ซ", "๐Ÿคทโ€โ™‚๏ธ"])
98+
}
7199
}

0 commit comments

Comments
ย (0)