Skip to content

Commit

Permalink
Take the login panel logic out of the ShareAudioDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
crsib committed Dec 12, 2023
1 parent 7d62b3a commit 2e4c87d
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 135 deletions.
2 changes: 2 additions & 0 deletions modules/mod-cloud-audiocom/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ set( SOURCES
ui/ShareAudioToolbar.h
ui/UserImage.cpp
ui/UserImage.h
ui/UserPanel.cpp
ui/UserPanel.h

AudioComModule.cpp
AuthorizationHandler.cpp
Expand Down
143 changes: 16 additions & 127 deletions modules/mod-cloud-audiocom/ui/ShareAudioDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
#include "UserService.h"

#include "AuthorizationHandler.h"
#include "LinkAccountDialog.h"
#include "UserImage.h"
#include "UserPanel.h"

#include "CodeConversions.h"

Expand All @@ -52,16 +51,10 @@
#include "HelpSystem.h"
#include "ProjectRate.h"

#ifdef HAS_CUSTOM_URL_HANDLING
#include "URLSchemesRegistry.h"
#endif

namespace cloud::audiocom
{
namespace
{
const wxSize avatarSize = { 32, 32 };

wxString GenerateTempPath(FileExtension extension)
{
const auto tempPath = GetUploadTempPath();
Expand Down Expand Up @@ -589,8 +582,6 @@ void ShareAudioDialog::UpdateProgress(uint64_t current, uint64_t total)

ShareAudioDialog::InitialStatePanel::InitialStatePanel(ShareAudioDialog& parent)
: parent { parent }
, mUserDataChangedSubscription(
GetUserService().Subscribe([this](const auto&) { UpdateUserData(); }))
{
}

Expand All @@ -602,36 +593,13 @@ void ShareAudioDialog::InitialStatePanel::PopulateInitialStatePanel(
{
s.SetBorder(16);

s.StartHorizontalLay(wxEXPAND, 0);
{
avatar = safenew UserImage(s.GetParent(), avatarSize);

s.AddWindow(avatar);

s.StartVerticalLay(wxEXPAND, 1);
{
s.SetBorder(0);
s.AddSpace(0, 0, 1);
name = s.AddVariableText(XO("Anonymous"));
s.AddSpace(0, 0, 1);
}
s.EndVerticalLay();
userPanel = safenew UserPanel { GetServiceConfig(), GetOAuthService(),
GetUserService(), true, s.GetParent() };

s.AddSpace(0, 0, 1);
mUserDataChangedSubscription = userPanel->Subscribe(
[this](auto message) { UpdateUserData(message.IsAuthorized); });

s.StartVerticalLay(wxEXPAND, 1);
{
s.AddSpace(0, 0, 1);

s.SetBorder(16);
oauthButton = s.AddButton(XXO("&Link Account"));
oauthButton->Bind(
wxEVT_BUTTON, [this](auto) { OnLinkButtonPressed(); });
s.AddSpace(0, 0, 1);
}
s.EndVerticalLay();
}
s.EndHorizontalLay();
s.Prop(0).AddWindow(userPanel, wxEXPAND);

s.SetBorder(0);

Expand Down Expand Up @@ -681,102 +649,23 @@ void ShareAudioDialog::InitialStatePanel::PopulateInitialStatePanel(
s.EndVerticalLay();
s.EndInvisiblePanel();

UpdateUserData();
UpdateUserData(
GetOAuthService().HasRefreshToken() &&
!GetUserService().GetUserSlug().empty());
}

void ShareAudioDialog::InitialStatePanel::UpdateUserData()
void ShareAudioDialog::InitialStatePanel::UpdateUserData(bool authorized)
{
auto rootParent = root->GetParent();
rootParent->Freeze();

auto layoutUpdater = finally(
[rootParent = root->GetParent(), this]()
{
oauthButton->Fit();
rootParent->Fit();
rootParent->Layout();

rootParent->Thaw();

rootParent->Refresh();
});

auto& oauthService = GetOAuthService();

if (!oauthService.HasRefreshToken())
{
SetAnonymousState();
return;
}

if (!oauthService.HasAccessToken())
oauthService.ValidateAuth({});

auto& userService = GetUserService();
parent.mIsAuthorised = authorized;

if (userService.GetUserSlug().empty())
{
SetAnonymousState();
return;
}

const auto displayName = userService.GetDisplayName();

if (!displayName.empty())
name->SetLabel(displayName);

const auto avatarPath = userService.GetAvatarPath();

if (!avatarPath.empty())
avatar->SetBitmap(avatarPath);
else
avatar->SetBitmap(theTheme.Bitmap(bmpAnonymousUser));

oauthButton->SetLabel(XXO("&Unlink Account").Translation());

parent.mIsAuthorised = true;

anonInfoPanel->Hide();
authorizedInfoPanel->Show();
anonInfoPanel->Show(!authorized);
authorizedInfoPanel->Show(authorized);

if (parent.mContinueButton != nullptr)
parent.mContinueButton->Enable(!trackTitle->GetValue().empty());
}

void ShareAudioDialog::InitialStatePanel::OnLinkButtonPressed()
{
auto& oauthService = GetOAuthService();
parent.mContinueButton->Enable(
authorized && !trackTitle->GetValue().empty());

if (oauthService.HasAccessToken())
oauthService.UnlinkAccount();
else
{
OpenInDefaultBrowser(
{ audacity::ToWXString(GetServiceConfig().GetOAuthLoginPage()) });

#ifdef HAS_CUSTOM_URL_HANDLING
if (!URLSchemesRegistry::Get().IsURLHandlingSupported())
#endif
{
LinkAccountDialog dlg(root);
dlg.ShowModal();
}
}
}

void ShareAudioDialog::InitialStatePanel::SetAnonymousState()
{
parent.mIsAuthorised = false;

name->SetLabel(XO("Anonymous").Translation());
avatar->SetBitmap(theTheme.Bitmap(bmpAnonymousUser));
oauthButton->SetLabel(XXO("&Link Account").Translation());

anonInfoPanel->Show();
authorizedInfoPanel->Hide();

if (parent.mContinueButton != nullptr)
parent.mContinueButton->Enable(false);
root->GetParent()->Layout();
}

wxString ShareAudioDialog::InitialStatePanel::GetTrackTitle() const
Expand Down
11 changes: 3 additions & 8 deletions modules/mod-cloud-audiocom/ui/ShareAudioDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class wxRadioButton;

namespace cloud::audiocom
{
class UserImage;
class UserPanel;

struct UploadFailedPayload;
struct UploadSuccessfulPayload;
Expand Down Expand Up @@ -69,9 +69,7 @@ class ShareAudioDialog final :

wxWindow* root { nullptr };

UserImage* avatar { nullptr };
wxStaticText* name { nullptr };
wxButton* oauthButton { nullptr };
UserPanel* userPanel { nullptr };
wxPanel* anonInfoPanel { nullptr };
wxPanel* authorizedInfoPanel { nullptr };
wxTextCtrl* trackTitle { nullptr };
Expand All @@ -80,10 +78,7 @@ class ShareAudioDialog final :

void PopulateInitialStatePanel(ShuttleGui& s);

void UpdateUserData();
void OnLinkButtonPressed();

void SetAnonymousState();
void UpdateUserData(bool authorized);

wxString GetTrackTitle() const;
bool HasValidTitle() const;
Expand Down
164 changes: 164 additions & 0 deletions modules/mod-cloud-audiocom/ui/UserPanel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*!********************************************************************
Audacity: A Digital Audio Editor
UserPanel.cpp
Dmitry Vedenko
**********************************************************************/

#include "UserPanel.h"

#include <cassert>

#include <wx/stattext.h>
#include <wx/button.h>
#include <wx/sizer.h>

#ifdef HAS_CUSTOM_URL_HANDLING
# include "URLSchemesRegistry.h"
#endif

#include "LinkAccountDialog.h"

#include "ServiceConfig.h"
#include "OAuthService.h"
#include "UserService.h"

#include "CodeConversions.h"

#include "ShuttleGui.h"
#include "Theme.h"
#include "AllThemeResources.h"

#include "UserImage.h"

#include "MemoryX.h"

#include "HelpSystem.h"

namespace cloud::audiocom
{

namespace
{
const wxSize avatarSize = { 32, 32 };
} // namespace

UserPanel::UserPanel(
const ServiceConfig& serviceConfig, OAuthService& authService,
UserService& userService, bool hasLinkButton, wxWindow* parent,
const wxPoint& pos, const wxSize& size)
: wxPanelWrapper { parent, wxID_ANY, pos, size }
, mServiceConfig { serviceConfig }
, mAuthService { authService }
, mUserService { userService }
, mUserDataChangedSubscription {
userService.Subscribe([this](const auto&) { UpdateUserData(); })
}
{

mUserImage = safenew UserImage(this, avatarSize);
mUserName = safenew wxStaticText(this, wxID_ANY, XO("Anonymous").Translation());
mLinkButton = safenew wxButton(this, wxID_ANY, XXO("&Link Account").Translation());
mLinkButton->Bind(wxEVT_BUTTON, [this](auto) { OnLinkButtonPressed(); });
mLinkButton->Show(hasLinkButton);

auto sizer = safenew wxBoxSizer { wxHORIZONTAL };

sizer->Add(mUserImage, 0, wxALIGN_CENTER_VERTICAL);
sizer->AddSpacer(8);
sizer->Add(mUserName, 0, wxALIGN_CENTER_VERTICAL);
sizer->AddStretchSpacer();
sizer->Add(mLinkButton, 0, wxALIGN_CENTER_VERTICAL);

SetSizerAndFit(sizer);
UpdateUserData();
}

UserPanel::~UserPanel() = default;

void UserPanel::UpdateUserData()
{
Freeze();

auto layoutUpdater = finally(
[this]()
{
mLinkButton->Fit();

Layout();
Thaw();

Refresh();
});

auto& oauthService = GetOAuthService();

if (!oauthService.HasRefreshToken())
{
SetAnonymousState();
return;
}

if (!oauthService.HasAccessToken())
oauthService.ValidateAuth({});

auto& userService = GetUserService();

if (userService.GetUserSlug().empty())
{
SetAnonymousState();
return;
}

const auto displayName = userService.GetDisplayName();

if (!displayName.empty())
mUserName->SetLabel(displayName);

const auto avatarPath = userService.GetAvatarPath();

if (!avatarPath.empty())
mUserImage->SetBitmap(avatarPath);
else
mUserImage->SetBitmap(theTheme.Bitmap(bmpAnonymousUser));

mLinkButton->SetLabel(XXO("&Unlink Account").Translation());

Publish(UserPanelStateChangedMessage { true });
}

void UserPanel::OnLinkButtonPressed()
{
auto& oauthService = GetOAuthService();

if (oauthService.HasAccessToken())
oauthService.UnlinkAccount();
else
{
OpenInDefaultBrowser(
{ audacity::ToWXString(mServiceConfig.GetOAuthLoginPage()) });

#ifdef HAS_CUSTOM_URL_HANDLING
if (!URLSchemesRegistry::Get().IsURLHandlingSupported())
#endif
{
LinkAccountDialog dlg(this);
dlg.ShowModal();
}
}
}

void UserPanel::SetAnonymousState()
{
mUserName->SetLabel(XO("Anonymous").Translation());
mUserImage->SetBitmap(theTheme.Bitmap(bmpAnonymousUser));
mLinkButton->SetLabel(XXO("&Link Account").Translation());

Publish(UserPanelStateChangedMessage { false });
}

} // namespace cloud::audiocom
Loading

0 comments on commit 2e4c87d

Please sign in to comment.