-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Feature/joonne/data storage #37
base: master
Are you sure you want to change the base?
Changes from 6 commits
0bf9a1f
ec8d5c5
787aaf7
69e78cb
dcbd584
e10e6c5
173a23e
56b3da3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,10 @@ Page { | |
if (visible) updateCover(qsTr("Now playing"), program.title, program.itemTitle) | ||
} | ||
|
||
Component.onDestruction: { | ||
appWindow.insertStartedProgram({ id: program.id, progress: mediaPlayer.position }) | ||
} | ||
|
||
function initialize() { | ||
YleApi.getMediaUrl(program.id, program.mediaId) | ||
.then(function(response) { | ||
|
@@ -41,6 +45,7 @@ Page { | |
subtitlesText.getSubtitles(subtitlesUrl) | ||
} | ||
mediaPlayer.source = response.url | ||
if (appWindow.state.startedPrograms[program.id]) mediaPlayer.seek(appWindow.state.startedPrograms[program.id]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this could be more readable if this had curly brackets and function call inside those or at least an empty line after this because otherwise it's possible to read that the if affects the next line. Also the line is quite long now. |
||
mediaPlayer.play() | ||
YleApi.reportUsage(program.id, program.mediaId) | ||
}) | ||
|
@@ -122,6 +127,10 @@ Page { | |
if (subtitlesUrl && subtitlesText) { | ||
subtitlesText.getSubtitles(subtitlesUrl) | ||
} | ||
|
||
if (mediaPlayer.playbackState === MediaPlayer.PausedState) { | ||
appWindow.insertStartedProgram({ id: program.id, progress: mediaPlayer.position }) | ||
} | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include "serializer.h" | ||
|
||
#include <QFile> | ||
#include <QJsonArray> | ||
#include <QJsonDocument> | ||
#include <QTextStream> | ||
#include <QStandardPaths> | ||
#include <QDir> | ||
#include <QDebug> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe make the include list alphabetically ordered. |
||
|
||
Serializer::Serializer(QObject *parent) : QObject(parent) | ||
{ | ||
this->ensureDir(); | ||
this->readState(); | ||
} | ||
|
||
Serializer::~Serializer() | ||
{ | ||
this->writeState(); | ||
} | ||
|
||
QJsonObject Serializer::getInitialState() const | ||
{ | ||
return QJsonObject | ||
{ | ||
{"startedPrograms", QJsonObject()} | ||
}; | ||
} | ||
|
||
bool Serializer::readState() | ||
{ | ||
QFile file(this->getPath()); | ||
|
||
if (!file.open(QIODevice::ReadWrite)) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move curly bracket to same line with if which is the style used in other files. Same for other if statements in the new code. |
||
return false; | ||
} | ||
|
||
auto json = (QJsonDocument::fromJson(file.readAll())).object(); | ||
|
||
this->m_state = json.empty() ? this->getInitialState() : json; | ||
|
||
return true; | ||
} | ||
|
||
bool Serializer::writeState() const | ||
{ | ||
QFile file(this->getPath()); | ||
|
||
if (!file.open(QIODevice::ReadWrite)) | ||
{ | ||
return false; | ||
} | ||
|
||
QJsonDocument saveDoc(m_state); | ||
file.write(saveDoc.toJson()); | ||
|
||
return true; | ||
} | ||
|
||
void Serializer::setState(QJsonObject state) | ||
{ | ||
m_state = state; | ||
} | ||
|
||
QJsonObject Serializer::getState() | ||
{ | ||
return m_state; | ||
} | ||
|
||
void Serializer::ensureDir() const | ||
{ | ||
QDir dir(QStandardPaths::writableLocation(QStandardPaths::DataLocation)); | ||
|
||
if (!dir.exists()) | ||
{ | ||
dir.mkpath("."); | ||
} | ||
} | ||
|
||
QString Serializer::getPath() const | ||
{ | ||
return QStandardPaths::writableLocation(QStandardPaths::DataLocation) + QDir::separator() + "state.json"; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#ifndef SERIALIZER_H | ||
#define SERIALIZER_H | ||
|
||
#include <QObject> | ||
#include <QJsonObject> | ||
|
||
class Serializer : public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
explicit Serializer(QObject *parent = 0); | ||
~Serializer(); | ||
|
||
Q_INVOKABLE void setState(QJsonObject state); | ||
Q_INVOKABLE QJsonObject getState(); | ||
|
||
signals: | ||
public slots: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to have the empty signals and public slots defined here? |
||
private: | ||
bool readState(); | ||
bool writeState() const; | ||
QString getPath() const; | ||
void ensureDir() const; | ||
QJsonObject getInitialState() const; | ||
|
||
QJsonObject m_state; | ||
}; | ||
|
||
#endif // SERIALIZER_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check if this is called upon closing the app by a swipe and make sure this is called also when pause button is pressed in case the handle isn't called upon closing the app forcefully