Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Reactive;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using ReactiveUI;
- namespace VKGroupPostsView.ViewModels
- {
- public class VkLoginViewModel:ViewModelBase
- {
- private bool _LoginPanelVisible = true;
- private bool _TwoFactorCodePanelVisible = false;
- public ulong AppID = 6121396;
- public string LoginText { get; set; }
- public string PasswordText { get; set; }
- public string? TwoAuthCodeText { get; set; } = null;
- public bool TwoFactorCodePanelVisible {
- get => _TwoFactorCodePanelVisible;
- set
- {
- this.RaiseAndSetIfChanged(ref _TwoFactorCodePanelVisible, value);
- }
- }
- public bool LoginPanelVisible {
- get => _LoginPanelVisible;
- set
- {
- this.RaiseAndSetIfChanged(ref _LoginPanelVisible, value);
- }
- }
- public bool SendCode = false;
- public ReactiveCommand<Unit, Unit> AuthCommand { get; }
- public ReactiveCommand<Unit, Unit> SendCodeCommand { get; }
- public VkLoginViewModel()
- {
- AuthCommand = ReactiveCommand.Create(()=>
- {
- Auth();
- });
- SendCodeCommand = ReactiveCommand.Create(() =>
- {
- SendCode = true;
- });
- }
- public async void Auth()
- {
- VkNet.VkApi vkApi = new VkNet.VkApi();
- var auth= vkApi.AuthorizeAsync(new VkNet.Model.ApiAuthParams()
- {
- ApplicationId = AppID,
- Login = LoginText,
- Password = PasswordText,
- TwoFactorSupported = true,
- TwoFactorAuthorization = new Func<string?>(() =>
- {
- LoginPanelVisible = false;
- TwoFactorCodePanelVisible = true;
- Thread.Sleep(300);
- if(SendCode&TwoAuthCodeText!=null)
- {
- string? code = TwoAuthCodeText;
- TwoAuthCodeText=null;
- SendCode = false;
- return code;
- }
- return "";
- })
- }).GetAwaiter();
- auth.OnCompleted(() =>
- {
- try
- {
- auth.GetResult();
- Debug.WriteLine("AUTH OK");
- }
- catch (Exception ex) {
- Debug.WriteLine(ex.Message);
- }
- });
- }
- }
- }
- Window
- <UserControl xmlns="https://github.com/avaloniaui"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:vm="using:VKGroupPostsView.ViewModels"
- mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
- x:Class="VKGroupPostsView.Views.VkLoginView">
- <UserControl.DataContext>
- <vm:VkLoginViewModel></vm:VkLoginViewModel>
- </UserControl.DataContext>
- <Grid>
- <TextBlock Margin="10" Text="Авторизация" HorizontalAlignment="Center"></TextBlock>
- <StackPanel IsVisible="{Binding LoginPanelVisible}" Margin="50">
- <TextBlock HorizontalAlignment="Center" Text="Логин"></TextBlock>
- <TextBox Margin="0,20,0,0" Text="{Binding LoginText}"></TextBox>
- <TextBlock HorizontalAlignment="Center" Margin="0,20,0,0" Text="Пароль"></TextBlock>
- <TextBox Margin="0,20,0,0" Text="{Binding PasswordText}"></TextBox>
- <Button Command="{Binding AuthCommand}" HorizontalAlignment="Center" Margin="20"> Войти</Button>
- </StackPanel>
- <StackPanel IsVisible="{ Binding TwoFactorCodePanelVisible}" Margin="50">
- <TextBlock HorizontalAlignment="Center" Text="Введите код авторизации"></TextBlock>
- <TextBox Margin="0,20,0,0" Text="{Binding TwoAuthCodeText}"></TextBox>
- <Button Command="{Binding SendCodeCommand}" HorizontalAlignment="Center" Margin="20"> отправить</Button>
- </StackPanel>
- </Grid>
- </UserControl>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement