Skip to content
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

Add 4.26 support to Transak Widget #156

Open
wants to merge 18 commits into
base: feat/transak
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/branch-name.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ jobs:
steps:
- uses: deepakputhraya/action-branch-name@master
with:
regex: '([a-z])+\/([a-z0-9.-])+'
allowed_prefixes: 'feat,fix,build,chore,ci,docs,style,refactor,perf,test,revert,release'
regex: '([a-z])*\/?([a-z0-9.-])+'
ignore: main
min_length: 5
max_length: 50
1 change: 1 addition & 0 deletions Source/Immutable/Immutable.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public Immutable(ReadOnlyTargetRules Target) : base(Target)
PrivateDependencyModuleNames.AddRange(
new string[]
{
"BluExtension",
"CoreUObject",
"Engine",
"Slate",
Expand Down
209 changes: 106 additions & 103 deletions Source/Immutable/Private/Immutable/Transak/TransakWebBrowser.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
#include "Immutable/Transak/TransakWebBrowser.h"

#include "PlatformHttp.h"

#include "Immutable/ImmutableUtilities.h"
#include "Immutable/TransakConfig.h"

#if (ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 1)
#include "SWebBrowser.h"

#include "Immutable/Misc/ImtblLogging.h"
#else
#include "UserInterface/BluWebBrowser.h"
#endif

#define LOCTEXT_NAMESPACE "TransakWebBrowser"

bool UTransakWebBrowser::IsReady() const
{
return bIsReady;
}

void UTransakWebBrowser::Load(const FString& WalletAddress, const FString& Email, const FString& ProductsAvailed, const FString& ScreenTitle)
{
if (!WebBrowserWidget.IsValid())
{
return;
}

#define LOCTEXT_NAMESPACE "Immutable"
FString UrlToLoad = ComputePath(WalletAddress, Email, ProductsAvailed, ScreenTitle);
FDelegateHandle OnWhenReadyHandle = CallAndRegister_OnWhenReady(UTransakWebBrowser::FOnWhenReady::FDelegate::CreateWeakLambda(this, [this, UrlToLoad, OnWhenReadyHandle]()
{
WebBrowserWidget->LoadURL(UrlToLoad);
OnWhenReady.Remove(OnWhenReadyHandle);
}));
}

FDelegateHandle UTransakWebBrowser::CallAndRegister_OnWhenReady(FOnWhenReady::FDelegate Delegate)
{
Expand All @@ -20,7 +47,7 @@ FDelegateHandle UTransakWebBrowser::CallAndRegister_OnWhenReady(FOnWhenReady::FD

TSharedRef<SWidget> UTransakWebBrowser::RebuildWidget()
{
if ( IsDesignTime() )
if (IsDesignTime())
{
return SNew(SBox)
.HAlign(HAlign_Center)
Expand All @@ -34,29 +61,102 @@ TSharedRef<SWidget> UTransakWebBrowser::RebuildWidget()
{
#if (ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 1)
WebBrowserWidget = SNew(SWebBrowser)
.InitialURL(TEXT("about:blank"))
.InitialURL(InitialURL)
.ShowControls(false)
.SupportsTransparency(false)
.OnUrlChanged(BIND_UOBJECT_DELEGATE(FOnTextChanged, HandleOnUrlChanged))
.OnBeforePopup(BIND_UOBJECT_DELEGATE(FOnBeforePopupDelegate, HandleOnBeforePopup))
.OnConsoleMessage(BIND_UOBJECT_DELEGATE(FOnConsoleMessageDelegate, HandleOnConsoleMessage));
return WebBrowserWidget.ToSharedRef();
#else
return SNullWidget::NullWidget;
WebBrowserWidget = SNew(SBluWebBrowser)
.InitialURL(InitialURL)
.OnUrlChanged(BIND_UOBJECT_DELEGATE(FOnTextChanged, HandleOnUrlChanged));
return WebBrowserWidget.ToSharedRef();
#endif
}
}

#if (ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 1)
FString UTransakWebBrowser::ComputePath(const FString& WalletAddress, const FString& Email, const FString& ProductsAvailed, const FString& ScreenTitle)
{
UTransakConfig* TransakConfig = FImmutableUtilities::GetDefaultTransakConfig();

if (!TransakConfig)
{
return "";
}

FString Path = TransakConfig->GetURL();
TArray<FString> QueryParams;

QueryParams.Add(FString(TEXT("apiKey=")) + FPlatformHttp::UrlEncode(TransakConfig->GetAPIKey()));
QueryParams.Add(FString(TEXT("email=")) + FPlatformHttp::UrlEncode(Email));
QueryParams.Add(FString(TEXT("walletAddress=")) + FPlatformHttp::UrlEncode(WalletAddress));
QueryParams.Add(FString(TEXT("themeColor=")) + FPlatformHttp::UrlEncode(TransakConfig->GetThemeColor().ToString()));
QueryParams.Add(FString(TEXT("isAutoFillUserData=")) + FPlatformHttp::UrlEncode(TransakConfig->IsAutoFillUserData() ? TEXT("true") : TEXT("false")));
QueryParams.Add(FString(TEXT("disableWalletAddressForm=")) + FPlatformHttp::UrlEncode(TransakConfig->DisableWalletAddressForm() ? TEXT("true") : TEXT("false")));

if (!TransakConfig->GetNetwork().IsEmpty())
{
QueryParams.Add(FString(TEXT("network=")) + FPlatformHttp::UrlEncode(TransakConfig->GetNetwork()));
}

if (!ProductsAvailed.IsEmpty())
{
QueryParams.Add(FString(TEXT("productsAvailed=")) + FPlatformHttp::UrlEncode(ProductsAvailed));
}

if (!ScreenTitle.IsEmpty())
{
QueryParams.Add(FString(TEXT("exchangeScreenTitle=")) + FPlatformHttp::UrlEncode(ScreenTitle));
}

if (!TransakConfig->GetDefaultCryptoCurrency().IsEmpty())
{
QueryParams.Add(FString(TEXT("defaultCryptoCurrency=")) + FPlatformHttp::UrlEncode(TransakConfig->GetDefaultCryptoCurrency()));
}

if (!TransakConfig->GetDefaultFiatAmount().IsEmpty())
{
QueryParams.Add(FString(TEXT("defaultFiatAmount=")) + FPlatformHttp::UrlEncode(TransakConfig->GetDefaultFiatAmount()));
}

if (!TransakConfig->GetDefaultFiatCurrency().IsEmpty())
{
QueryParams.Add(FString(TEXT("defaultFiatCurrency=")) + FPlatformHttp::UrlEncode(TransakConfig->GetDefaultFiatCurrency()));
}

if (!TransakConfig->GetDefaultPaymentMethod().IsEmpty())
{
QueryParams.Add(FString(TEXT("defaultPaymentMethod=")) + FPlatformHttp::UrlEncode(TransakConfig->GetDefaultPaymentMethod()));
}

if (TransakConfig->GetCryptoCurrencyList().Num() > 0)
{
QueryParams.Add(FString(TEXT("cryptoCurrencyList=")) + FPlatformHttp::UrlEncode(FString::Join(TransakConfig->GetCryptoCurrencyList(), TEXT(","))));
}

if (TransakConfig->GetDisablePaymentMethods().Num() > 0)
{
QueryParams.Add(FString(TEXT("disablePaymentMethods=")) + FPlatformHttp::UrlEncode(FString::Join(TransakConfig->GetDisablePaymentMethods(), TEXT(","))));
}

Path += TEXT("?");
Path += FString::Join(QueryParams, TEXT("&"));

return Path;
}

void UTransakWebBrowser::HandleOnUrlChanged(const FText& Text)
{
if (Text.EqualToCaseIgnored(FText::FromString(TEXT("about:blank"))))
if (Text.EqualToCaseIgnored(FText::FromString(InitialURL)))
{
bIsReady = true;
OnWhenReady.Broadcast();
}
}

#if (ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 1)
void UTransakWebBrowser::HandleOnConsoleMessage(const FString& Message, const FString& Source, int32 Line, EWebBrowserConsoleLogSeverity Severity)
{
IMTBL_LOG("Transak Web Browser console message: %s, Source: %s, Line: %d", *Message, *Source, Line);
Expand All @@ -67,100 +167,3 @@ bool UTransakWebBrowser::HandleOnBeforePopup(FString URL, FString Frame)
return false;
}
#endif

void UTransakWebBrowser::Load(const FString& WalletAddress, const FString& Email, const FString& ProductsAvailed, const FString& ScreenTitle)
{
if (!WebBrowserWidget.IsValid())
{
return;
}

FString UrlToLoad = ComputePath(WalletAddress, Email, ProductsAvailed, ScreenTitle);

if (bIsReady)
{
#if (ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 1)
WebBrowserWidget->LoadURL(UrlToLoad);
#endif
}
else
{
FDelegateHandle OnWhenReadyHandle = CallAndRegister_OnWhenReady(UTransakWebBrowser::FOnWhenReady::FDelegate::CreateWeakLambda(this, [this, UrlToLoad, OnWhenReadyHandle]()
{
#if (ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 1)
WebBrowserWidget->LoadURL(UrlToLoad);
#endif
OnWhenReady.Remove(OnWhenReadyHandle);
}));
}
}

FString UTransakWebBrowser::ComputePath(const FString& WalletAddress, const FString& Email, const FString& ProductsAvailed, const FString& ScreenTitle)
{
UTransakConfig* TransakConfig = FImmutableUtilities::GetDefaultTransakConfig();

if (!TransakConfig)
{
return "";
}

FString Path = TransakConfig->GetURL();
TArray<FString> QueryParams;

QueryParams.Add(FString(TEXT("apiKey=")) + FPlatformHttp::UrlEncode(TransakConfig->GetAPIKey()));
QueryParams.Add(FString(TEXT("email=")) + FPlatformHttp::UrlEncode(Email));
QueryParams.Add(FString(TEXT("walletAddress=")) + FPlatformHttp::UrlEncode(WalletAddress));
QueryParams.Add(FString(TEXT("themeColor=")) + FPlatformHttp::UrlEncode(TransakConfig->GetThemeColor().ToString()));
QueryParams.Add(FString(TEXT("isAutoFillUserData=")) + FPlatformHttp::UrlEncode(TransakConfig->IsAutoFillUserData() ? TEXT("true") : TEXT("false")));
QueryParams.Add(FString(TEXT("disableWalletAddressForm=")) + FPlatformHttp::UrlEncode(TransakConfig->DisableWalletAddressForm() ? TEXT("true") : TEXT("false")));

if (!TransakConfig->GetNetwork().IsEmpty())
{
QueryParams.Add(FString(TEXT("network=")) + FPlatformHttp::UrlEncode(TransakConfig->GetNetwork()));
}

if (!ProductsAvailed.IsEmpty())
{
QueryParams.Add(FString(TEXT("productsAvailed=")) + FPlatformHttp::UrlEncode(ProductsAvailed));
}

if (!ScreenTitle.IsEmpty())
{
QueryParams.Add(FString(TEXT("exchangeScreenTitle=")) + FPlatformHttp::UrlEncode(ScreenTitle));
}

if (!TransakConfig->GetDefaultCryptoCurrency().IsEmpty())
{
QueryParams.Add(FString(TEXT("defaultCryptoCurrency=")) + FPlatformHttp::UrlEncode(TransakConfig->GetDefaultCryptoCurrency()));
}

if (!TransakConfig->GetDefaultFiatAmount().IsEmpty())
{
QueryParams.Add(FString(TEXT("defaultFiatAmount=")) + FPlatformHttp::UrlEncode(TransakConfig->GetDefaultFiatAmount()));
}

if (!TransakConfig->GetDefaultFiatCurrency().IsEmpty())
{
QueryParams.Add(FString(TEXT("defaultFiatCurrency=")) + FPlatformHttp::UrlEncode(TransakConfig->GetDefaultFiatCurrency()));
}

if (!TransakConfig->GetDefaultPaymentMethod().IsEmpty())
{
QueryParams.Add(FString(TEXT("defaultPaymentMethod=")) + FPlatformHttp::UrlEncode(TransakConfig->GetDefaultPaymentMethod()));
}

if (TransakConfig->GetCryptoCurrencyList().Num() > 0)
{
QueryParams.Add(FString(TEXT("cryptoCurrencyList=")) + FPlatformHttp::UrlEncode(FString::Join(TransakConfig->GetCryptoCurrencyList(), TEXT(","))));
}

if (TransakConfig->GetDisablePaymentMethods().Num() > 0)
{
QueryParams.Add(FString(TEXT("disablePaymentMethods=")) + FPlatformHttp::UrlEncode(FString::Join(TransakConfig->GetDisablePaymentMethods(), TEXT(","))));
}

Path += TEXT("?");
Path += FString::Join(QueryParams, TEXT("&"));

return Path;
}
31 changes: 20 additions & 11 deletions Source/Immutable/Public/Immutable/Transak/TransakWebBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

#include "Components/Widget.h"

#include "TransakWebBrowser.generated.h"

#if (ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 1)
#include "SWebBrowser.h"
#endif
enum class EWebBrowserConsoleLogSeverity;

#include "TransakWebBrowser.generated.h"
class SWebBrowser;
#else
class SBluWebBrowser;
#endif

/**
*
Expand All @@ -18,37 +22,42 @@ class IMMUTABLE_API UTransakWebBrowser : public UWidget

public:
DECLARE_MULTICAST_DELEGATE(FOnWhenReady);


public:
UFUNCTION(BlueprintPure)
bool IsReady() const;

UFUNCTION(BlueprintCallable)
void Load(const FString& WalletAddress, const FString& Email, const FString& ProductsAvailed, const FString& ScreenTitle);

bool IsReady() const { return bIsReady; };

FDelegateHandle CallAndRegister_OnWhenReady(FOnWhenReady::FDelegate Delegate);

protected:
// UWidget interface
virtual TSharedRef<SWidget> RebuildWidget() override;
// End of UWidget interface

private:
protected:
FString ComputePath(const FString& WalletAddress, const FString& Email, const FString& ProductsAvailed, const FString& ScreenTitle);

void HandleOnUrlChanged(const FText& Text);
#if (ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 1)
void HandleOnConsoleMessage(const FString& Message, const FString& Source, int32 Line, EWebBrowserConsoleLogSeverity Severity);
void HandleOnUrlChanged(const FText& Text);
bool HandleOnBeforePopup(FString URL, FString Frame);
#endif

protected:
/** URL that the browser will initially navigate to. The URL should include the protocol, eg http:// */
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
FString InitialURL = TEXT("about:blank");

#if (ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 1)
TSharedPtr<class SWebBrowser> WebBrowserWidget;
#else
TSharedPtr<class SNullWidget> WebBrowserWidget;
TSharedPtr<SBluWebBrowser> WebBrowserWidget;
#endif
FOnWhenReady OnWhenReady;

private:
protected:
bool bIsReady = false;

};
Loading