Initial commit
This commit is contained in:
102
Valheim Dashboard/ViewModels/FTPViewModel.cs
Normal file
102
Valheim Dashboard/ViewModels/FTPViewModel.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Reactive;
|
||||
using System.Threading.Tasks;
|
||||
using ReactiveUI;
|
||||
|
||||
namespace Valheim_Dashboard.ViewModels
|
||||
{
|
||||
public class FtpViewModel : ViewModelBase
|
||||
{
|
||||
public static FtpClient _ftpClient;
|
||||
|
||||
// Speichert den aktuellen FTP-Pfad
|
||||
private string _currentPath = "/";
|
||||
|
||||
public string CurrentPath
|
||||
{
|
||||
get => _currentPath;
|
||||
set => this.RaiseAndSetIfChanged(ref _currentPath, value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ObservableCollection, die jetzt FtpItem-Objekte enthält
|
||||
public ObservableCollection<FtpItem> Files { get; } = new();
|
||||
|
||||
// Liste für FTP-Dateien und Ordner
|
||||
// public ObservableCollection<string> Files { get; } = new();
|
||||
|
||||
// Befehl zum Laden des Verzeichnisses
|
||||
public ReactiveCommand<Unit, Unit> LoadFtpCommand { get; }
|
||||
|
||||
// Command, der ausgeführt wird, wenn ein Ordner geklickt wird
|
||||
public ReactiveCommand<FtpItem, Unit> FolderClickedCommand { get; }
|
||||
|
||||
|
||||
public FtpViewModel()
|
||||
{
|
||||
|
||||
|
||||
// Befehl für das Neuladen des Verzeichnisses
|
||||
LoadFtpCommand = ReactiveCommand.CreateFromTask(LoadFtpDirectory);
|
||||
|
||||
// Command, der beim Klick auf einen Ordner ausgeführt wird
|
||||
// FolderClickedCommand = ReactiveCommand.Create<FtpItem>(OnFolderClicked);
|
||||
FolderClickedCommand = ReactiveCommand.Create<FtpItem>(item =>
|
||||
{
|
||||
// Logik für den Klick auf das Folder-Item
|
||||
Console.WriteLine($"Folder {item.Name} clicked!");
|
||||
});
|
||||
|
||||
// Beim Erstellen direkt FTP-Verzeichnis laden
|
||||
LoadFtpCommand.Execute().Subscribe();
|
||||
Files.Add(new FtpItem { Name = "TestFile.txt", IsDirectory = false });
|
||||
|
||||
}
|
||||
|
||||
private async Task LoadFtpDirectory()
|
||||
{
|
||||
try
|
||||
{
|
||||
// Verwende die neue Methode, um detaillierte Informationen zu erhalten
|
||||
var items = await _ftpClient.ListDirectoryDetailsAsync(CurrentPath);
|
||||
|
||||
// Sortiere: Ordner zuerst, dann Dateien, jeweils alphabetisch
|
||||
var sortedItems = items
|
||||
.OrderBy(item => item.IsDirectory ? 0 : 1)
|
||||
.ThenBy(item => item.Name, StringComparer.OrdinalIgnoreCase)
|
||||
.ToList();
|
||||
|
||||
// Hier gehen wir davon aus, dass du z. B. eine ObservableCollection vom Typ string hast.
|
||||
// Du kannst aber auch deine ObservableCollection umstellen, sodass du FtpItem-Objekte speicherst.
|
||||
Files.Clear();
|
||||
foreach (var item in sortedItems)
|
||||
{
|
||||
Console.WriteLine($"Item geladen: {item.Name}");
|
||||
// Je nach Bedarf kannst du hier den Namen oder auch weitere Infos anzeigen.
|
||||
Files.Add(item);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Fehler beim Laden des FTP-Verzeichnisses: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnFolderClicked(FtpItem item)
|
||||
{
|
||||
Console.WriteLine("OrdnerKlick");
|
||||
if (item.IsDirectory)
|
||||
{
|
||||
// Beispiel: Den aktuellen Pfad serweitern und das neue Verzeichnis laden
|
||||
// Je nach Bedarf kannst du hier auch Navigation oder andere Logik einbauen
|
||||
CurrentPath = System.IO.Path.Combine(CurrentPath, item.Name);
|
||||
// Neues Verzeichnis laden
|
||||
LoadFtpCommand.Execute().Subscribe();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user