commit 10446b3c853bb8287582057c9b9cada8cafab7c6
parent 55dfef7e663e1936fdd5a43766cc6fd7fbbaca56
Author: jatinchowdhury18 <jatinchowdhury18@gmail.com>
Date: Mon, 19 Apr 2021 09:39:04 -0700
Better handling for IAPs when offline (#191)
* Better handling for IAPs when offline
* {Apply clang-format}
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Diffstat:
4 files changed, 79 insertions(+), 39 deletions(-)
diff --git a/Plugin/Source/GUI/CMakeLists.txt b/Plugin/Source/GUI/CMakeLists.txt
@@ -17,5 +17,6 @@ if(IOS)
target_sources(CHOWTapeModel PRIVATE
IOSOnly/DragToScrollListener.cpp
IOSOnly/ScrollView.cpp
+ IOSOnly/TipJar.cpp
)
endif()
diff --git a/Plugin/Source/GUI/IOSOnly/TipJar.cpp b/Plugin/Source/GUI/IOSOnly/TipJar.cpp
@@ -0,0 +1,71 @@
+#include "TipJar.h"
+
+namespace
+{
+const std::vector<std::pair<String, String>> productInfos = {
+ { "Small Tip ($2)", "chowtape_small_tip_123" },
+ { "Medium Tip ($5)", "chowtape_medium_tip_456" },
+ { "Large Tip ($10)", "chowtape_large_tip_789" },
+ { "Huge Tip ($25)", "chowtape_huge_tip_808" },
+};
+}
+
+TipJar::TipJar()
+{
+ if (! InAppPurchases::getInstance()->isInAppPurchasesSupported())
+ {
+ // this should never happen, since we only enable IAPs on iOS!
+ jassertfalse;
+ return;
+ }
+
+ setText ("Tip Jar");
+ setColour (backgroundColourId, Colours::transparentBlack);
+ setJustificationType (Justification::centred);
+
+ StringArray purchaseIDs;
+ for (auto& info : productInfos)
+ purchaseIDs.add (info.second);
+
+ setDisconnectedMenu();
+
+ InAppPurchases::getInstance()->addListener (this);
+ InAppPurchases::getInstance()->getProductsInformation (purchaseIDs);
+}
+
+TipJar::~TipJar()
+{
+ InAppPurchases::getInstance()->removeListener (this);
+}
+
+void TipJar::setDisconnectedMenu()
+{
+ auto rootMenu = getRootMenu();
+ rootMenu->addItem ("Not connected to server!", false, false, {});
+}
+
+void TipJar::productsInfoReturned (const Array<InAppPurchases::Product>& products)
+{
+ if (products.isEmpty())
+ return;
+
+ auto rootMenu = getRootMenu();
+ rootMenu->clear();
+ for (auto& info : productInfos)
+ {
+ for (int i = 0; i < products.size(); ++i)
+ {
+ if (info.second == products[i].identifier)
+ {
+ rootMenu->addItem (info.first, [=] { doTipPurchase (info.second); });
+ break;
+ }
+ }
+ }
+}
+
+void TipJar::doTipPurchase (const String& id)
+{
+ InAppPurchases::getInstance()->purchaseProduct (id);
+ setText ("Tip Jar");
+}
diff --git a/Plugin/Source/GUI/IOSOnly/TipJar.h b/Plugin/Source/GUI/IOSOnly/TipJar.h
@@ -2,49 +2,18 @@
#include <JuceHeader.h>
-class TipJar : public ComboBox
+class TipJar : public ComboBox,
+ private InAppPurchases::Listener
{
public:
- TipJar()
- {
- productInfos = {
- { "Small Tip ($2)", "chowtape_small_tip_123" },
- { "Medium Tip ($5)", "chowtape_medium_tip_456" },
- { "Large Tip ($10)", "chowtape_large_tip_789" },
- { "Huge Tip ($25)", "chowtape_huge_tip_808" },
- };
-
- if (! InAppPurchases::getInstance()->isInAppPurchasesSupported())
- {
- // this should never happen, since we only enable IAPs on iOS!
- jassertfalse;
- return;
- }
-
- setTextWhenNothingSelected ("Tip Jar");
- setColour (backgroundColourId, Colours::transparentBlack);
- setJustificationType (Justification::centred);
-
- auto rootMenu = getRootMenu();
- StringArray purchaseIDs;
- for (auto& info : productInfos)
- {
- rootMenu->addItem (info.first, [=] { doTipPurchase (info.second); });
- purchaseIDs.add (info.second);
- }
+ TipJar();
+ ~TipJar() override;
- InAppPurchases::getInstance()->getProductsInformation (purchaseIDs);
- }
-
- void doTipPurchase (const String& id)
- {
- InAppPurchases::getInstance()->purchaseProduct (id);
- setText ("Tip Jar");
- };
+ void productsInfoReturned (const Array<InAppPurchases::Product>& products) override;
+ void doTipPurchase (const String& id);
+ void setDisconnectedMenu();
private:
- std::vector<std::pair<String, String>> productInfos;
-
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TipJar)
};
diff --git a/Plugin/modules/CMakeLists.txt b/Plugin/modules/CMakeLists.txt
@@ -62,6 +62,5 @@ set_target_properties(juce_plugin_modules PROPERTIES
if(IOS)
target_link_libraries(juce_plugin_modules PRIVATE juce::juce_product_unlocking)
- target_link_libraries(juce_plugin_modules PRIVATE "-framework StoreKit")
target_compile_definitions(juce_plugin_modules PUBLIC JUCE_IN_APP_PURCHASES=1)
endif()