-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cpp
More file actions
1035 lines (861 loc) · 40.7 KB
/
Copy pathMainWindow.xaml.cpp
File metadata and controls
1035 lines (861 loc) · 40.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "pch.h"
#include "MainWindow.xaml.h"
#if __has_include("MainWindow.g.cpp")
#include "MainWindow.g.cpp"
#endif
#include <thread>
#include <chrono>
#include <ctime>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <WebView2.h>
#include <windows.h>
#include <tlhelp32.h>
#include <winrt/Windows.Data.Json.h>
#include <winrt/Windows.Storage.Streams.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Microsoft.UI.Xaml.h>
#include <winrt/Microsoft.UI.Xaml.Controls.h>
#include <winrt/Windows.UI.Xaml.Media.h>
#include <winrt/Windows.UI.Xaml.Media.Imaging.h>
#include <winrt/Windows.Web.Http.Headers.h>
#include <winrt/Windows.Security.Cryptography.h>
#include "winrt/Windows.Globalization.DateTimeFormatting.h"
#include "winrt/Microsoft.UI.Dispatching.h"
#include <winrt/Windows.Web.Http.h>
#include <nlohmann/json.hpp>
#include <iostream>
#include <sapi.h>
#include <wincrypt.h>
#pragma comment(lib, "crypt32.lib")
#pragma warning(push)
#pragma warning(disable: 4996)
#include <sphelper.h>
#pragma warning(pop)
using namespace winrt;
using namespace winrt::Windows;
using namespace winrt::Microsoft::UI::Xaml;
using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::Web::Http;
using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::Storage::Streams;
using namespace std::chrono;
using namespace Windows::Globalization::DateTimeFormatting;
using json = nlohmann::json;
namespace winrt::Personal_Assistant::implementation
{
// Structure
struct User {
std::wstring username;
std::wstring password;
std::wstring name;
int age;
bool loggedIn = false;
int totalexpense = 0;
std::vector<winrt::hstring> expenseVector;
std::vector<std::pair<winrt::hstring, hstring>> uncompletedTasks;
std::vector<std::pair<hstring, hstring>> completedTasks;
};
// Global Variables
std::vector<User> users;
bool LoggedIn = false;
std::wstring LoggedInUsername;
std::wstring LoggedInName;
int LoggedInAge;
int totalexpense;
std::vector<hstring> ExpenseVector;
std::vector<std::pair<hstring, hstring>> UncompletedTasks;
std::vector<std::pair<hstring, hstring>> CompletedTasks;
// String Conversions
std::string WStringToString(const std::wstring& wstr) {
return to_string(wstr);
}
std::wstring StringToWString(const std::string& str) {
return to_hstring(str).c_str();
}
void MainWindow::RefreshTaskList()
{
UncompletedTaskList().Items().Clear();
for (const auto& task : UncompletedTasks)
{
auto taskObject = Collections::PropertySet();
taskObject.Insert(L"Title", box_value(task.first));
taskObject.Insert(L"Description", box_value(task.second));
UncompletedTaskList().Items().Append(taskObject);
}
CompletedTaskList().Items().Clear();
for (const auto& task : CompletedTasks)
{
auto taskObject = Collections::PropertySet();
taskObject.Insert(L"Title", box_value(task.first));
taskObject.Insert(L"Description", box_value(task.second));
CompletedTaskList().Items().Append(taskObject);
}
}
void MainWindow::RefreshExpenses() {
ExpenseList().Items().Clear();
for (const auto& item : ExpenseVector)
{
ExpenseList().Items().Append(box_value(item));
}
TotalExpense().Text(std::to_wstring(totalexpense));
}
const std::string BASE64_STANDARD = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const std::string BASE64_CUSTOM = "QWERTYUIOPASDFGHJKLZXCVBNMghjklzxcvbnmqwertyuiopasdf5678901234-*_";
std::string Base64Encode(const std::string& input) {
DWORD base64Length = 0;
CryptBinaryToStringA(reinterpret_cast<const BYTE*>(input.data()), input.size(), CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, NULL, &base64Length);
std::string base64(base64Length, '\0');
CryptBinaryToStringA(reinterpret_cast<const BYTE*>(input.data()), input.size(), CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, &base64[0], &base64Length);
// Replace standard Base64 chars with custom ones
for (char& c : base64) {
size_t index = BASE64_STANDARD.find(c);
if (index != std::string::npos) {
c = BASE64_CUSTOM[index];
}
}
return base64;
}
std::string Base64Decode(const std::string& input) {
std::string modifiedBase64 = input;
// Replace custom chars back to standard Base64
for (char& c : modifiedBase64) {
size_t index = BASE64_CUSTOM.find(c);
if (index != std::string::npos) {
c = BASE64_STANDARD[index];
}
}
DWORD binaryLength = 0;
CryptStringToBinaryA(modifiedBase64.c_str(), modifiedBase64.size(), CRYPT_STRING_BASE64, NULL, &binaryLength, NULL, NULL);
std::vector<BYTE> binaryData(binaryLength);
CryptStringToBinaryA(modifiedBase64.c_str(), modifiedBase64.size(), CRYPT_STRING_BASE64, binaryData.data(), &binaryLength, NULL, NULL);
return std::string(binaryData.begin(), binaryData.end());
}
std::string Base64EncodeInt(int num) {
return Base64Encode(std::to_string(num));
}
int Base64DecodeInt(const std::string& encoded) {
return std::stoi(Base64Decode(encoded));
}
void MainWindow::UpdateGreeting()
{
// Get the current time
auto now = system_clock::to_time_t(system_clock::now());
struct tm localTime;
localtime_s(&localTime, &now);
// Format date separately
wchar_t dateBuffer[50];
wcsftime(dateBuffer, sizeof(dateBuffer), L"%A, %B %d, %Y", &localTime);
std::wstring dateStr = dateBuffer;
// Format time separately
wchar_t timeBuffer[20];
wcsftime(timeBuffer, sizeof(timeBuffer), L"%I:%M %p", &localTime);
std::wstring timeStr = timeBuffer;
// Determine greeting
int hour = localTime.tm_hour;
std::wstring greeting;
if (hour < 12){
greeting = L"Good Morning";
} else if (hour < 18) {
greeting = L"Good Afternoon";
} else {
greeting = L"Good Evening";
}
std::wstring TheName;
for (const auto& user : users) {
if (user.username == LoggedInUsername) {
TheName = user.name;
}
}
// Update TextBlock in UI thread
auto dispatcher = this->Dispatcher();
DispatcherQueue().TryEnqueue([this, dateStr, timeStr, TheName, greeting]()
{
GreetingTextBlock().Text(greeting + L" " + TheName + L"!");
DateBlock().Text(L"Date: " + dateStr);
TimeBlock().Text(L"Time: " + timeStr);
});
}
void MainWindow::StartProgressRingAnimation(Controls::ProgressRing progressRing, int delayMilliseconds, std::function<void()> onComplete)
{
progressRing.Visibility(Visibility::Visible);
progressRing.Value(0);
std::thread([this, progressRing, delayMilliseconds, onComplete]() {
for (int i = 0; i <= 100; ++i)
{
DispatcherQueue().TryEnqueue([progressRing, i]() {
progressRing.Value(i);
});
std::this_thread::sleep_for(std::chrono::milliseconds(delayMilliseconds));
}
DispatcherQueue().TryEnqueue([progressRing, onComplete]()
{
progressRing.Visibility(Visibility::Collapsed);
if (onComplete)
{
onComplete();
}
});
}).detach();
}
void ShowAuthError(Controls::InfoBar errorBar, Controls::InfoBarSeverity severity, hstring message)
{
errorBar.Severity(severity);
errorBar.Message(message);
errorBar.IsOpen(true);
}
void MainWindow::ShowErrorMessage(hstring const& message)
{
ErrorForMenu().Subtitle(message);
ErrorForMenu().IsOpen(true);
}
int ExtractAmountFromString(const hstring& expenseString)
{
std::wstring str(expenseString);
size_t pos = str.find(L" PKR - ");
if (pos == std::wstring::npos)
{
throw std::invalid_argument("Invalid string format");
}
std::wstring amountStr = str.substr(0, pos);
try
{
return std::stoi(amountStr);
}
catch (const std::invalid_argument&)
{
throw std::invalid_argument("Invalid amount format");
}
}
void MainWindow::OpenLink(hstring url) {
if (MyWebView2() != nullptr) {
MyWebView2().Source(Uri(url));
}
}
void MainWindow::Sound_Toggled(IInspectable const& sender, RoutedEventArgs const& e)
{
if (Sound().IsOn()) {
ElementSoundPlayer::State(ElementSoundPlayerState::On);
ElementSoundPlayer::SpatialAudioMode(ElementSpatialAudioMode::On);
}
else
{
ElementSoundPlayer::State(ElementSoundPlayerState::Off);
ElementSoundPlayer::SpatialAudioMode(ElementSpatialAudioMode::Off);
}
}
//File Handling
void to_json(json& j, const User& user) {
j = json{
{"username", Base64Encode(WStringToString(user.username))},
{"password", Base64Encode(WStringToString(user.password))},
{"name", Base64Encode(WStringToString(user.name))},
{"age", Base64EncodeInt(user.age)},
{"loggedIn", user.loggedIn},
{"totalexpense", Base64EncodeInt(user.totalexpense)},
{"expenseVector", json::array()},
{"uncompletedTasks", json::array()},
{"completedTasks", json::array()}
};
for (const auto& exp : user.expenseVector) {
j["expenseVector"].push_back(Base64Encode(WStringToString(exp.c_str())));
}
for (const auto& task : user.uncompletedTasks) {
j["uncompletedTasks"].push_back({
Base64Encode(WStringToString(task.first.c_str())),
Base64Encode(WStringToString(task.second.c_str()))
});
}
for (const auto& task : user.completedTasks) {
j["completedTasks"].push_back({
Base64Encode(WStringToString(task.first.c_str())),
Base64Encode(WStringToString(task.second.c_str()))
});
}
}
void from_json(const json& j, User& user) {
user.username = StringToWString(Base64Decode(j["username"].get<std::string>()));
user.password = StringToWString(Base64Decode(j["password"].get<std::string>()));
user.name = StringToWString(Base64Decode(j["name"].get<std::string>()));
user.age = Base64DecodeInt(j["age"].get<std::string>());
user.loggedIn = j.value("loggedIn", false);
user.totalexpense = Base64DecodeInt(j["totalexpense"].get<std::string>());
for (const auto& expense : j.value("expenseVector", json::array())) {
user.expenseVector.push_back(to_hstring(Base64Decode(expense.get<std::string>())));
}
for (const auto& task : j.value("uncompletedTasks", json::array())) {
user.uncompletedTasks.emplace_back(
to_hstring(Base64Decode(task[0].get<std::string>())),
to_hstring(Base64Decode(task[1].get<std::string>()))
);
}
for (const auto& task : j.value("completedTasks", json::array())) {
user.completedTasks.emplace_back(
to_hstring(Base64Decode(task[0].get<std::string>())),
to_hstring(Base64Decode(task[1].get<std::string>()))
);
}
}
void SaveUsers() {
OutputDebugString(L"\n===== Saving Users =====\n");
for (auto& user : users) {
if (user.username == LoggedInUsername) {
user.loggedIn = LoggedIn;
user.totalexpense = totalexpense;
user.expenseVector = ExpenseVector;
user.uncompletedTasks = UncompletedTasks;
user.completedTasks = CompletedTasks;
std::wstring debugInfo = L" Saving User Data: \n";
debugInfo += L"Username: " + user.username + L"\n";
debugInfo += L"Name: " + user.name + L"\n";
debugInfo += L"Age: " + std::to_wstring(user.age) + L"\n";
debugInfo += L"Total Expense: " + std::to_wstring(user.totalexpense) + L"\n";
debugInfo += L"Uncompleted Tasks Count: " + std::to_wstring(user.uncompletedTasks.size()) + L"\n";
debugInfo += L"Completed Tasks Count: " + std::to_wstring(user.completedTasks.size()) + L"\n";
OutputDebugString(debugInfo.c_str());
break;
}
}
std::ofstream file("C:\\Users\\rocky\\Documents\\users.json");
if (!file) {
OutputDebugString(L" Error: Unable to open users.json for writing!\n");
return;
}
try {
json j = users;
file << j.dump(4);
OutputDebugString(L" Users saved successfully!\n");
}
catch (const std::exception& e) {
std::wstring errorMsg = L" JSON Write Error: " + std::wstring(e.what(), e.what() + strlen(e.what())) + L"\n";
OutputDebugString(errorMsg.c_str());
}
}
void LoadUsers() {
OutputDebugString(L"\n===== Loading Users =====\n");
std::ifstream file("C:\\Users\\rocky\\Documents\\users.json");
if (!file) {
OutputDebugString(L" Error: users.json not found or unable to open!\n");
return;
}
json j;
try {
file >> j;
OutputDebugString(L" JSON file loaded successfully!\n");
}
catch (const std::exception& e) {
OutputDebugString(L" JSON Read Error! File might be corrupted.\n");
return;
}
try {
users = j.get<std::vector<User>>();
OutputDebugString(L" Users parsed successfully from JSON!\n");
}
catch (const std::exception& e) {
OutputDebugString(L" User Parse Error!\n");
return;
}
bool foundLoggedInUser = false;
for (auto& user : users) {
LoggedIn = true;
LoggedInUsername = user.username;
LoggedInName = user.name;
LoggedInAge = user.age;
totalexpense = user.totalexpense;
ExpenseVector = user.expenseVector;
UncompletedTasks = user.uncompletedTasks;
CompletedTasks = user.completedTasks;
std::wstring debugInfo = L" Loaded User Data: \n";
debugInfo += L"Username: " + user.username + L"\n";
debugInfo += L"Name: " + user.name + L"\n";
debugInfo += L"Age: " + std::to_wstring(user.age) + L"\n";
debugInfo += L"Total Expense: " + std::to_wstring(user.totalexpense) + L"\n";
debugInfo += L"Uncompleted Tasks Count: " + std::to_wstring(user.uncompletedTasks.size()) + L"\n";
debugInfo += L"Completed Tasks Count: " + std::to_wstring(user.completedTasks.size()) + L"\n";
OutputDebugString(debugInfo.c_str());
foundLoggedInUser = true;
break;
}
if (!foundLoggedInUser) {
OutputDebugString(L"⚠ No logged-in user found in JSON!\n");
}
}
// Visibility Functions
void MainWindow::Auth_Menu_SelectionChanged(IInspectable const&, Controls::NavigationViewSelectionChangedEventArgs const& args)
{
auto selectedItem = args.SelectedItem().try_as<Controls::NavigationViewItem>();
if (selectedItem)
{
auto tag = unbox_value_or<hstring>(selectedItem.Tag(), L"");
SignIn().Visibility(Visibility::Collapsed);
Registration().Visibility(Visibility::Collapsed);
ElementSoundPlayer::Play(ElementSoundKind::Show);
if (tag == L"SignIn")
{
ErrorForAuth().IsOpen(false);
SignInHeading().Margin(Thickness{ 0, 55, 107, 0 });
SignIn().Visibility(Visibility::Visible);
AuthMenuBackground().Height(380);
}
else if (tag == L"SignUp")
{
ErrorForAuth().IsOpen(false);
RegistrationHeading().Margin(Thickness{ 0, 57 ,105,0 });
Registration().Visibility(Visibility::Visible);
AuthMenuBackground().Height(425);
}
};
}
void MainWindow::MainMenu_SelectionChanged(IInspectable const&, Controls::NavigationViewSelectionChangedEventArgs const& args)
{
UpdateGreeting();
auto selectedItem = args.SelectedItem().try_as<Controls::NavigationViewItem>();
if (selectedItem)
{
auto tag = unbox_value_or<hstring>(selectedItem.Tag(), L"");
// Hide all pages first
Home().Visibility(Visibility::Collapsed);
LogOut().Visibility(Visibility::Collapsed);
Browser().Visibility(Visibility::Collapsed);
Chatbot().Visibility(Visibility::Collapsed);
ExpenseCalculator().Visibility(Visibility::Collapsed);
ToDoList().Visibility(Visibility::Collapsed);
ElementSoundPlayer::Play(ElementSoundKind::Show);
if (tag == L"HomePage")
{
Home().Visibility(Visibility::Visible);
}
else if (tag == L"AuthSettingsPage")
{
LogOut().Visibility(Visibility::Visible);
}
else if (tag == L"BrowserPage")
{
Browser().Visibility(Visibility::Visible);
}
else if (tag == L"ChatBotPage")
{
Chatbot().Visibility(Visibility::Visible);
}
else if (tag == L"ExpenseCalculatorPage")
{
ExpenseCalculator().Visibility(Visibility::Visible);
}
else if (tag == L"ToDoListPage")
{
ToDoList().Visibility(Visibility::Visible);
}
}
}
//Auth Functions
void MainWindow::SignInButton_Click(IInspectable const& sender, RoutedEventArgs const& e)
{
std::wstring enteredUsername = SignInUsername().Text().c_str();
std::wstring enteredPassword = SignInPassword().Password().c_str();
ErrorForAuth().IsOpen(false);
if (enteredUsername.empty() || enteredPassword.empty())
{
ShowAuthError(ErrorForAuth(), Controls::InfoBarSeverity::Error, L"Please Fill All The Fields!");
SignInHeading().Margin(Thickness{ 0, 4, 107, 0 });
return;
}
// Debug
OutputDebugString((L"Entered Username: " + enteredUsername + L"\n").c_str());
OutputDebugString((L"Entered Password: " + enteredPassword + L"\n").c_str());
bool userFound = false;
for (const auto& user : users)
{
// Debug
OutputDebugString((L"Checking User: " + user.username + L" | Password: " + user.password + L"\n").c_str());
if (user.username == enteredUsername)
{
userFound = true;
if (user.password == enteredPassword)
{
LoggedInUsername = enteredUsername;
UpdateGreeting();
ProgressRingBackground().Visibility(Visibility::Visible);
Auth().Visibility(Visibility::Collapsed);
StartProgressRingAnimation(ProgressRingAuth(), 20, [this]() {
// Successful Login
ShowAuthError(ErrorForAuth(), Controls::InfoBarSeverity::Success, L"Login Success!");
MainMenu().Visibility(Visibility::Visible);
LoggedIn - true;
if (!RememberMe().IsOn()) {
SignInUsername().Text(L"");
SignInPassword().Password(L"");
}
MenuBG().Visibility(Visibility::Visible);
ProgressRingBackground().Visibility(Visibility::Collapsed);
RefreshTaskList();
RefreshExpenses();
return;
});
}
else
{
// Incorrect Password
ShowAuthError(ErrorForAuth(), Controls::InfoBarSeverity::Error, L"Invalid Password!");
SignInHeading().Margin(Thickness{ 0, 4, 107, 0 });
return;
}
}
}
// User not found
if (!userFound)
{
ShowAuthError(ErrorForAuth(), Controls::InfoBarSeverity::Error, L"No User Found!");
SignInHeading().Margin(Thickness{ 0, 4, 107, 0 });
}
}
void MainWindow::RegistrationButton_Click(IInspectable const& sender, RoutedEventArgs const& e)
{
std::wstring username = RegistrationUsername().Text().c_str();
std::wstring password = RegistrationPassword().Password().c_str();
std::wstring fullname = RegistrationName().Text().c_str();
int age = 25;
ErrorForAuth().IsOpen(false);
//Already Exists Error
for (const auto& user : users)
{
if (user.username == username)
{
RegistrationHeading().Margin(Thickness{ 0, 6 ,105,0 });
ShowAuthError(ErrorForAuth(), Controls::InfoBarSeverity::Error, L"Username Already Exists!");
return;
}
}
//Empty Error
if (username.empty() || password.empty() || fullname.empty() || age <= 0)
{
RegistrationHeading().Margin(Thickness{ 0, 6 ,105,0 });
ShowAuthError(ErrorForAuth(), Controls::InfoBarSeverity::Error, L"All fields must be filled correctly!");
return;
}
else if (password.length() < 7)
{
RegistrationHeading().Margin(Thickness{ 0, 6 ,105,0 });
ShowAuthError(ErrorForAuth(), Controls::InfoBarSeverity::Error, L"Password must be at least 7 characters!");
return;
}
// Saving The User
User newUser = { username, password, fullname, age };
users.push_back(newUser);
SaveUsers();
RegistrationUsername().Text(L"");
RegistrationName().Text(L"");
RegistrationPassword().Password(L"");
// Show success message
RegistrationHeading().Margin(Thickness{ 0, 6 ,105,0 });
ShowAuthError(ErrorForAuth(), Controls::InfoBarSeverity::Success, L"Registration Successful!");
}
void MainWindow::LogOutButton_Click(IInspectable const& sender, RoutedEventArgs const& e)
{
LoggedIn = false;
SaveUsers();
LoggedInUsername = L"";
MainMenu().Visibility(Visibility::Collapsed);
ErrorForAuth().IsOpen(false);
ProgressRingBackground().Visibility(Visibility::Visible);
MyWebView2().Source(Uri(L"https://www.google.com"));
auto flyout = LogOutButton().Flyout().as<Controls::Flyout>();
if (flyout)
{
flyout.Hide();
}
StartProgressRingAnimation(ProgressRingAuth(), 20, [this]() {
MenuBG().Visibility(Visibility::Collapsed);
Auth().Visibility(Visibility::Visible);
ProgressRingBackground().Visibility(Visibility::Collapsed);
});
}
// Created For Both Assistant And UI
void MainWindow::AddExpense(double amount, hstring reason) {
int expenseamount = amount;
hstring NewExpense = std::to_wstring(expenseamount) + L" PKR - " + reason;
ExpenseVector.insert(ExpenseVector.begin(), NewExpense);
// Update total expense
totalexpense += expenseamount;
RefreshExpenses();
SaveUsers();
}
//Expense Calculator Functions
void MainWindow::AddExpense_Click(IInspectable const&, RoutedEventArgs const&)
{
int expenseamount = ExpenseAmount().Value();
if (expenseamount <= 0 || std::isnan(expenseamount) || ExpenseReason().Text().empty()) {
ShowErrorMessage(L"Please Fill All The Fields Valid!");
return;
}
AddExpense(ExpenseAmount().Value(), ExpenseReason().Text());
ExpenseAmount().Value(0);
ExpenseReason().Text(L"");
}
void MainWindow::DeleteExpense_Click(IInspectable const& sender, RoutedEventArgs const&)
{
auto button = sender.try_as<Controls::Button>();
if (!button) return;
auto itemTag = button.Tag().try_as<hstring>();
if (!itemTag || itemTag->empty()) return;
auto it = std::find(ExpenseVector.begin(), ExpenseVector.end(), *itemTag);
if (it != ExpenseVector.end())
{
// Update total expense
int amount = ExtractAmountFromString(*it);
totalexpense -= amount;
ExpenseVector.erase(it);
RefreshExpenses();
}
}
// Created For Both Assistant And UI
void MainWindow::CreateTask(hstring taskName, hstring taskDesc) {
hstring Task_name = taskName;
hstring Task_Description = taskDesc;
UncompletedTasks.emplace_back(Task_name, Task_Description);
RefreshTaskList();
SaveUsers();
}
void MainWindow::AddTaskButton_Click(IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& e)
{
hstring Task_name = TaskName().Text();
hstring Task_Description = TaskDescription().Text();
if (Task_name.empty() || Task_Description.empty()) {
ShowErrorMessage(L"Please Fill All The Fields Valid!");
return;
}
CreateTask(Task_name, Task_Description);
TaskDescription().Text(L"");
TaskName().Text(L"");
}
// Complete Button Click Handler
void MainWindow::CompleteTask_Click(
IInspectable const& sender,
Microsoft::UI::Xaml::RoutedEventArgs const&)
{
auto button = sender.try_as<Microsoft::UI::Xaml::Controls::AppBarButton>();
if (button)
{
auto taskTag = button.Tag().try_as<hstring>();
if (taskTag)
{
for (auto it = UncompletedTasks.begin(); it != UncompletedTasks.end(); ++it)
{
if (it->first == *taskTag)
{
CompletedTasks.push_back(*it);
UncompletedTasks.erase(it);
break;
}
}
RefreshTaskList();
}
}
}
void MainWindow::DeleteCompletedTask_Click(IInspectable const& sender, RoutedEventArgs const&)
{
auto button = sender.try_as<Controls::Button>();
if (!button) return;
auto itemTag = button.Tag().try_as<hstring>();
if (!itemTag || itemTag->empty()) return;
auto it = std::find_if(CompletedTasks.begin(), CompletedTasks.end(),
[&itemTag](const std::pair<hstring, hstring>& task)
{
return task.first == *itemTag;
});
if (it != CompletedTasks.end())
{
CompletedTasks.erase(it);
RefreshTaskList();
}
}
fire_and_forget MainWindow::SendMessageToGemini(hstring userMessage) {
auto lifetime = get_strong();
try {
HttpClient httpClient;
HttpRequestMessage request(
HttpMethod::Post(),
Uri(L"https://generativelanguage.googleapis.com/v1beta/tunedModels/YOUR_MODEL:generateContent?key=YOUR_API_KEY")
);
std::wstring jsonBody = LR"({"contents":[{"parts":[{"text": ")" +
std::wstring(userMessage.c_str()) + LR"("}]}]})";
HttpStringContent jsonContent(
hstring(jsonBody),
Windows::Storage::Streams::UnicodeEncoding::Utf8,
L"application/json"
);
request.Content(jsonContent);
HttpResponseMessage response = co_await httpClient.SendRequestAsync(request);
if (response.StatusCode() == HttpStatusCode::Ok) {
hstring responseText = co_await response.Content().ReadAsStringAsync();
OutputDebugStringW((L"Raw API Response: " + std::wstring(responseText.c_str())).c_str());
Windows::Data::Json::JsonObject jsonResponse;
if (Windows::Data::Json::JsonObject::TryParse(responseText, jsonResponse)) {
if (jsonResponse.HasKey(L"candidates")) {
auto candidatesArray = jsonResponse.GetNamedArray(L"candidates");
if (candidatesArray.Size() > 0) {
auto firstCandidate = candidatesArray.GetAt(0).GetObject();
if (firstCandidate.HasKey(L"content")) {
auto contentObject = firstCandidate.GetNamedObject(L"content");
if (contentObject.HasKey(L"parts")) {
auto partsArray = contentObject.GetNamedArray(L"parts");
if (partsArray.Size() > 0) {
auto textResponse = partsArray.GetAt(0).GetObject().GetNamedString(L"text");
std::wstring textStd = textResponse.c_str();
// **Extract text before JSON if any**
size_t jsonStart = textStd.find(L"{");
size_t jsonEnd = textStd.rfind(L"}");
std::wstring preJsonText;
std::wstring validJson;
preJsonText = textStd.substr(0, jsonStart);
validJson = textStd.substr(jsonStart, jsonEnd - jsonStart + 1);
// **Sanitize JSON by removing newlines and carriage returns**
std::wstring sanitizedJson;
for (wchar_t c : validJson) {
if (c != L'\n' && c != L'\r') {
sanitizedJson += c;
}
}
std::wstring sanitizedpreJsonText;
for (wchar_t c : preJsonText) {
if (c != L'```') {
sanitizedpreJsonText += c;
}
else {
break;
}
}
// **Debug Output: Print extracted pre-JSON text**
OutputDebugStringW((L"Pre-JSON Text: " + preJsonText).c_str());
// **Debug Output: Print sanitized JSON**
OutputDebugStringW((L"Sanitized Extracted JSON: " + sanitizedJson).c_str());
if (jsonStart != std::wstring::npos && jsonEnd != std::wstring::npos) {
Windows::Data::Json::JsonObject parsedTextJson;
if (Windows::Data::Json::JsonObject::TryParse(hstring(sanitizedJson), parsedTextJson)) {
hstring contentText = parsedTextJson.GetNamedString(L"content", L"");
hstring speakText = parsedTextJson.GetNamedString(L"speak", L"");
// Store the pre-JSON text if needed for later use
ChatbotResponseText().Text(contentText);
// **Process Actions**
if (parsedTextJson.HasKey(L"actionType")) {
hstring actionType = parsedTextJson.GetNamedString(L"actionType");
OutputDebugStringW((L"Action Type: " + actionType).c_str());
if (parsedTextJson.HasKey(L"parameters")) {
Windows::Data::Json::JsonObject parameters = parsedTextJson.GetNamedObject(L"parameters");
if (actionType == L"createTask") {
hstring taskName = parameters.GetNamedString(L"taskName", L"");
hstring taskDesc = parameters.GetNamedString(L"taskDesc", L"");
OutputDebugStringW((L"Task Name: " + taskName + L", Task Desc: " + taskDesc).c_str());
ChatbotResponseText().Text(L"Task Created: " + taskName + L" - " + taskDesc);
CreateTask(taskName, taskDesc);
auto ManuItem = MainMenu().MenuItems().GetAt(2).try_as<Controls::NavigationViewItem>();
MainMenu().SelectedItem(ManuItem);
}
else if (actionType == L"addExpense") {
double expenseAmount = parameters.GetNamedNumber(L"expenseAmount", 0);
hstring expenseReason = parameters.GetNamedString(L"expenseReason", L"");
if (!expenseReason.empty()) {
OutputDebugStringW((L"Expense Amount: $" + to_hstring(expenseAmount) + L", Reason: " + expenseReason).c_str());
ChatbotResponseText().Text(L"Expense Added: $" + to_hstring(expenseAmount) + L" for " + expenseReason);
AddExpense(expenseAmount, expenseReason);
} else {
hstring expenseCategory = parameters.GetNamedString(L"expenseCategory", L"");
OutputDebugStringW((L"Expense Amount: $" + to_hstring(expenseAmount) + L", Reason: " + expenseCategory).c_str());
ChatbotResponseText().Text(L"Expense Added: $" + to_hstring(expenseAmount) + L" for " + expenseCategory);
AddExpense(expenseAmount, expenseCategory);
}
auto ManuItem = MainMenu().MenuItems().GetAt(1).try_as<Controls::NavigationViewItem>();
MainMenu().SelectedItem(ManuItem);
}
else if (actionType == L"openLink") {
hstring url = parameters.GetNamedString(L"url", L"");
OutputDebugStringW((L"Opening Link: " + url).c_str());
ChatbotResponseText().Text(L"Opening: " + url);
OpenLink(url);
auto ManuItem = MainMenu().MenuItems().GetAt(3).try_as<Controls::NavigationViewItem>();
MainMenu().SelectedItem(ManuItem);
}
}
else {
OutputDebugStringW(L"Error: 'parameters' key missing in response.");
}
}
co_return;
}
else {
OutputDebugStringW(L"Error: Extracted JSON is invalid.");
}
}
else {
// **Handle plain text response**
OutputDebugStringW(L"Plain text response detected.");
ChatbotResponseText().Text(sanitizedpreJsonText);
}
}
}
}
}
}
}
else {
// **If JSON parsing fails, assume it's plain text**
OutputDebugStringW(L"Response is not JSON. Assuming plain text.");
ChatbotResponseText().Text(responseText);
}
}
else {
ChatbotResponseText().Text(L"Error: HTTP Request Failed!");
}
}
catch (const hresult_error& ex) {
ChatbotResponseText().Text(L"Critical Error: " + ex.message());
}
catch (const std::exception& ex) {
ChatbotResponseText().Text(L"Exception: " + to_hstring(ex.what()));
}
}
void MainWindow::SendButton_Click(const IInspectable&, const Microsoft::UI::Xaml::RoutedEventArgs&) {
hstring userInput = UserInputTextBox().Text();
if (userInput.empty()) {
ChatbotResponseText().Text(L"Please enter a message.");
return;
}
SendMessageToGemini(userInput);
}
//on close
void MainWindow::OnCloseRequested()
{
if (MyWebView2() != nullptr)
{
auto coreWebView2 = MyWebView2();
if (coreWebView2)
{
coreWebView2.Close();
}
MyWebView2(nullptr);
}
SaveUsers();