diff --git a/FileReadAndWrite.csproj b/FileReadAndWrite.csproj
index bc7327d..c5741c4 100644
--- a/FileReadAndWrite.csproj
+++ b/FileReadAndWrite.csproj
@@ -57,6 +57,7 @@
+
diff --git a/Service/FileService.cs b/Service/FileService.cs
index 4b170da..970bcd8 100644
--- a/Service/FileService.cs
+++ b/Service/FileService.cs
@@ -7,11 +7,18 @@ public class FileService
public FileService()
{
// Plik, który będzie przechowywał zawartość naszej aplikacji
- _filePath = Path.Combine(FileSystem.AppDataDirectory, "data.txt");
+ _filePath = Path.Combine(FileSystem.AppDataDirectory, "tekst.txt");
}
- public async Task SaveToFile(){
+ public async Task SaveToFile(string content){
+ await File.WriteAllTextAsync(_filePath, content);
+ }
+ public async Task LoadFromFile(){
+ if (!File.Exists(_filePath))
+ return "Nie znaleziono pliku!";
+
+ return await File.ReadAllTextAsync(_filePath);
}
public async Task LoadAsset(string asset){
diff --git a/View/AppDataFilePage.xaml b/View/AppDataFilePage.xaml
index d21f065..1fa9f4a 100644
--- a/View/AppDataFilePage.xaml
+++ b/View/AppDataFilePage.xaml
@@ -8,7 +8,10 @@
-
+
+
+
+
diff --git a/View/AppDataFilePage.xaml.cs b/View/AppDataFilePage.xaml.cs
index e560815..3487995 100644
--- a/View/AppDataFilePage.xaml.cs
+++ b/View/AppDataFilePage.xaml.cs
@@ -1,10 +1,14 @@
+using FileReadAndWrite.Service;
+using FileReadAndWrite.ViewModel;
+
namespace FileReadAndWrite.View
{
public partial class AppDataFilePage : ContentPage
{
- public AppDataFilePage()
+ public AppDataFilePage(FileService fileService)
{
InitializeComponent();
+ BindingContext = new AppDataFilePageVM(fileService);
}
}
}
\ No newline at end of file
diff --git a/View/RawFilePage.xaml.cs b/View/RawFilePage.xaml.cs
index 8d72ec5..a43e4a8 100644
--- a/View/RawFilePage.xaml.cs
+++ b/View/RawFilePage.xaml.cs
@@ -1,31 +1,12 @@
-using System.ComponentModel;
-using System.Windows.Input;
using FileReadAndWrite.Service;
+using FileReadAndWrite.ViewModel;
namespace FileReadAndWrite.View;
public partial class RawFilePage : ContentPage {
- private readonly FileService _fileService;
- private string _rawFileContent = "";
- public string RawFileContent {
- get => _rawFileContent;
- set {
- _rawFileContent = value;
- OnPropertyChanged(nameof(RawFileContent));
- }
- }
-
- public ICommand LoadRawFileCmd { get; }
-
public RawFilePage(FileService fileService) {
InitializeComponent();
- _fileService = fileService;
- BindingContext = this;
- LoadRawFileCmd = new Command(async () => await LoadRawFile());
- }
-
- public async Task LoadRawFile(){
- RawFileContent = await _fileService.LoadAsset("data.txt");
+ BindingContext = new RawFilePageVM(fileService);
}
}
\ No newline at end of file
diff --git a/ViewModel/AppDataFilePageVM.cs b/ViewModel/AppDataFilePageVM.cs
new file mode 100644
index 0000000..20f9502
--- /dev/null
+++ b/ViewModel/AppDataFilePageVM.cs
@@ -0,0 +1,43 @@
+using System.ComponentModel;
+using System.Windows.Input;
+using FileReadAndWrite.Model;
+using FileReadAndWrite.Service;
+using FileReadAndWrite.View;
+
+namespace FileReadAndWrite.ViewModel;
+
+public class AppDataFilePageVM : INotifyPropertyChanged {
+ private readonly FileService _fileService;
+
+ private string _appDataFileContent = "";
+ public string AppDataFileContent {
+ get => _appDataFileContent;
+ set {
+ _appDataFileContent = value;
+ OnPropertyChanged(nameof(AppDataFileContent));
+ }
+ }
+
+ public ICommand LoadAppDataFileCmd { get; }
+ public ICommand SaveAppDataFileCmd { get; }
+
+ public AppDataFilePageVM(FileService fileService) {
+ _fileService = fileService;
+
+ LoadAppDataFileCmd = new Command(async () => await LoadAppDataFile());
+ SaveAppDataFileCmd = new Command(async () => await SaveAppDataFile());
+ }
+
+ public async Task LoadAppDataFile(){
+ AppDataFileContent = await _fileService.LoadFromFile();
+ }
+
+ public async Task SaveAppDataFile(){
+ await _fileService.SaveToFile(AppDataFileContent);
+ }
+
+ public event PropertyChangedEventHandler? PropertyChanged;
+ public void OnPropertyChanged(string propertyName) {
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
+ }
+}
\ No newline at end of file
diff --git a/ViewModel/MainViewModel.cs b/ViewModel/MainViewModel.cs
index 672cb92..7deb766 100644
--- a/ViewModel/MainViewModel.cs
+++ b/ViewModel/MainViewModel.cs
@@ -26,7 +26,7 @@ public class MainViewModel : INotifyPropertyChanged {
}
private async Task EnterAppDataFile(){
- await _navigation.PushAsync(new AppDataFilePage());
+ await _navigation.PushAsync(new AppDataFilePage(_fileService));
}
public event PropertyChangedEventHandler? PropertyChanged;
diff --git a/ViewModel/RawFilePageVM.cs b/ViewModel/RawFilePageVM.cs
new file mode 100644
index 0000000..a81a70f
--- /dev/null
+++ b/ViewModel/RawFilePageVM.cs
@@ -0,0 +1,37 @@
+using System.ComponentModel;
+using System.Windows.Input;
+using FileReadAndWrite.Model;
+using FileReadAndWrite.Service;
+using FileReadAndWrite.View;
+
+namespace FileReadAndWrite.ViewModel;
+
+public class RawFilePageVM : INotifyPropertyChanged {
+ private readonly FileService _fileService;
+
+ private string _rawFileContent = "";
+ public string RawFileContent {
+ get => _rawFileContent;
+ set {
+ _rawFileContent = value;
+ OnPropertyChanged(nameof(RawFileContent));
+ }
+ }
+
+ public ICommand LoadRawFileCmd { get; }
+
+ public RawFilePageVM(FileService fileService) {
+ _fileService = fileService;
+
+ LoadRawFileCmd = new Command(async () => await LoadRawFile());
+ }
+
+ public async Task LoadRawFile(){
+ RawFileContent = await _fileService.LoadAsset("data.txt");
+ }
+
+ public event PropertyChangedEventHandler? PropertyChanged;
+ public void OnPropertyChanged(string propertyName) {
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
+ }
+}
\ No newline at end of file