Skip to content

Commit

Permalink
Feat/usermessages (#26)
Browse files Browse the repository at this point in the history
* Implemented showing UserMessages from server in modal;

* Bump version;
  • Loading branch information
k-karuna authored Nov 24, 2021
1 parent 73cc974 commit 6c40c7b
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 59 deletions.
6 changes: 3 additions & 3 deletions Atomex.Client.Desktop.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<OutputType>Exe</OutputType>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
<DefaultItemExcludes>$(DefaultItemExcludes);atomex.client.core\**</DefaultItemExcludes>
<UseAppHost>true</UseAppHost>
<ApplicationIcon>Resources/Images/logo_dark_256x256.ico</ApplicationIcon>

<AssemblyVersion>1.1.56</AssemblyVersion>
<Version>1.1.56</Version>
<AssemblyVersion>1.1.57</AssemblyVersion>
<Version>1.1.57</Version>
</PropertyGroup>
<ItemGroup>
<AvaloniaResource Include="Assets\**"/>
Expand Down
135 changes: 83 additions & 52 deletions ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Input;
using Atomex.Client.Desktop.Controls;
using Atomex.Client.Desktop.Common;
using Atomex.Client.Desktop.Properties;
using Atomex.Common;
using ReactiveUI;
using Atomex.Services;
Expand All @@ -27,7 +27,7 @@ private void ShowContent(ViewModelBase content)
}
}

public void ShowStart()
private void ShowStart()
{
ShowContent(new StartViewModel(ShowContent, ShowStart, AtomexApp, this));
}
Expand Down Expand Up @@ -56,21 +56,20 @@ public MainWindowViewModel(IAtomexApp app, IMainView mainView = null)
{
AtomexApp = app ?? throw new ArgumentNullException(nameof(app));
MainWalletVM = new WalletMainViewModel(AtomexApp);

SubscribeToServices();

if (mainView != null)
{
MainView = mainView;
MainView.MainViewClosing += (sender, args) => Closing(args);
MainView.Inactivity += InactivityHandler;
}

ShowStart();
}

public static IAtomexApp AtomexApp { get; private set; }
public IMainView MainView { get; set; }
private static IAtomexApp AtomexApp { get; set; }
private IMainView MainView { get; set; }


private bool _hasAccount;
Expand All @@ -84,14 +83,15 @@ public bool HasAccount
if (_hasAccount)
{
ShowContent(MainWalletVM);

var currencies = AtomexApp.Account.Currencies.ToList();
var a = 5;

if (AccountRestored)
{
App.DialogService.Show(new RestoreDialogViewModel(AtomexApp));
AccountRestored = false;
var restoreViewModel = new RestoreDialogViewModel(AtomexApp)
{
OnRestored = () => AccountRestored = false
};

App.DialogService.Show(restoreViewModel);
}
}

Expand All @@ -100,9 +100,7 @@ public bool HasAccount
}

public bool AccountRestored { get; set; }

private bool _updatesReady;

public bool UpdatesReady => HasUpdates && UpdateDownloadProgress == 100;
public bool IsDownloadingUpdate => HasUpdates && UpdateDownloadProgress > 0 && UpdateDownloadProgress < 100;

Expand Down Expand Up @@ -182,6 +180,8 @@ private void OnTerminalChangedEventHandler(object sender, AtomexClientChangedEve
// auto sign out after timeout
if (MainView != null && account.UserSettings.AutoSignOut)
MainView.StartInactivityControl(TimeSpan.FromMinutes(account.UserSettings.PeriodOfInactivityInMin));

StartLookingForUserMessages(TimeSpan.FromSeconds(90));
}


Expand All @@ -202,6 +202,8 @@ private async void OnUpdateClick()

private bool _userIgnoreActiveSwaps { get; set; }

private UnlockViewModel _unlockViewModel { get; set; }

private async Task SignOut(bool withAppUpdate = false)
{
try
Expand All @@ -226,14 +228,11 @@ private async Task SignOut(bool withAppUpdate = false)
return;
}

_ = Dispatcher.UIThread.InvokeAsync(() =>
{
App.DialogService.Close();
});
_ = Dispatcher.UIThread.InvokeAsync(() => { App.DialogService.Close(); });

AtomexApp.UseAtomexClient(null);
_userIgnoreActiveSwaps = false;

ShowStart();
}
catch (Exception e)
Expand All @@ -242,33 +241,6 @@ private async Task SignOut(bool withAppUpdate = false)
}
}

private bool _forcedClose;

private async void Closing(CancelEventArgs args)
{
// if (AtomexApp.Account == null || _forcedClose)
// return;
//
// args.Cancel = true;
//
// try
// {
// await Task.Yield();
//
// var cancel = await WhetherToCancelClosingAsync();
//
// if (!cancel)
// {
// _forcedClose = true;
// MainView.Close();
// }
// }
// catch (Exception e)
// {
// Log.Error(e, "Closing error");
// }
}

