-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
185 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Windows.Storage; | ||
|
||
namespace SimpleList.Services | ||
{ | ||
public class Utils | ||
{ | ||
public static async Task<ulong> GetFolderSize(StorageFolder folder) | ||
{ | ||
ulong res = 0; | ||
foreach (StorageFile file in await folder.GetFilesAsync()) | ||
{ | ||
Windows.Storage.FileProperties.BasicProperties properties = await file.GetBasicPropertiesAsync(); | ||
res += properties.Size; | ||
} | ||
|
||
foreach (StorageFolder subFolder in await folder.GetFoldersAsync()) | ||
{ | ||
res += await GetFolderSize(subFolder); | ||
} | ||
return res; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,71 @@ | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
using CommunityToolkit.Mvvm.DependencyInjection; | ||
using Microsoft.UI.Dispatching; | ||
using SimpleList.Services; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Windows.Storage; | ||
|
||
namespace SimpleList.ViewModels | ||
{ | ||
public class UploadTaskViewModel : ObservableObject | ||
{ | ||
private string _name; | ||
private string _path; | ||
private string _status; | ||
private double _progress; | ||
private bool _isCompleted; | ||
public UploadTaskViewModel(string itemId, IStorageItem item) | ||
{ | ||
_itemId = itemId; | ||
_item = item; | ||
Drive = Ioc.Default.GetService<OneDrive>(); | ||
} | ||
|
||
public void StartUpload() | ||
public async Task StartUpload() | ||
{ | ||
_isUploading = true; | ||
IProgress<long> progress = new Progress<long>(value => | ||
{ | ||
_dispatcher.TryEnqueue(async () => | ||
{ | ||
Progress = _item is StorageFile ? (int)((ulong)value * 100 / (await _item.GetBasicPropertiesAsync()).Size) : (int)value; | ||
}); | ||
}); | ||
if (_item is StorageFile file) | ||
{ | ||
|
||
await Drive.UploadFileAsync(file, _itemId, progress); | ||
} else if (_item is StorageFolder folder) | ||
{ | ||
await Drive.UploadFolderAsync(folder, _itemId, progress); | ||
} | ||
Completed = true; | ||
_isUploading = false; | ||
await Task.CompletedTask; | ||
} | ||
|
||
public static void PauseUpload() | ||
{ | ||
// TODO: Implement upload logic | ||
// Seems that Microsoft Graph API doesn't support pause upload. | ||
} | ||
|
||
public void CancelTask() | ||
{ | ||
// https://github.com/microsoftgraph/msgraph-sdk-dotnet/issues/1678 | ||
// Unfortunately, Microsoft Graph v4 doesn't support cancel upload while CommunityToolkit.Graph is still using this version. | ||
// So before it updates to v5, we can only remove the task from the list. | ||
_manager.RemoveSelectedUploadTasks(this); | ||
} | ||
|
||
private readonly string _itemId; | ||
private readonly IStorageItem _item; | ||
private readonly TaskManagerViewModel _manager = Ioc.Default.GetService<TaskManagerViewModel>(); | ||
private readonly DispatcherQueue _dispatcher = DispatcherQueue.GetForCurrentThread(); | ||
private int _progress; | ||
private bool _completed = false; | ||
private bool _isUploading = true; | ||
|
||
public OneDrive Drive; | ||
public string Name => _item.Name; | ||
public bool Completed { get => _completed; private set => SetProperty(ref _completed, value); } | ||
public int Progress { get => _progress; private set => SetProperty(ref _progress, value); } | ||
public bool IsUploading { get => _isUploading; private set => SetProperty(ref _isUploading, value); } | ||
} | ||
} |