Skip to content
This repository was archived by the owner on Mar 4, 2023. It is now read-only.

Commit 96826f9

Browse files
committed
added android shared preference settings accessor
1 parent 99a2df8 commit 96826f9

File tree

17 files changed

+673
-4
lines changed

17 files changed

+673
-4
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
TEMPLATE = app
2+
3+
QT += quick mvvmcore
4+
5+
TARGET = DroidSettings
6+
7+
SOURCES += \
8+
main.cpp \
9+
settings.cpp
10+
11+
RESOURCES += qml.qrc
12+
13+
target.path = $$[QT_INSTALL_EXAMPLES]/mvvmcore/$$TARGET
14+
INSTALLS += target
15+
16+
HEADERS += \
17+
settings.h
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <QGuiApplication>
2+
#include <QQmlApplicationEngine>
3+
#include <QQmlContext>
4+
#include "settings.h"
5+
6+
int main(int argc, char *argv[])
7+
{
8+
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
9+
10+
QGuiApplication app(argc, argv);
11+
12+
Settings settings;
13+
14+
QQmlApplicationEngine engine;
15+
engine.rootContext()->setContextProperty(QStringLiteral("settings"), &settings);
16+
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
17+
if (engine.rootObjects().isEmpty())
18+
return -1;
19+
20+
return app.exec();
21+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import QtQuick 2.10
2+
import QtQuick.Controls 2.3
3+
import QtQuick.Layouts 1.3
4+
5+
ApplicationWindow {
6+
visible: true
7+
width: 640
8+
height: 480
9+
title: qsTr("Android Settings Test")
10+
11+
Pane {
12+
anchors.fill: parent
13+
14+
GridLayout {
15+
anchors.fill: parent
16+
columns: 6
17+
18+
TextField {
19+
id: keyField
20+
placeholderText: qsTr("key")
21+
Layout.fillWidth: true
22+
Layout.columnSpan: 3
23+
}
24+
25+
TextField {
26+
id: valueField
27+
placeholderText: qsTr("value")
28+
Layout.fillWidth: true
29+
Layout.columnSpan: 3
30+
}
31+
32+
Button {
33+
id: addButton
34+
text: qsTr("save")
35+
Layout.fillWidth: true
36+
Layout.columnSpan: 2
37+
onClicked: settings.save(keyField.text, valueField.text)
38+
}
39+
40+
Button {
41+
id: loadButton
42+
text: qsTr("load")
43+
Layout.fillWidth: true
44+
Layout.columnSpan: 2
45+
onClicked: valueField.text = settings.load(keyField.text)
46+
}
47+
48+
Button {
49+
id: removeButton
50+
text: qsTr("remove")
51+
Layout.fillWidth: true
52+
Layout.columnSpan: 2
53+
onClicked: settings.remove(keyField.text)
54+
}
55+
56+
Label {
57+
id: eventLabel
58+
Layout.columnSpan: 2
59+
Layout.fillWidth: true
60+
Layout.fillHeight: true
61+
62+
Connections {
63+
target: settings
64+
onChangeEvent: eventLabel.text = eventLabel.text + text + "\n";
65+
}
66+
}
67+
}
68+
}
69+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<RCC>
2+
<qresource prefix="/">
3+
<file>main.qml</file>
4+
</qresource>
5+
</RCC>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "settings.h"
2+
3+
Settings::Settings(QObject *parent) :
4+
QObject{parent},
5+
_accessor{new QtMvvm::AndroidSettingsAccessor{}}
6+
{
7+
connect(_accessor, &QtMvvm::AndroidSettingsAccessor::entryChanged,
8+
this, &Settings::entryChanged);
9+
connect(_accessor, &QtMvvm::AndroidSettingsAccessor::entryRemoved,
10+
this, &Settings::entryRemoved);
11+
}
12+
13+
QString Settings::load(const QString &key)
14+
{
15+
return _accessor->load(key, tr("<unset>")).toString();
16+
}
17+
18+
void Settings::save(const QString &key, const QString &value)
19+
{
20+
_accessor->save(key, value);
21+
}
22+
23+
void Settings::remove(const QString &key)
24+
{
25+
_accessor->remove(key);
26+
}
27+
28+
void Settings::entryChanged(const QString &key, const QVariant &value)
29+
{
30+
emit changeEvent(tr("Data for key <%1> changed to: %2")
31+
.arg(key, value.toString()));
32+
}
33+
34+
void Settings::entryRemoved(const QString &key)
35+
{
36+
emit changeEvent(tr("Data for key <%1> removed").arg(key));
37+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef SETTINGS_H
2+
#define SETTINGS_H
3+
4+
#include <QObject>
5+
#include <QtMvvmCore/AndroidSettingsAccessor>
6+
7+
class Settings : public QObject
8+
{
9+
Q_OBJECT
10+
11+
public:
12+
explicit Settings(QObject *parent = nullptr);
13+
14+
Q_INVOKABLE QString load(const QString &key);
15+
16+
public Q_SLOTS:
17+
void save(const QString &key, const QString &value);
18+
void remove(const QString &key);
19+
20+
Q_SIGNALS:
21+
void changeEvent(const QString &text);
22+
23+
private Q_SLOTS:
24+
void entryChanged(const QString &key, const QVariant &value);
25+
void entryRemoved(const QString &key);
26+
27+
private:
28+
QtMvvm::AndroidSettingsAccessor *_accessor;
29+
};
30+
31+
#endif // SETTINGS_H

examples/mvvmcore/SampleCore/SampleCore.pro

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,3 @@ DISTFILES += $$TRANSLATIONS
3535

3636
target.path = $$[QT_INSTALL_EXAMPLES]/mvvmcore/$$TARGET
3737
INSTALLS += target
38-
39-
samples_in_build: QMAKE_QSETTINGSTRANSLATOR = $$PWD/../../../bin/qsettingstranslator.py

examples/mvvmcore/mvvmcore.pro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ TEMPLATE = subdirs
22
QT_FOR_CONFIG += core
33

44
SUBDIRS += \
5-
SampleCore
5+
SampleCore \
6+
DroidSettings

src/imports/mvvmcore/qtmvvmcore_plugin.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <QtMvvmCore/ViewModel>
66
#include <QtMvvmCore/SettingsViewModel>
77
#include <QtMvvmCore/Messages>
8+
#include <QtMvvmCore/ISettingsAccessor>
89

910
#include "qqmlmvvmbinding.h"
1011
#include "qqmlmvvmmessage.h"

src/jar/jar.pro

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
TARGET = QtMvvmCore
2+
3+
load(qt_build_paths)
4+
CONFIG += java
5+
6+
DESTDIR = $$MODULE_BASE_OUTDIR/jar
7+
8+
JAVACLASSPATH += $$PWD/src
9+
JAVASOURCES += $$PWD/src/de/skycoder42/qtmvvm/core/AndroidSettingsAccessor.java
10+
11+
# install
12+
target.path = $$[QT_INSTALL_PREFIX]/jar
13+
INSTALLS += target

0 commit comments

Comments
 (0)