Skip to content

Commit

Permalink
Use frequent used emojis instead of a fixed set
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Mรผller <[email protected]>
  • Loading branch information
SystemKeeper committed Sep 19, 2024
1 parent ceba1a9 commit ba43555
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 2 deletions.
8 changes: 8 additions & 0 deletions NextcloudTalk.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@
1FD6F83E2B87B712004048AB /* NCUserStatusExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FD6F83D2B87B712004048AB /* NCUserStatusExtensions.swift */; };
1FD8AE6B2A3A216300787C16 /* UIRoomTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FD8AD8C2A3A162100787C16 /* UIRoomTest.swift */; };
1FD9182928C55A73009092AB /* BGTaskHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FD9182828C55A73009092AB /* BGTaskHelper.swift */; };
1FDB47F62C9C71CE00D6F423 /* TalkAccount.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FDB47F52C9C71CE00D6F423 /* TalkAccount.swift */; };
1FDB47F82C9C7E3F00D6F423 /* NCDatabaseManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FDB47F72C9C7E3F00D6F423 /* NCDatabaseManager.swift */; };
1FDCC3D429EBF6E700DEB39B /* AvatarImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FDCC3D329EBF6E700DEB39B /* AvatarImageView.swift */; };
1FDCC3E329EC787400DEB39B /* AvatarManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F90DA0329E9A28E00E81E3D /* AvatarManager.swift */; };
1FDCC3ED29EC7E6700DEB39B /* AvatarImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FDCC3D329EBF6E700DEB39B /* AvatarImageView.swift */; };
Expand Down Expand Up @@ -786,6 +788,8 @@
1FD8AD8A2A3A162100787C16 /* NextcloudTalkUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = NextcloudTalkUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
1FD8AD8C2A3A162100787C16 /* UIRoomTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIRoomTest.swift; sourceTree = "<group>"; };
1FD9182828C55A73009092AB /* BGTaskHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BGTaskHelper.swift; sourceTree = "<group>"; };
1FDB47F52C9C71CE00D6F423 /* TalkAccount.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TalkAccount.swift; sourceTree = "<group>"; };
1FDB47F72C9C7E3F00D6F423 /* NCDatabaseManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCDatabaseManager.swift; sourceTree = "<group>"; };
1FDCC3D329EBF6E700DEB39B /* AvatarImageView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AvatarImageView.swift; sourceTree = "<group>"; };
1FDCC3EC29EC7DD400DEB39B /* NextcloudTalk-Bridging-Header-Extensions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NextcloudTalk-Bridging-Header-Extensions.h"; sourceTree = "<group>"; };
1FDCC3EF29ECB4CE00DEB39B /* AvatarButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AvatarButton.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1626,8 +1630,10 @@
children = (
2C40281322832EED0000DDFC /* NCDatabaseManager.h */,
2C40281422832EED0000DDFC /* NCDatabaseManager.m */,
1FDB47F72C9C7E3F00D6F423 /* NCDatabaseManager.swift */,
2C4446D12658147900DF1DBC /* TalkAccount.h */,
2C4446D22658147900DF1DBC /* TalkAccount.m */,
1FDB47F52C9C71CE00D6F423 /* TalkAccount.swift */,
1F1B50452B90CDE600B0F2F4 /* TalkCapabilities.h */,
1F1B50462B90CDF800B0F2F4 /* TalkCapabilities.m */,
2C4446D6265814D100DF1DBC /* ServerCapabilities.h */,
Expand Down Expand Up @@ -2927,6 +2933,7 @@
1F46CE2928E05B3200E7D88E /* ReferenceDefaultView.swift in Sources */,
2C444706265E59B100DF1DBC /* ShareConfirmationCollectionViewCell.m in Sources */,
2C78EF991F80F81E008AFA74 /* NCSignalingController.m in Sources */,
1FDB47F82C9C7E3F00D6F423 /* NCDatabaseManager.swift in Sources */,
2CB304202264775E0053078A /* UIResponder+SLKAdditions.m in Sources */,
2C8E2A1B232174C20022BFC9 /* MessageSeparatorTableViewCell.m in Sources */,
1FAB2EEE2AD1BC1B001214EB /* UIControlExtensions.swift in Sources */,
Expand Down Expand Up @@ -2960,6 +2967,7 @@
1FE7DE302BB4598F0040EE12 /* RoomInvitationViewCell.swift in Sources */,
1F77A6222AB9EB06007B6037 /* SocketConnection.m in Sources */,
1FF136152BFB74C3006A6101 /* NCChatMessage.swift in Sources */,
1FDB47F62C9C71CE00D6F423 /* TalkAccount.swift in Sources */,
2CC1C38829C0945700C8436B /* DRCellSlideActionView.m in Sources */,
1FA732FC2966CBB7003D2103 /* CallFlowLayout.swift in Sources */,
2C78EF951F7E70EB008AFA74 /* NCPeerConnection.m in Sources */,
Expand Down
2 changes: 2 additions & 0 deletions NextcloudTalk/BaseChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2358,6 +2358,8 @@ import QuickLook
let activeAccount = NCDatabaseManager.sharedInstance().activeAccount()
self.setTemporaryReaction(reaction: reaction, withState: .adding, toMessage: message)

NCDatabaseManager.sharedInstance().increaseEmojiUsage(forEmoji: reaction, forAccount: activeAccount.accountId)

