-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Progress #19: Initial work on PurchaseWrapper
Begin implementation of PurchaseWrapper, which wraps CapnP Purchase APIs. Complete notification is done, still to do is getting prces, notifying server of payment
- Loading branch information
1 parent
049e83e
commit b4418ff
Showing
9 changed files
with
98 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include "PurchaseWrapper.hpp" | ||
|
||
namespace swv { | ||
|
||
void PurchaseWrapper::setComplete(bool complete) | ||
{ | ||
if (complete == m_complete) return; | ||
m_complete = complete; | ||
emit completeChanged(complete); | ||
} | ||
|
||
PurchaseWrapper::PurchaseWrapper(Purchase::Client&& api, kj::TaskSet& tasks, QObject *parent) | ||
: QObject(parent), | ||
api(kj::mv(api)), | ||
tasks(tasks), | ||
converter(tasks) | ||
{ | ||
class CompleteNotifier : public Notifier<capnp::Text>::Server { | ||
PurchaseWrapper& wrapper; | ||
virtual ::kj::Promise<void> notify(NotifyContext context) { | ||
wrapper.setComplete(context.getParams().getMessage() == "true"); | ||
return kj::READY_NOW; | ||
} | ||
|
||
public: | ||
CompleteNotifier(PurchaseWrapper& wrapper) : wrapper(wrapper) {} | ||
}; | ||
|
||
auto request = api.subscribeRequest(); | ||
request.setNotifier(kj::heap<CompleteNotifier>(*this)); | ||
tasks.add(request.send().then([](capnp::Response<Purchase::SubscribeResults>){})); | ||
tasks.add(api.completeRequest().send().then([this] (capnp::Response<Purchase::CompleteResults> r) { | ||
setComplete(r.getResult()); | ||
})); | ||
} | ||
|
||
PurchaseWrapper::~PurchaseWrapper() noexcept | ||
{} | ||
|
||
} // namespace swv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#ifndef PURCHASEWRAPPER_HPP | ||
#define PURCHASEWRAPPER_HPP | ||
|
||
#include <purchase.capnp.h> | ||
#include <PromiseConverter.hpp> | ||
|
||
#include <QObject> | ||
|
||
namespace swv { | ||
|
||
/** | ||
* @brief The PurchaseWrapper class provides a QML-friendly interface to the CapnP Purchase type | ||
* | ||
* PurchaseWrapper subscribes to a push notification from the server when the purchase is complete, so the complete | ||
* property will be updated automatically as soon as the purchase completes. | ||
*/ | ||
class PurchaseWrapper : public QObject | ||
{ | ||
Q_OBJECT | ||
Q_PROPERTY(bool complete READ complete NOTIFY completeChanged) | ||
|
||
Purchase::Client api; | ||
kj::TaskSet& tasks; | ||
PromiseConverter converter; | ||
bool m_complete = false; | ||
|
||
void setComplete(bool complete); | ||
|
||
public: | ||
explicit PurchaseWrapper(Purchase::Client&& api, kj::TaskSet& tasks, QObject *parent = 0); | ||
virtual ~PurchaseWrapper() noexcept; | ||
|
||
bool complete() const { | ||
return m_complete; | ||
} | ||
|
||
signals: | ||
void completeChanged(bool complete); | ||
}; | ||
|
||
} // namespace swv | ||
#endif // PURCHASEWRAPPER_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters