Skip to content

Commit

Permalink
add 4.26 support to transak widget (#156)
Browse files Browse the repository at this point in the history
* feat: added transak web widget and environment enum

* fix: resolved compile issue with ue4

* fix: compiler issue with ue4

* chore: isolated source code for ue5

* feat: add blu web browser to transak widget (#3498)

* refactor: apply formatting

* refactor: move include inside respective engine macro

* refactor: expose is ready to blueprint

* refactor: change private to protected

* refactor: reorder .h and .cpp functions to match each other

* chore: update loctext namespace

* fix: compile error

* chore: removed redundant code

* chore: removed prefixes

---------

Co-authored-by: Yermek Garifullanov <[email protected]>
  • Loading branch information
ImmutableJeffrey and YermekG authored Dec 19, 2024
1 parent 44295f1 commit 91f1e9f
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 117 deletions.
1 change: 0 additions & 1 deletion .github/workflows/branch-name.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ jobs:
- 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'
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
211 changes: 107 additions & 104 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 @@ -66,101 +166,4 @@ 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;
}
#endif
33 changes: 21 additions & 12 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;

};
};

0 comments on commit 91f1e9f

Please sign in to comment.