From 23ca5f99306d765270825765eec68a28d4889388 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Tue, 15 Sep 2020 13:42:23 -0400 Subject: [PATCH] feat: make settings user dependant and clean settings uses --- src/app/profile/view.nim | 4 ++ src/app/profile/views/profile_info.nim | 1 + ui/app/AppLayouts/Chat/ChatColumn.qml | 2 - .../Chat/ChatColumn/ChatMessages.qml | 2 - ui/app/AppLayouts/Chat/ChatColumn/Message.qml | 1 - .../MessageComponents/CompactMessage.qml | 2 +- .../MessageComponents/NormalMessage.qml | 2 +- ui/app/AppLayouts/Chat/ChatLayout.qml | 7 +-- .../AppLayouts/Chat/components/EmojiPopup.qml | 14 +++--- ui/app/AppLayouts/Profile/ProfileLayout.qml | 9 ++-- .../Profile/Sections/AdvancedContainer.qml | 4 +- .../Profile/Sections/AppearanceContainer.qml | 2 +- .../Profile/Sections/LanguageContainer.qml | 2 +- .../Profile/Sections/PrivacyContainer.qml | 2 +- ui/app/AppLayouts/Wallet/WalletLayout.qml | 6 +-- ui/app/AppMain.qml | 49 +++++++++++++++++-- ui/main.qml | 14 ++---- 17 files changed, 74 insertions(+), 49 deletions(-) diff --git a/src/app/profile/view.nim b/src/app/profile/view.nim index 99e0458dc..7304a8189 100644 --- a/src/app/profile/view.nim +++ b/src/app/profile/view.nim @@ -139,11 +139,15 @@ QtObject: proc getProfile(self: ProfileView): QVariant {.slot.} = return newQVariant(self.profile) + proc profileChanged*(self: ProfileView) {.signal.} + proc setNewProfile*(self: ProfileView, profile: Profile) = self.profile.setProfile(profile) + self.profileChanged() QtProperty[QVariant] profile: read = getProfile + notify = profileChanged proc contactToAddChanged*(self: ProfileView) {.signal.} diff --git a/src/app/profile/views/profile_info.nim b/src/app/profile/views/profile_info.nim index 1234cc018..ace489f8a 100644 --- a/src/app/profile/views/profile_info.nim +++ b/src/app/profile/views/profile_info.nim @@ -33,6 +33,7 @@ QtObject: self.appearance = profile.appearance self.pubKey = profile.id self.address = profile.address + debug "Setting account", username = profile.username, pubKey = profile.id, address = profile.address self.profileChanged() proc username*(self: ProfileInfoView): string {.slot.} = result = self.username diff --git a/ui/app/AppLayouts/Chat/ChatColumn.qml b/ui/app/AppLayouts/Chat/ChatColumn.qml index 6309274f3..269c7d225 100644 --- a/ui/app/AppLayouts/Chat/ChatColumn.qml +++ b/ui/app/AppLayouts/Chat/ChatColumn.qml @@ -16,7 +16,6 @@ StackLayout { property bool isExtendedInput: isReply || isImage - property var appSettings property bool isConnected: false property string contactToRemove: "" @@ -118,7 +117,6 @@ StackLayout { ChatMessages { id: chatMessages messageList: chatsModel.messageList - appSettings: chatColumnLayout.appSettings } } diff --git a/ui/app/AppLayouts/Chat/ChatColumn/ChatMessages.qml b/ui/app/AppLayouts/Chat/ChatColumn/ChatMessages.qml index 51effed8b..6a1c20171 100644 --- a/ui/app/AppLayouts/Chat/ChatColumn/ChatMessages.qml +++ b/ui/app/AppLayouts/Chat/ChatColumn/ChatMessages.qml @@ -12,7 +12,6 @@ ScrollView { id: scrollView property var messageList: MessagesData {} - property var appSettings property bool loadingMessages: false property real scrollY: chatLogView.visibleArea.yPosition * chatLogView.contentHeight @@ -241,7 +240,6 @@ ScrollView { } return -1; } - appSettings: scrollView.appSettings scrollToBottom: chatLogView.scrollToBottom timeout: model.timeout } diff --git a/ui/app/AppLayouts/Chat/ChatColumn/Message.qml b/ui/app/AppLayouts/Chat/ChatColumn/Message.qml index 03c91a110..ebc9f143d 100644 --- a/ui/app/AppLayouts/Chat/ChatColumn/Message.qml +++ b/ui/app/AppLayouts/Chat/ChatColumn/Message.qml @@ -44,7 +44,6 @@ Item { property var profileClick: function () {} property var imageClick: function () {} property var scrollToBottom: function () {} - property var appSettings id: messageItem width: parent.width diff --git a/ui/app/AppLayouts/Chat/ChatColumn/MessageComponents/CompactMessage.qml b/ui/app/AppLayouts/Chat/ChatColumn/MessageComponents/CompactMessage.qml index b1869ad1f..6fd8dd046 100644 --- a/ui/app/AppLayouts/Chat/ChatColumn/MessageComponents/CompactMessage.qml +++ b/ui/app/AppLayouts/Chat/ChatColumn/MessageComponents/CompactMessage.qml @@ -6,7 +6,7 @@ Item { property var clickMessage: function () {} property int chatHorizontalPadding: 12 property int chatVerticalPadding: 7 - property bool showImages: messageItem.appSettings.displayChatImages && imageUrls != "" + property bool showImages: appSettings.displayChatImages && imageUrls != "" id: chatTextItem anchors.top: parent.top diff --git a/ui/app/AppLayouts/Chat/ChatColumn/MessageComponents/NormalMessage.qml b/ui/app/AppLayouts/Chat/ChatColumn/MessageComponents/NormalMessage.qml index f1a41fddd..50fd26f6b 100644 --- a/ui/app/AppLayouts/Chat/ChatColumn/MessageComponents/NormalMessage.qml +++ b/ui/app/AppLayouts/Chat/ChatColumn/MessageComponents/NormalMessage.qml @@ -4,7 +4,7 @@ import "../../../../../imports" Item { property var clickMessage: function () {} - property bool showImages: messageItem.appSettings.displayChatImages && imageUrls != "" + property bool showImages: appSettings.displayChatImages && imageUrls != "" id: chatTextItem anchors.top: parent.top diff --git a/ui/app/AppLayouts/Chat/ChatLayout.qml b/ui/app/AppLayouts/Chat/ChatLayout.qml index dcc01d471..7d3a26fbc 100644 --- a/ui/app/AppLayouts/Chat/ChatLayout.qml +++ b/ui/app/AppLayouts/Chat/ChatLayout.qml @@ -6,13 +6,11 @@ import "../../../shared" import "." SplitView { - property var appSettings - id: chatView handle: SplitViewHandle {} - Component.onCompleted: this.restoreState(appSettings.chatSplitView) - Component.onDestruction: appSettings.chatSplitView = this.saveState() + Component.onCompleted: this.restoreState(settings.chatSplitView) + Component.onDestruction: settings.chatSplitView = this.saveState() ContactsColumn { id: contactsColumn @@ -24,7 +22,6 @@ SplitView { ChatColumn { id: chatColumn chatGroupsListViewCount: contactsColumn.chatGroupsListViewCount - appSettings: chatView.appSettings } } diff --git a/ui/app/AppLayouts/Chat/components/EmojiPopup.qml b/ui/app/AppLayouts/Chat/components/EmojiPopup.qml index e6d7d7463..2eb878a6d 100644 --- a/ui/app/AppLayouts/Chat/components/EmojiPopup.qml +++ b/ui/app/AppLayouts/Chat/components/EmojiPopup.qml @@ -49,18 +49,20 @@ Popup { const encodedIcon = String.fromCodePoint(...codePointParts); // Add at the start of the list - appSettings.recentEmojis.unshift(emoji) + let recentEmojis = appSettings.recentEmojis + recentEmojis.unshift(emoji) // Remove duplicates - appSettings.recentEmojis = appSettings.recentEmojis.filter(function (e, index) { - return !appSettings.recentEmojis.some(function (e2, index2) { + recentEmojis = recentEmojis.filter(function (e, index) { + return !recentEmojis.some(function (e2, index2) { return index2 < index && e2.filename === e.filename }) }) - if (appSettings.recentEmojis.length > MAX_EMOJI_NUMBER) { + if (recentEmojis.length > MAX_EMOJI_NUMBER) { //remove last one - appSettings.recentEmojis.splice(MAX_EMOJI_NUMBER - 1) + recentEmojis.splice(MAX_EMOJI_NUMBER - 1) } - emojiSectionsRepeater.itemAt(0).allEmojis = appSettings.recentEmojis + emojiSectionsRepeater.itemAt(0).allEmojis = recentEmojis + changeSetting("recentEmojis", recentEmojis) popup.addToChat(Emoji.parse(encodedIcon, "26x26") + ' ') // Adding a space because otherwise, some emojis would fuse since it's just an emoji is just a string popup.close() diff --git a/ui/app/AppLayouts/Profile/ProfileLayout.qml b/ui/app/AppLayouts/Profile/ProfileLayout.qml index ef1939566..6a54258e6 100644 --- a/ui/app/AppLayouts/Profile/ProfileLayout.qml +++ b/ui/app/AppLayouts/Profile/ProfileLayout.qml @@ -6,7 +6,6 @@ import "../../../shared" import "./Sections" SplitView { - property var appSettings property int contentMargin: 120 id: profileView @@ -15,8 +14,8 @@ SplitView { handle: SplitViewHandle {} - Component.onCompleted: this.restoreState(appSettings.profileSplitView) - Component.onDestruction: appSettings.profileSplitView = this.saveState() + Component.onCompleted: this.restoreState(settings.profileSplitView) + Component.onDestruction: settings.profileSplitView = this.saveState() LeftTab { id: leftTab @@ -72,9 +71,7 @@ SplitView { DevicesContainer {} - AdvancedContainer { - appSettings: profileView.appSettings - } + AdvancedContainer {} HelpContainer {} diff --git a/ui/app/AppLayouts/Profile/Sections/AdvancedContainer.qml b/ui/app/AppLayouts/Profile/Sections/AdvancedContainer.qml index 37ae053aa..98ad3a275 100644 --- a/ui/app/AppLayouts/Profile/Sections/AdvancedContainer.qml +++ b/ui/app/AppLayouts/Profile/Sections/AdvancedContainer.qml @@ -5,8 +5,6 @@ import "../../../../imports" import "../../../../shared" Item { - property var appSettings - id: advancedContainer width: 200 height: 200 @@ -39,7 +37,7 @@ Item { Switch { checked: appSettings.walletEnabled onCheckedChanged: function(value) { - appSettings.walletEnabled = this.checked + changeSetting("walletEnabled", this.checked) } } StyledText { diff --git a/ui/app/AppLayouts/Profile/Sections/AppearanceContainer.qml b/ui/app/AppLayouts/Profile/Sections/AppearanceContainer.qml index 5c736e386..00bf5dc13 100644 --- a/ui/app/AppLayouts/Profile/Sections/AppearanceContainer.qml +++ b/ui/app/AppLayouts/Profile/Sections/AppearanceContainer.qml @@ -62,7 +62,7 @@ Item { Switch { checked: compactModeSetting.isCompactMode onToggled: function() { - appSettings.compactMode = !compactModeSetting.isCompactMode + changeSetting("compactMode", !compactModeSetting.isCompactMode) } } } diff --git a/ui/app/AppLayouts/Profile/Sections/LanguageContainer.qml b/ui/app/AppLayouts/Profile/Sections/LanguageContainer.qml index c9748e8e5..1a82dcbac 100644 --- a/ui/app/AppLayouts/Profile/Sections/LanguageContainer.qml +++ b/ui/app/AppLayouts/Profile/Sections/LanguageContainer.qml @@ -62,7 +62,7 @@ Item { onTriggered: function () { const locale = Locales_JSON.locales[index] profileModel.changeLocale(locale) - appSettings.locale = locale + changeSetting("locale", locale) } StyledText { diff --git a/ui/app/AppLayouts/Profile/Sections/PrivacyContainer.qml b/ui/app/AppLayouts/Profile/Sections/PrivacyContainer.qml index 8eb887444..b65040c82 100644 --- a/ui/app/AppLayouts/Profile/Sections/PrivacyContainer.qml +++ b/ui/app/AppLayouts/Profile/Sections/PrivacyContainer.qml @@ -96,7 +96,7 @@ Item { Switch { checked: appSettings.displayChatImages onCheckedChanged: function (value) { - appSettings.displayChatImages = this.checked + changeSetting("displayChatImages", this.checked) } } StyledText { diff --git a/ui/app/AppLayouts/Wallet/WalletLayout.qml b/ui/app/AppLayouts/Wallet/WalletLayout.qml index 7551c3a68..7d3e4eb38 100644 --- a/ui/app/AppLayouts/Wallet/WalletLayout.qml +++ b/ui/app/AppLayouts/Wallet/WalletLayout.qml @@ -6,16 +6,14 @@ import "../../../shared" import "." SplitView { - property var appSettings - id: walletView Layout.fillHeight: true Layout.fillWidth: true handle: SplitViewHandle {} - Component.onCompleted: this.restoreState(appSettings.walletSplitView) - Component.onDestruction: appSettings.walletSplitView = this.saveState() + Component.onCompleted: this.restoreState(settings.walletSplitView) + Component.onDestruction: settings.walletSplitView = this.saveState() LeftTab { id: leftTab diff --git a/ui/app/AppMain.qml b/ui/app/AppMain.qml index 8abfa54aa..57057a2db 100644 --- a/ui/app/AppMain.qml +++ b/ui/app/AppMain.qml @@ -6,12 +6,56 @@ import "../shared" import "./AppLayouts" RowLayout { - property var appSettings + readonly property var defaultSettings: { + return { + chatSplitView: undefined, + walletSplitView: undefined, + profileSplitView: undefined, + walletEnabled: false, + displayChatImages: false, + compactMode: false, + locale: "en", + recentEmojis: [] + } + } + property var mainSettings + property var userSettings + property var appSettings: defaultSettings + property string currentAccount id: rowLayout Layout.fillHeight: true Layout.fillWidth: true + Connections { + target: profileModel + onProfileChanged: { + currentAccount = profileModel.profile.address + + if (!mainSettings.userSettings) { + mainSettings.userSettings = {} + } + userSettings = mainSettings.userSettings + + if (!userSettings[currentAccount]) { + userSettings[currentAccount] = defaultSettings + } + appSettings = userSettings[currentAccount] + + if (appSettings.locale !== "en") { + profileModel.changeLocale(appSettings.locale) + } + } + } + + function changeSetting(name, value) { + appSettings[name] = value + appSettingsChanged() + userSettings[currentAccount] = appSettings + mainSettings.userSettings = userSettings + } + + ToastMessage { id: toastMessage } @@ -198,7 +242,6 @@ RowLayout { Layout.fillWidth: true Layout.alignment: Qt.AlignLeft | Qt.AlignTop Layout.fillHeight: true - appSettings: rowLayout.appSettings } WalletLayout { @@ -206,7 +249,6 @@ RowLayout { Layout.fillWidth: true Layout.alignment: Qt.AlignLeft | Qt.AlignTop Layout.fillHeight: true - appSettings: rowLayout.appSettings } ProfileLayout { @@ -214,7 +256,6 @@ RowLayout { Layout.fillWidth: true Layout.alignment: Qt.AlignLeft | Qt.AlignTop Layout.fillHeight: true - appSettings: rowLayout.appSettings } NodeLayout { diff --git a/ui/main.qml b/ui/main.qml index 1cee06c6b..fc039015a 100644 --- a/ui/main.qml +++ b/ui/main.qml @@ -16,7 +16,6 @@ import "./shared" import "./imports" ApplicationWindow { - property alias appSettings: settings property bool hasAccounts: !!loginModel.rowCount() id: applicationWindow @@ -34,10 +33,6 @@ ApplicationWindow { visible: true Component.onCompleted: { - if (settings.locale !== "en") { - profileModel.changeLocale(settings.locale) - } - setX(Qt.application.screens[0].width / 2 - width / 2); setY(Qt.application.screens[0].height / 2 - height / 2); } @@ -63,14 +58,11 @@ ApplicationWindow { Settings { id: settings + property var userSettings + // Those can't be in the user settings as they are bytes that JS can't handle property var chatSplitView property var walletSplitView property var profileSplitView - property bool walletEnabled: false - property bool displayChatImages: false - property bool compactMode - property string locale: "en" - property var recentEmojis: [] } SystemTrayIcon { @@ -197,7 +189,7 @@ ApplicationWindow { Component { id: app AppMain { - appSettings: applicationWindow.appSettings + mainSettings: settings } }