tesses-cms/Tesses.CMS.Avalonia/Tesses.CMS.Avalonia.Android/MobilePlatform.cs

92 lines
2.8 KiB
C#
Raw Normal View History

2024-07-28 22:59:28 +00:00
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Platform.Storage;
using Newtonsoft.Json;
using Tesses.VirtualFilesystem;
using Tesses.VirtualFilesystem.Extensions;
using Tesses.VirtualFilesystem.Filesystems;
namespace Tesses.CMS.Avalonia.Android;
internal class MobilePlatform : IPlatform
{
string configpath;
MainActivity activity;
public const string MySAFKey = "MySafKey";
public MobilePlatform(MainActivity activity)
{
this.activity = activity;
configpath=activity.FilesDir?.AbsolutePath ?? "";
if(File.Exists(Path.Combine(configpath, "tesses_cms_app.json")))
{
var conf=JsonConvert.DeserializeObject<Configuration>(File.ReadAllText(Path.Combine(configpath, "tesses_cms_app.json")));
if(conf != null)
_conf = conf;
}
var res= SAFFileSystem.GetSAFFromSharedStorage(activity,MySAFKey);
if(res != null)
virtualFilesystem = res;
}
Configuration _conf = new Configuration();
public Configuration Configuration => _conf;
public bool PlatformUsesNormalPathsForDownload => false;
internal IVirtualFilesystem? virtualFilesystem;
public IVirtualFilesystem? DownloadFilesystem => virtualFilesystem;
public async Task BrowseForDownloadDirectoryAsync()
{
/*var sp=App.Window?.StorageProvider;
if(sp != null)
{
var res=await sp.OpenFolderPickerAsync(new global::Avalonia.Platform.Storage.FolderPickerOpenOptions{Title="Browse for a downloads folder"});
if(res.Count > 0)
{
var first=res.First();
string? path=first.TryGetLocalPath();
if(!string.IsNullOrWhiteSpace(path))
{
_conf.DownloadPath = path;
await WriteConfigurationAsync();
}
}
}*/
try{
if(virtualFilesystem != null)
SAFFileSystem.Revoke(activity,MySAFKey);
}catch(Exception ex)
{
_=ex;
}
virtualFilesystem=null;
SAFFileSystem.RequestDirectory(activity,true,true);
await Task.CompletedTask;
}
public async Task ReadConfigurationAsync()
{
if(File.Exists(Path.Combine(configpath, "tesses_cms_app.json")))
{
var conf=JsonConvert.DeserializeObject<Configuration>(await File.ReadAllTextAsync(Path.Combine(configpath, "tesses_cms_app.json")));
if(conf != null)
_conf = conf;
}
}
public async Task WriteConfigurationAsync()
{
await File.WriteAllTextAsync(Path.Combine(configpath, "tesses_cms_app.json"),JsonConvert.SerializeObject(_conf));
}
}