1
0
mirror of https://github.com/proxysu/ProxySU.git synced 2024-11-22 21:26:09 +03:00
ProxySU/ProxySuper.Core/ViewModels/HomeViewModel.cs

101 lines
3.0 KiB
C#
Raw Normal View History

2021-05-16 04:12:47 +03:00
using MvvmCross.Commands;
using MvvmCross.Navigation;
2021-05-14 14:07:19 +03:00
using MvvmCross.ViewModels;
using Newtonsoft.Json;
using ProxySuper.Core.Models;
2021-05-16 04:12:47 +03:00
using ProxySuper.Core.Models.Hosts;
using ProxySuper.Core.Models.Projects;
2021-05-14 14:07:19 +03:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2021-05-16 11:29:37 +03:00
using System.Runtime.CompilerServices;
2021-05-14 14:07:19 +03:00
using System.Text;
using System.Threading.Tasks;
namespace ProxySuper.Core.ViewModels
{
public class HomeViewModel : MvxViewModel
{
private readonly IMvxNavigationService _navigationService;
public HomeViewModel(IMvxNavigationService navigationService)
{
_navigationService = navigationService;
ReadRecords();
2021-05-22 12:14:27 +03:00
_navigationService.AfterClose += _navigationService_AfterClose;
}
private void _navigationService_AfterClose(object sender, MvvmCross.Navigation.EventArguments.IMvxNavigateEventArgs e)
{
if (e.ViewModel is XrayEditorViewModel ||
e.ViewModel is TrojanGoEditorViewModel)
{
SaveToJson();
}
2021-05-14 14:07:19 +03:00
}
2021-05-16 11:29:37 +03:00
public void ReadRecords()
2021-05-14 14:07:19 +03:00
{
2021-05-22 12:14:27 +03:00
List<Record> records = new List<Record>();
if (File.Exists("Data/Record.json"))
{
var json = File.ReadAllText("Data/Record.json");
records = JsonConvert.DeserializeObject<List<Record>>(json);
}
2021-05-14 14:07:19 +03:00
this.Records = new MvxObservableCollection<Record>();
2021-05-16 04:12:47 +03:00
2021-05-14 14:07:19 +03:00
records.ForEach(item =>
{
2021-05-16 04:12:47 +03:00
if (string.IsNullOrEmpty(item.Id))
{
item.Id = Guid.NewGuid().ToString();
}
2021-05-14 14:07:19 +03:00
this.Records.Add(item);
});
}
2021-05-22 12:14:27 +03:00
public void SaveToJson()
2021-05-16 11:29:37 +03:00
{
2021-05-22 12:14:27 +03:00
var json = JsonConvert.SerializeObject(this);
2021-05-16 11:29:37 +03:00
File.WriteAllText("Data/Record.json", json);
}
2021-05-14 14:07:19 +03:00
public MvxObservableCollection<Record> Records { get; set; }
2021-05-16 04:12:47 +03:00
public IMvxCommand AddXrayCommand => new MvxAsyncCommand(AddXrayRecord);
2021-05-16 11:29:37 +03:00
public IMvxCommand AddTrojanGoCommand => new MvxAsyncCommand(AddTrojanGoRecord);
2021-05-16 04:12:47 +03:00
public async Task AddXrayRecord()
{
Record record = new Record();
record.Id = Guid.NewGuid().ToString();
record.Host = new Host();
record.XraySettings = new XraySettings();
var result = await _navigationService.Navigate<XrayEditorViewModel, Record, Record>(record);
if (result == null) return;
Records.Add(result);
2021-05-22 12:14:27 +03:00
SaveToJson();
2021-05-16 04:12:47 +03:00
}
2021-05-16 11:29:37 +03:00
public async Task AddTrojanGoRecord()
{
Record record = new Record();
record.Id = Guid.NewGuid().ToString();
record.Host = new Host();
record.TrojanGoSettings = new TrojanGoSettings();
var result = await _navigationService.Navigate<TrojanGoEditorViewModel, Record, Record>(record);
if (result == null) return;
Records.Add(result);
}
2021-05-14 14:07:19 +03:00
}
}