private async Task<bool> HasActiveSwapsAsync()
{
var swaps = await AtomexApp.Account
Expand Down Expand Up @@ -301,13 +273,13 @@ private void InactivityHandler(object sender, EventArgs args)

var accountName = new DirectoryInfo(accountDirectory).Name;

var unlockViewModel = new UnlockViewModel(accountName, password =>
_unlockViewModel = new UnlockViewModel(accountName, password =>
{
var clientType = ClientType.Unknown;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) clientType = ClientType.AvaloniaWindows;
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) clientType = ClientType.AvaloniaMac;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) clientType = ClientType.AvaloniaLinux;

var _ = Account.LoadFromFile(
pathToAccount: pathToAccount,
password: password,
Expand All @@ -318,10 +290,11 @@ private void InactivityHandler(object sender, EventArgs args)
var wasClosed = false;
Dispatcher.UIThread.InvokeAsync(() =>
{
wasClosed = App.DialogService.Close();;
wasClosed = App.DialogService.Close();
;
});
unlockViewModel.Unlocked += (s, a) =>

_unlockViewModel.Unlocked = () =>
{
ShowContent(MainWalletVM);

Expand All @@ -332,9 +305,9 @@ private void InactivityHandler(object sender, EventArgs args)
};


ShowContent(unlockViewModel);
ShowContent(_unlockViewModel);
}

public bool IsLinux { get; } = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);

public void CloseDialog()
Expand All @@ -346,5 +319,63 @@ private void DesignerMode()
{
HasAccount = true;
}

private void StartLookingForUserMessages(TimeSpan delayInterval)
{
var userId = Atomex.ViewModels.Helpers.GetUserId(AtomexApp.Account);

_ = Task.Run(async () =>
{
while (_hasAccount)
{
if (AccountRestored) continue;

var messages = await Atomex.ViewModels.Helpers.GetUserMessages(userId);

foreach (var message in messages.Where(message => !message.IsReaded))
{
if (Content is UnlockViewModel)
{
_unlockViewModel.Unlocked = () =>
{
_ = Dispatcher.UIThread.InvokeAsync(() =>
{
ShowContent(MainWalletVM);

App.DialogService.Show(
MessageViewModel.Success(
title: Resources.CvWarning,
text: message.Message,
nextAction: async () =>
{
await Atomex.ViewModels.Helpers.MarkUserMessageReaded(message.Id);
App.DialogService.Close();
}));
});
};
}
else
{
_ = Dispatcher.UIThread.InvokeAsync(() =>
{
App.DialogService.Show(
MessageViewModel.Success(
title: Resources.CvWarning,
text: message.Message,
nextAction: async () =>
{
await Atomex.ViewModels.Helpers.MarkUserMessageReaded(message.Id);
App.DialogService.Close();
}));
});
}

break;
}

await Task.Delay(delayInterval);
}
});
}
}
}
9 changes: 9 additions & 0 deletions ViewModels/MessageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ public static MessageViewModel Success(string text, Action nextAction) =>
nextTitle: Resources.SvOk,
backAction: null,
nextAction: nextAction);

public static MessageViewModel Success(string title, string text, Action nextAction) =>
new MessageViewModel(
title: title,
text: text,
backTitle: null,
nextTitle: Resources.SvOk,
backAction: null,
nextAction: nextAction);

public static MessageViewModel Message(string title, string text, Action backAction) =>
new MessageViewModel(
Expand Down
2 changes: 1 addition & 1 deletion ViewModels/MyWalletsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public MyWalletsViewModel(
});
}, () => ShowContent(this));

unlockViewModel.Unlocked += (sender, args) =>
unlockViewModel.Unlocked = () =>
{
var atomexClient = new WebSocketAtomexClient(
configuration: App.Configuration,
Expand Down
2 changes: 2 additions & 0 deletions ViewModels/RestoreDialogViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Atomex.Client.Desktop.ViewModels
{
public class RestoreDialogViewModel : ViewModelBase
{
public Action? OnRestored;
private IAtomexApp App;
private CancellationTokenSource cancellation;

Expand Down Expand Up @@ -101,6 +102,7 @@ await Task.Run(() =>
hdWalletScanner.ScanAsync(currency.Name, cancellationToken: cancellation.Token))),
cancellation.Token);

OnRestored?.Invoke();
Log.Information($"Scan {restoringEntityTitle} done");
}
catch (OperationCanceledException)
Expand Down
4 changes: 2 additions & 2 deletions ViewModels/UnlockViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Atomex.Client.Desktop.ViewModels
{
public class UnlockViewModel : ViewModelBase
{
public event EventHandler Unlocked;
public Action? Unlocked;
public event EventHandler<ErrorEventArgs> Error;
public PasswordControlViewModel PasswordVM { get; set; }
public string WalletName { get; set; }
Expand Down Expand Up @@ -102,7 +102,7 @@ private async void OnUnlockClick()

PasswordVM.StringPass = string.Empty;

Unlocked?.Invoke(this, EventArgs.Empty);
Unlocked?.Invoke();
}

private Action GoBack;
Expand Down
2 changes: 1 addition & 1 deletion atomex.client.core

0 comments on commit 6c40c7b

Please sign in to comment.