NCAPIController.sharedInstance().addReaction(reaction, toMessage: message.messageId, inRoom: self.room.token, for: activeAccount) { _, error, _ in
if error != nil {
NotificationPresenter.shared().present(text: NSLocalizedString("An error occurred while adding a reaction to a message", comment: ""), dismissAfterDelay: 5.0, includedStyle: .error)
Expand Down
2 changes: 1 addition & 1 deletion NextcloudTalk/ChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,7 @@ import UIKit
let reactionViewPadding = 10
let emojiButtonPadding = 10
let emojiButtonSize = 48
let frequentlyUsedEmojis = ["๐Ÿ‘", "โค๏ธ", "๐Ÿ˜‚", "๐Ÿ˜…"]
let frequentlyUsedEmojis = NCDatabaseManager.sharedInstance().activeAccount().frequentlyUsedEmojis

let totalEmojiButtonWidth = frequentlyUsedEmojis.count * emojiButtonSize
let totalEmojiButtonPadding = frequentlyUsedEmojis.count * emojiButtonPadding
Expand Down
2 changes: 1 addition & 1 deletion NextcloudTalk/NCDatabaseManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

NSString *const kTalkDatabaseFolder = @"Library/Application Support/Talk";
NSString *const kTalkDatabaseFileName = @"talk.realm";
uint64_t const kTalkDatabaseSchemaVersion = 67;
uint64_t const kTalkDatabaseSchemaVersion = 69;

NSString * const kCapabilitySystemMessages = @"system-messages";
NSString * const kCapabilityNotificationLevels = @"notification-levels";
Expand Down
41 changes: 41 additions & 0 deletions NextcloudTalk/NCDatabaseManager.swift
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
}
}
}
}
1 change: 1 addition & 0 deletions NextcloudTalk/TalkAccount.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ NS_ASSUME_NONNULL_BEGIN
@property NSString *lastNotificationETag;
@property NSInteger lastPendingFederationInvitationFetch;
@property NSInteger pendingFederationInvitations;
@property NSString *frequentlyUsedEmojisJSONString;

@end

Expand Down
34 changes: 34 additions & 0 deletions NextcloudTalk/TalkAccount.swift
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
}
}
28 changes: 28 additions & 0 deletions NextcloudTalkTests/Unit/Chat/UnitChatViewControllerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,32 @@ final class UnitChatViewControllerTest: TestBaseRealm {

XCTAssertEqual(NCChatMessage.allObjects().count, 0)
}

func testFrequentlyEmojis() throws {
var activeAccount = NCDatabaseManager.sharedInstance().activeAccount()
XCTAssertEqual(activeAccount.frequentlyUsedEmojis, ["๐Ÿ‘", "โค๏ธ", "๐Ÿ˜‚", "๐Ÿ˜…"])

NCDatabaseManager.sharedInstance().increaseEmojiUsage(forEmoji: "๐Ÿ™ˆ", forAccount: activeAccount.accountId)
NCDatabaseManager.sharedInstance().increaseEmojiUsage(forEmoji: "๐Ÿ™ˆ", forAccount: activeAccount.accountId)
NCDatabaseManager.sharedInstance().increaseEmojiUsage(forEmoji: "๐Ÿ™ˆ", forAccount: activeAccount.accountId)
activeAccount = NCDatabaseManager.sharedInstance().activeAccount()
XCTAssertEqual(activeAccount.frequentlyUsedEmojis, ["๐Ÿ™ˆ", "๐Ÿ‘", "โค๏ธ", "๐Ÿ˜‚"])

NCDatabaseManager.sharedInstance().increaseEmojiUsage(forEmoji: "๐Ÿ‡ซ๐Ÿ‡ฎ", forAccount: activeAccount.accountId)
activeAccount = NCDatabaseManager.sharedInstance().activeAccount()
XCTAssertEqual(activeAccount.frequentlyUsedEmojis, ["๐Ÿ™ˆ", "๐Ÿ‡ซ๐Ÿ‡ฎ", "๐Ÿ‘", "โค๏ธ"])

NCDatabaseManager.sharedInstance().increaseEmojiUsage(forEmoji: "๐Ÿ‡ซ๐Ÿ‡ฎ", forAccount: activeAccount.accountId)
NCDatabaseManager.sharedInstance().increaseEmojiUsage(forEmoji: "๐Ÿ‡ซ๐Ÿ‡ฎ", forAccount: activeAccount.accountId)
NCDatabaseManager.sharedInstance().increaseEmojiUsage(forEmoji: "๐Ÿ‡ซ๐Ÿ‡ฎ", forAccount: activeAccount.accountId)
NCDatabaseManager.sharedInstance().increaseEmojiUsage(forEmoji: "๐Ÿ‡ซ๐Ÿ‡ฎ", forAccount: activeAccount.accountId)
activeAccount = NCDatabaseManager.sharedInstance().activeAccount()
XCTAssertEqual(activeAccount.frequentlyUsedEmojis, ["๐Ÿ‡ซ๐Ÿ‡ฎ", "๐Ÿ™ˆ", "๐Ÿ‘", "โค๏ธ"])

NCDatabaseManager.sharedInstance().increaseEmojiUsage(forEmoji: "๐Ÿ˜ตโ€๐Ÿ’ซ", forAccount: activeAccount.accountId)
NCDatabaseManager.sharedInstance().increaseEmojiUsage(forEmoji: "๐Ÿ˜ตโ€๐Ÿ’ซ", forAccount: activeAccount.accountId)
NCDatabaseManager.sharedInstance().increaseEmojiUsage(forEmoji: "๐Ÿคทโ€โ™‚๏ธ", forAccount: activeAccount.accountId)
activeAccount = NCDatabaseManager.sharedInstance().activeAccount()
XCTAssertEqual(activeAccount.frequentlyUsedEmojis, ["๐Ÿ‡ซ๐Ÿ‡ฎ", "๐Ÿ™ˆ", "๐Ÿ˜ตโ€๐Ÿ’ซ", "๐Ÿคทโ€โ™‚๏ธ"])
}
}

0 comments on commit ba43555

Please sign in to comment.