refactor: change settings to use a filename instead

This commit is contained in:
Jonathan Rainville 2020-09-15 15:47:13 -04:00 committed by Iuri Matias
parent 23ca5f9930
commit db9472bc0b
14 changed files with 115 additions and 63 deletions

View File

@ -33,7 +33,6 @@ 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

View File

@ -0,0 +1,26 @@
import NimQml, chronicles
import ../../status/signals/types
import ../../status/[status, node, network]
import ../../status/libstatus/types as status_types
import view
logScope:
topics = "utils"
type UtilsController* = ref object
status*: Status
view*: UtilsView
variant*: QVariant
proc newController*(status: Status): UtilsController =
result = UtilsController()
result.status = status
result.view = newUtilsView(status)
result.variant = newQVariant(result.view)
proc delete*(self: UtilsController) =
delete self.variant
delete self.view
proc init*(self: UtilsController) =
discard

View File

@ -0,0 +1,29 @@
import NimQml, os
import ../../status/status
import ../../status/libstatus/accounts/constants as accountConstants
QtObject:
type UtilsView* = ref object of QObject
status*: Status
proc setup(self: UtilsView) =
self.QObject.setup
proc delete*(self: UtilsView) =
self.QObject.delete
proc newUtilsView*(status: Status): UtilsView =
new(result, delete)
result = UtilsView()
result.status = status
result.setup
proc getDataDir*(self: UtilsView): string {.slot.} =
result = accountConstants.DATADIR
proc joinPath*(self: UtilsView, start: string, ending: string): string {.slot.} =
result = os.joinPath(start, ending)
proc join3Paths*(self: UtilsView, start: string, middle: string, ending: string): string {.slot.} =
result = os.joinPath(start, middle, ending)

View File

@ -3,6 +3,7 @@ import NimQml, eventemitter, chronicles, os, strformat
import app/chat/core as chat
import app/wallet/core as wallet
import app/node/core as node
import app/utilsView/core as utilsView
import app/profile/core as profile
import app/onboarding/core as onboarding
import app/login/core as login
@ -65,6 +66,9 @@ proc mainProc() =
var node = node.newController(status)
engine.setRootContextProperty("nodeModel", node.variant)
var utilsController = utilsView.newController(status)
engine.setRootContextProperty("utilsModel", utilsController.variant)
proc changeLanguage(locale: string) =
engine.setTranslationPackage(joinPath(i18nPath, fmt"qml_{locale}.qm"))
@ -84,6 +88,7 @@ proc mainProc() =
profile.init(args.account)
wallet.init()
chat.init()
utilsController.init()
wallet.checkPendingTransactions()
wallet.start()
@ -105,6 +110,7 @@ proc mainProc() =
wallet.delete()
chat.delete()
profile.delete()
utilsController.delete()
# Initialize only controllers whose init functions

View File

@ -9,8 +9,14 @@ SplitView {
id: chatView
handle: SplitViewHandle {}
Component.onCompleted: this.restoreState(settings.chatSplitView)
Component.onDestruction: settings.chatSplitView = this.saveState()
Connections {
target: appMain
onSettingsLoaded: {
// Add recent
chatView.restoreState(appSettings.chatSplitView)
}
}
Component.onDestruction: appSettings.chatSplitView = this.saveState()
ContactsColumn {
id: contactsColumn

View File

@ -58,11 +58,11 @@ Popup {
})
})
if (recentEmojis.length > MAX_EMOJI_NUMBER) {
//remove last one
// remove last one
recentEmojis.splice(MAX_EMOJI_NUMBER - 1)
}
emojiSectionsRepeater.itemAt(0).allEmojis = recentEmojis
changeSetting("recentEmojis", recentEmojis)
appSettings.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()
@ -81,11 +81,6 @@ Popup {
newCategories[categoryNames[emoji.category]].push(Object.assign({}, emoji, {filename: emoji.unicode + '.png'}))
})
// Add recent
appSettings.recentEmojis.forEach(function (emoji) {
newCategories[categoryNames.recent].push(Object.assign({}, emoji, {category: "recent"}))
})
if (newCategories[categoryNames.recent].length === 0) {
newCategories[categoryNames.recent].push({
category: "recent",
@ -95,6 +90,16 @@ Popup {
categories = newCategories
}
Connections {
target: appMain
onSettingsLoaded: {
// Add recent
if (!appSettings.recentEmojis || !appSettings.recentEmojis.length) {
return
}
emojiSectionsRepeater.itemAt(0).allEmojis = appSettings.recentEmojis
}
}
onOpened: {
searchBox.forceActiveFocus(Qt.MouseFocusReason)

View File

@ -14,8 +14,14 @@ SplitView {
handle: SplitViewHandle {}
Component.onCompleted: this.restoreState(settings.profileSplitView)
Component.onDestruction: settings.profileSplitView = this.saveState()
Connections {
target: appMain
onSettingsLoaded: {
// Add recent
profileView.restoreState(appSettings.profileSplitView)
}
}
Component.onDestruction: appSettings.profileSplitView = this.saveState()
LeftTab {
id: leftTab

View File

@ -37,7 +37,7 @@ Item {
Switch {
checked: appSettings.walletEnabled
onCheckedChanged: function(value) {
changeSetting("walletEnabled", this.checked)
appSettings.walletEnabled = this.checked
}
}
StyledText {

View File

@ -62,7 +62,7 @@ Item {
Switch {
checked: compactModeSetting.isCompactMode
onToggled: function() {
changeSetting("compactMode", !compactModeSetting.isCompactMode)
appSettings.compactMode = !compactModeSetting.isCompactMode
}
}
}

View File

@ -62,7 +62,7 @@ Item {
onTriggered: function () {
const locale = Locales_JSON.locales[index]
profileModel.changeLocale(locale)
changeSetting("locale", locale)
appSettings.locale = locale
}
StyledText {

View File

@ -96,7 +96,7 @@ Item {
Switch {
checked: appSettings.displayChatImages
onCheckedChanged: function (value) {
changeSetting("displayChatImages", this.checked)
appSettings.displayChatImages = this.checked
}
}
StyledText {

View File

@ -12,8 +12,14 @@ SplitView {
handle: SplitViewHandle {}
Component.onCompleted: this.restoreState(settings.walletSplitView)
Component.onDestruction: settings.walletSplitView = this.saveState()
Connections {
target: appMain
onSettingsLoaded: {
// Add recent
walletView.restoreState(appSettings.walletSplitView)
}
}
Component.onDestruction: appSettings.walletSplitView = this.saveState()
LeftTab {
id: leftTab

View File

@ -6,56 +6,23 @@ import "../shared"
import "./AppLayouts"
RowLayout {
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
id: appMain
Layout.fillHeight: true
Layout.fillWidth: true
signal settingsLoaded()
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]
appSettings.fileName = utilsModel.join3Paths(utilsModel.getDataDir(), 'qt', profileModel.profile.address)
settingsLoaded()
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
}
@ -126,7 +93,7 @@ RowLayout {
TabButton {
id: walletBtn
enabled: isExperimental === "1" || rowLayout.appSettings.walletEnabled
enabled: isExperimental === "1" || appSettings.walletEnabled
visible: this.enabled
width: 40
height: this.enabled ? 40 : 0

View File

@ -57,12 +57,16 @@ ApplicationWindow {
}
Settings {
id: settings
property var userSettings
// Those can't be in the user settings as they are bytes that JS can't handle
id: appSettings
fileName: "data/qt/unknownUser"
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 {
@ -188,9 +192,7 @@ ApplicationWindow {
Component {
id: app
AppMain {
mainSettings: settings
}
AppMain {}
}
Component {