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

344 lines
12 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;
2021-05-23 09:24:13 +03:00
using Newtonsoft.Json.Serialization;
2021-05-14 14:07:19 +03:00
using ProxySuper.Core.Models;
2021-05-16 04:12:47 +03:00
using ProxySuper.Core.Models.Hosts;
using ProxySuper.Core.Models.Projects;
2021-05-25 13:28:37 +03:00
using ProxySuper.Core.Services;
2021-05-14 14:07:19 +03:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
2021-05-23 09:24:13 +03:00
using System.Windows;
2021-05-14 14:07:19 +03:00
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
}
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-23 09:24:13 +03:00
var json = JsonConvert.SerializeObject(Records, Formatting.Indented, new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
2021-05-16 11:29:37 +03:00
File.WriteAllText("Data/Record.json", json);
}
2021-08-21 06:56:20 +03:00
public void SortDone(string id)
{
var item = Records.Where(x => x.Id == id).FirstOrDefault();
if (item == null) return;
var index = Records.IndexOf(item);
if (index >= Records.Count - 1) return;
Records.Remove(item);
Records.Insert(index + 1, item);
RaisePropertyChanged("Records");
SaveToJson();
}
public void SortUp(string id)
{
var item = Records.Where(x => x.Id == id).FirstOrDefault();
if (item == null) return;
var index = Records.IndexOf(item);
if (index <= 0) return;
Records.Remove(item);
Records.Insert(index - 1, item);
RaisePropertyChanged("Records");
SaveToJson();
}
2021-05-14 14:07:19 +03:00
public MvxObservableCollection<Record> Records { get; set; }
2021-05-16 04:12:47 +03:00
2021-08-21 06:56:20 +03:00
public IMvxCommand SortUpCommand => new MvxCommand<string>(SortUp);
public IMvxCommand SortDoneCommand => new MvxCommand<string>(SortDone);
2021-08-20 06:52:45 +03:00
public IMvxCommand AddV2rayCommand => new MvxAsyncCommand(AddV2rayRecord);
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-25 13:28:37 +03:00
public IMvxCommand AddNaiveProxyCommand => new MvxAsyncCommand(AddNaiveProxyRecord);
public IMvxCommand AddMTProtoGoCommand => new MvxAsyncCommand(AddMTProtoGoRecord);
2021-08-23 07:00:39 +03:00
2021-07-02 13:35:25 +03:00
public IMvxCommand AddBrookCommand => new MvxAsyncCommand(AddBrookRecord);
2021-05-23 09:24:13 +03:00
public IMvxCommand RemoveCommand => new MvxAsyncCommand<string>(DeleteRecord);
public IMvxCommand EditCommand => new MvxAsyncCommand<string>(EditRecord);
public IMvxCommand ViewConfigCommand => new MvxAsyncCommand<string>(ViewConfig);
public IMvxCommand InstallCommand => new MvxAsyncCommand<string>(GoToInstall);
2021-08-20 06:52:45 +03:00
public async Task AddV2rayRecord()
{
Record record = new Record();
record.Id = Utils.GetTickID();
record.Host = new Host();
record.V2raySettings = new V2raySettings();
var result = await _navigationService.Navigate<V2rayEditorViewModel, Record, Record>(record);
if (result == null) return;
Records.Add(result);
SaveToJson();
}
2021-05-16 04:12:47 +03:00
public async Task AddXrayRecord()
{
Record record = new Record();
2021-05-25 13:28:37 +03:00
record.Id = Utils.GetTickID();
2021-05-16 04:12:47 +03:00
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();
2021-05-25 13:28:37 +03:00
record.Id = Utils.GetTickID();
2021-05-16 11:29:37 +03:00
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-23 09:24:13 +03:00
SaveToJson();
}
public async Task AddMTProtoGoRecord()
2021-08-23 07:00:39 +03:00
{
Record record = new Record();
record.Id = Utils.GetTickID();
record.Host = new Host();
record.MTProtoGoSettings = new MTProtoGoSettings();
2021-08-23 07:00:39 +03:00
var result = await _navigationService.Navigate<MTProtoGoEditorViewModel, Record, Record>(record);
2021-08-23 07:00:39 +03:00
if (result == null) return;
Records.Add(result);
SaveToJson();
}
2021-05-25 13:28:37 +03:00
public async Task AddNaiveProxyRecord()
{
Record record = new Record();
record.Id = Utils.GetTickID();
record.Host = new Host();
record.NaiveProxySettings = new NaiveProxySettings();
var result = await _navigationService.Navigate<NaiveProxyEditorViewModel, Record, Record>(record);
if (result == null) return;
Records.Add(result);
SaveToJson();
}
2021-07-02 13:35:25 +03:00
public async Task AddBrookRecord()
{
Record record = new Record();
record.Id = Utils.GetTickID();
record.Host = new Host();
record.BrookSettings = new BrookSettings();
var result = await _navigationService.Navigate<BrookEditorViewModel, Record, Record>(record);
if (result == null) return;
Records.Add(result);
SaveToJson();
}
2021-05-25 13:28:37 +03:00
2021-05-23 09:24:13 +03:00
public async Task EditRecord(string id)
{
var record = Records.FirstOrDefault(x => x.Id == id);
if (record == null) return;
Record result = null;
2021-08-20 06:52:45 +03:00
if (record.Type == ProjectType.V2ray)
{
result = await _navigationService.Navigate<V2rayEditorViewModel, Record, Record>(record);
if (result == null) return;
record.Host = result.Host;
record.V2raySettings = result.V2raySettings;
}
2021-05-23 09:24:13 +03:00
if (record.Type == ProjectType.Xray)
{
result = await _navigationService.Navigate<XrayEditorViewModel, Record, Record>(record);
2021-05-24 13:57:17 +03:00
if (result == null) return;
2021-05-23 12:40:06 +03:00
record.Host = result.Host;
record.XraySettings = result.XraySettings;
2021-05-23 09:24:13 +03:00
}
2021-05-23 12:40:06 +03:00
if (record.Type == ProjectType.TrojanGo)
2021-05-23 09:24:13 +03:00
{
result = await _navigationService.Navigate<TrojanGoEditorViewModel, Record, Record>(record);
2021-05-24 13:57:17 +03:00
if (result == null) return;
2021-05-23 12:40:06 +03:00
record.Host = result.Host;
record.TrojanGoSettings = result.TrojanGoSettings;
2021-05-23 09:24:13 +03:00
}
2021-05-25 13:28:37 +03:00
if (record.Type == ProjectType.NaiveProxy)
{
result = await _navigationService.Navigate<NaiveProxyEditorViewModel, Record, Record>(record);
if (result == null) return;
record.Host = result.Host;
record.NaiveProxySettings = result.NaiveProxySettings;
}
2021-07-06 13:30:14 +03:00
if (record.Type == ProjectType.Brook)
{
result = await _navigationService.Navigate<BrookEditorViewModel, Record, Record>(record);
if (result == null) return;
record.Host = result.Host;
record.BrookSettings = result.BrookSettings;
}
if (record.Type == ProjectType.MTProtoGo)
2021-08-23 10:59:34 +03:00
{
result = await _navigationService.Navigate<MTProtoGoEditorViewModel, Record, Record>(record);
2021-08-23 10:59:34 +03:00
if (result == null) return;
record.Host = result.Host;
record.MTProtoGoSettings = result.MTProtoGoSettings;
2021-08-23 10:59:34 +03:00
}
2021-05-23 09:24:13 +03:00
SaveToJson();
}
public async Task DeleteRecord(string id)
{
var result = MessageBox.Show($"您确认删除主机吗?", "提示", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
var record = Records.FirstOrDefault(x => x.Id == id);
if (record != null)
{
Records.Remove(record);
SaveToJson();
}
}
await Task.CompletedTask;
}
public async Task ViewConfig(string id)
{
var record = Records.FirstOrDefault(x => x.Id == id);
if (record == null) return;
2021-08-20 06:52:45 +03:00
if (record.Type == ProjectType.V2ray)
{
await _navigationService.Navigate<V2rayConfigViewModel, V2raySettings>(record.V2raySettings);
}
2021-05-23 09:24:13 +03:00
if (record.Type == ProjectType.Xray)
{
await _navigationService.Navigate<XrayConfigViewModel, XraySettings>(record.XraySettings);
}
if (record.Type == ProjectType.TrojanGo)
{
await _navigationService.Navigate<TrojanGoConfigViewModel, TrojanGoSettings>(record.TrojanGoSettings);
}
2021-05-25 13:28:37 +03:00
if (record.Type == ProjectType.NaiveProxy)
{
await _navigationService.Navigate<NaiveProxyConfigViewModel, NaiveProxySettings>(record.NaiveProxySettings);
}
2021-07-07 11:56:53 +03:00
if (record.Type == ProjectType.Brook)
{
await _navigationService.Navigate<BrookConfigViewModel, BrookSettings>(record.BrookSettings);
}
if (record.Type == ProjectType.MTProtoGo)
2021-08-23 10:59:34 +03:00
{
await _navigationService.Navigate<MTProtoGoConfigViewModel, MTProtoGoSettings>(record.MTProtoGoSettings);
2021-08-23 10:59:34 +03:00
}
2021-05-23 09:24:13 +03:00
}
public async Task GoToInstall(string id)
{
var record = Records.FirstOrDefault(x => x.Id == id);
if (record == null) return;
2021-08-23 10:59:34 +03:00
record.OnSave = SaveToJson;
2021-05-23 09:24:13 +03:00
2021-08-20 06:52:45 +03:00
if (record.Type == ProjectType.V2ray)
{
await _navigationService.Navigate<V2rayInstallViewModel, Record>(record);
}
2021-05-23 09:24:13 +03:00
if (record.Type == ProjectType.Xray)
{
2021-08-16 13:25:43 +03:00
await _navigationService.Navigate<XrayInstallViewModel, Record>(record);
2021-05-23 09:24:13 +03:00
}
if (record.Type == ProjectType.TrojanGo)
{
2021-08-18 12:34:32 +03:00
await _navigationService.Navigate<TrojanGoInstallViewModel, Record>(record);
2021-05-23 09:24:13 +03:00
}
2021-05-25 13:28:37 +03:00
if (record.Type == ProjectType.NaiveProxy)
{
2021-08-18 12:34:32 +03:00
await _navigationService.Navigate<NaiveProxyInstallViewModel, Record>(record);
2021-05-25 13:28:37 +03:00
}
2021-07-06 13:30:14 +03:00
if (record.Type == ProjectType.Brook)
{
2021-08-18 12:34:32 +03:00
await _navigationService.Navigate<BrookInstallViewModel, Record>(record);
2021-07-06 13:30:14 +03:00
}
if (record.Type == ProjectType.MTProtoGo)
2021-08-23 10:59:34 +03:00
{
await _navigationService.Navigate<MTProtoGoInstallViewModel, Record>(record);
2021-08-23 10:59:34 +03:00
}
SaveToJson();
2021-05-16 11:29:37 +03:00
}
2021-08-23 10:59:34 +03:00
2021-05-14 14:07:19 +03:00
}
}