Skip to content

Commit 19735f0

Browse files
committed
Add ChatPlex connection screen
1 parent 1d4abc7 commit 19735f0

File tree

2 files changed

+205
-7
lines changed

2 files changed

+205
-7
lines changed

shared/CP_SDK/UI/Views/SettingsLeftView.hpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#pragma once
22

3-
#include "../ViewController.hpp"
3+
#include "../../ChatPlexService.hpp"
44
#include "../../XUI/XUI.hpp"
5+
#include "../ViewController.hpp"
56

67
namespace CP_SDK::UI::Views {
78

@@ -23,9 +24,34 @@ namespace CP_SDK::UI::Views {
2324
CP_SDK_IL2CPP_DECLARE_DTOR_MONOBEHAVIOUR_CHILD(SettingsLeftView);
2425
CP_SDK_UI_VIEW_CONTROLLER_INSTANCE();
2526

27+
private:
28+
_v::XUIText::Ptr m_StatusText;
29+
_v::XUIText::Ptr m_SubscriptionText;
30+
_v::XUIPrimaryButton::Ptr m_PrimaryButton;
31+
_v::XUISecondaryButton::Ptr m_SecondaryButton;
32+
33+
private:
34+
bool m_IsLinking = false;
35+
2636
private:
2737
/// @brief On view creation
2838
void OnViewCreation_Impl();
39+
/// @brief On view deactivation
40+
void OnViewDeactivation_Impl();
41+
42+
private:
43+
/// @brief On primary button pressed
44+
void OnPrimaryButtonPressed();
45+
/// @brief On secondary button pressed
46+
void OnSecondaryButtonPressed();
47+
/// @brief On Loading cancel
48+
void OnLoadingCancel();
49+
50+
private:
51+
/// @brief On ChatPlex service state change
52+
/// @param oldState Old state
53+
/// @param newState New state
54+
void ChatPlexService_StateChanged(ChatPlexService::EState oldState, ChatPlexService::EState newState);
2955

3056
};
3157

src/CP_SDK/UI/Views/SettingsLeftView.cpp

Lines changed: 178 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
#include "CP_SDK/UI/Views/SettingsLeftView.hpp"
22
#include "CP_SDK/Unity/SpriteU.hpp"
3+
#include "CP_SDK/Unity/MTMainThreadInvoker.hpp"
4+
#include <optional>
5+
#include <string>
36

47
using namespace CP_SDK::XUI;
58
using namespace UnityEngine;
69

10+
#include "assets.hpp"
11+
712
namespace CP_SDK::UI::Views {
813

914
CP_SDK_IL2CPP_INHERIT_INIT(SettingsLeftView);
@@ -14,7 +19,8 @@ namespace CP_SDK::UI::Views {
1419
/// @brief Constructor
1520
CP_SDK_IL2CPP_DECLARE_CTOR_IMPL(SettingsLeftView)
1621
{
17-
OnViewCreation = {this, &SettingsLeftView::OnViewCreation_Impl};
22+
OnViewCreation = {this, &SettingsLeftView::OnViewCreation_Impl};
23+
OnViewDeactivation = {this, &SettingsLeftView::OnViewDeactivation_Impl};
1824
}
1925
/// @brief Destructor
2026
CP_SDK_IL2CPP_DECLARE_DTOR_MONOBEHAVIOUR_IMPL(SettingsLeftView)
@@ -28,13 +34,179 @@ namespace CP_SDK::UI::Views {
2834
/// @brief On view creation
2935
void SettingsLeftView::OnViewCreation_Impl()
3036
{
37+
auto l_Sprite = Unity::SpriteU::CreateFromRaw(Assets::ChatPlexLogoTransparent_png);
38+
3139
Templates::FullRectLayout({
32-
Templates::TitleBar(u"Tools"),
40+
Templates::TitleBar(u"ChatPlex Account"),
41+
42+
XUIPrimaryButton::Make(u"")
43+
->SetBackgroundSprite(nullptr)
44+
->SetIconSprite(l_Sprite)
45+
->SetWidth(52)
46+
->SetHeight(52)
47+
->AsShared(),
48+
49+
XUIText::Make(u"Not connected")
50+
->Bind(&m_StatusText)
51+
->AsShared(),
52+
53+
XUIText::Make(u" ")
54+
->Bind(&m_SubscriptionText)
55+
->AsShared(),
56+
57+
XUIVLayout::Make({
58+
XUIPrimaryButton::Make(u"Connect", {this, &SettingsLeftView::OnPrimaryButtonPressed})
59+
->Bind(&m_PrimaryButton)
60+
->AsShared(),
61+
XUISecondaryButton::Make(u"Disconnect", {this, &SettingsLeftView::OnSecondaryButtonPressed})
62+
->Bind(&m_SecondaryButton)
63+
->AsShared()
64+
})
65+
->SetWidth(60.0f)
66+
->SetPadding(0)
67+
->ForEachDirect<XUIPrimaryButton>([](XUIPrimaryButton* y) -> void
68+
{
69+
y->SetHeight(8.0f);
70+
y->OnReady([](Components::CPrimaryButton* x) -> void
71+
{
72+
x->CSizeFitter()->horizontalFit = ContentSizeFitter::FitMode::Unconstrained;
73+
});
74+
})
75+
->ForEachDirect<XUISecondaryButton>([](XUISecondaryButton* y ) -> void
76+
{
77+
y->SetHeight(8.0f);
78+
y->OnReady([](Components::CSecondaryButton* x) -> void
79+
{
80+
x->CSizeFitter()->horizontalFit = ContentSizeFitter::FitMode::Unconstrained;
81+
});
82+
})
83+
->AsShared()
84+
})
85+
->SetBackground(true, std::nullopt, true)
86+
->BuildUI(get_transform());
87+
88+
ChatPlexService::StateChanged += {this, &SettingsLeftView::ChatPlexService_StateChanged};
89+
90+
ChatPlexService_StateChanged(ChatPlexService::State(), ChatPlexService::State());
91+
}
92+
/// @brief On view deactivation
93+
void SettingsLeftView::OnViewDeactivation_Impl()
94+
{
95+
ChatPlexService::StateChanged -= {this, &SettingsLeftView::ChatPlexService_StateChanged};
96+
}
97+
98+
////////////////////////////////////////////////////////////////////////////
99+
////////////////////////////////////////////////////////////////////////////
100+
101+
/// @brief On primary button pressed
102+
void SettingsLeftView::OnPrimaryButtonPressed()
103+
{
104+
if (ChatPlexService::State() == ChatPlexService::EState::Disconnected)
105+
{
106+
m_IsLinking = true;
107+
ChatPlexService::StartLinking();
108+
ShowLoadingModal(u"Loading...", true, {this, &SettingsLeftView::OnLoadingCancel});
109+
}
110+
else if (ChatPlexService::State() == ChatPlexService::EState::Error || ChatPlexService::State() == ChatPlexService::EState::Connected)
111+
{
112+
ChatPlexService::Refresh();
113+
}
114+
}
115+
/// @brief On secondary button pressed
116+
void SettingsLeftView::OnSecondaryButtonPressed()
117+
{
118+
ChatPlexService::Disconnect();
119+
}
120+
/// @brief On Loading cancel
121+
void SettingsLeftView::OnLoadingCancel()
122+
{
123+
if (ChatPlexService::State() == ChatPlexService::EState::LinkRequest || ChatPlexService::State() == ChatPlexService::EState::LinkWait)
124+
{
125+
m_IsLinking = false;
126+
ChatPlexService::StopLinking();
127+
}
128+
}
129+
130+
////////////////////////////////////////////////////////////////////////////
131+
////////////////////////////////////////////////////////////////////////////
132+
133+
/// @brief On ChatPlex service state change
134+
/// @param oldState Old state
135+
/// @param newState New state
136+
void SettingsLeftView::ChatPlexService_StateChanged(ChatPlexService::EState oldState, ChatPlexService::EState newState)
137+
{
138+
Unity::MTMainThreadInvoker::Enqueue([this, oldState, newState]() -> void
139+
{
140+
if (m_IsLinking)
141+
{
142+
if (newState == ChatPlexService::EState::LinkRequest)
143+
ShowLoadingModal(u"Creating link request...", true, {this, &SettingsLeftView::OnLoadingCancel});
144+
else if (newState == ChatPlexService::EState::LinkWait)
145+
ShowLoadingModal(
146+
std::u16string(u"Go to https://chatplex.org/link and the input following code\n") + ChatPlexService::LinkCode(),
147+
true,
148+
{this, &SettingsLeftView::OnLoadingCancel}
149+
);
150+
else if (newState == ChatPlexService::EState::Error)
151+
{
152+
m_IsLinking = false;
153+
154+
CloseLoadingModal();
155+
ShowMessageModal(u"Error: " + ChatPlexService::LastError());
156+
}
157+
else
158+
{
159+
m_IsLinking = false;
160+
CloseLoadingModal();
161+
}
162+
}
163+
164+
switch (newState)
165+
{
166+
case ChatPlexService::EState::Disconnected:
167+
m_StatusText->SetColor(Color::get_red());
168+
m_StatusText->SetText(u"Disconected!");
169+
m_PrimaryButton->SetInteractable(true);
170+
m_PrimaryButton->SetText(u"Connect");
171+
m_SecondaryButton->SetInteractable(false);
172+
break;
173+
174+
case ChatPlexService::EState::Error:
175+
m_StatusText->SetColor(Color::get_red());
176+
m_StatusText->SetText(u"Disconected, error!");
177+
m_PrimaryButton->SetInteractable(true);
178+
m_PrimaryButton->SetText(u"Connect");
179+
m_SecondaryButton->SetInteractable(false);
180+
break;
181+
182+
case ChatPlexService::EState::Connecting:
183+
m_StatusText->SetColor(Color::get_blue());
184+
m_StatusText->SetText(u"Connecting...");
185+
m_PrimaryButton->SetInteractable(false);
186+
m_PrimaryButton->SetText(u"Connect");
187+
m_SecondaryButton->SetInteractable(false);
188+
break;
189+
190+
case ChatPlexService::EState::LinkRequest:
191+
case ChatPlexService::EState::LinkWait:
192+
m_StatusText->SetColor(Color::get_blue());
193+
m_StatusText->SetText(u"Linking account...");
194+
m_PrimaryButton->SetInteractable(false);
195+
m_PrimaryButton->SetText(u"Connect");
196+
m_SecondaryButton->SetInteractable(false);
197+
break;
198+
199+
case ChatPlexService::EState::Connected:
200+
m_StatusText->SetColor(Color::get_green());
201+
m_StatusText->SetText(u"Connected!");
202+
m_PrimaryButton->SetInteractable(true);
203+
m_PrimaryButton->SetText(u"Refresh");
204+
m_SecondaryButton->SetInteractable(true);
205+
break;
206+
}
33207

34-
XUIText::Make(u"No available tools at the moment!")->AsShared()
35-
})
36-
->SetBackground(true, std::nullopt, true)
37-
->BuildUI(get_transform());
208+
m_SubscriptionText->Element()->SetText(ChatPlexService::ActiveSubscription());
209+
});
38210
}
39211

40212
} ///< namespace CP_SDK::UI::Views

0 commit comments

Comments
 (0)