Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

オンラインリソースの取得履歴を表示できるようにした #106

Merged
merged 11 commits into from
Nov 24, 2023
Merged
177 changes: 177 additions & 0 deletions TRViS/RootPages/SelectOnlineResourcePopup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
using CommunityToolkit.Maui.Views;

using TRViS.ViewModels;

namespace TRViS.RootPages;

public class SelectOnlineResourcePopup : Popup
{
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

readonly Button CloseButton = new()
{
Text = "Close",
HorizontalOptions = LayoutOptions.End,
Margin = new(4),
};

readonly Button LoadButton = new()
{
Text = "Load🌍",
HorizontalOptions = LayoutOptions.End,
Margin = new(4),
};

readonly Entry UrlInput = new()
{
Placeholder = "https://",
Margin = new(4),
ClearButtonVisibility = ClearButtonVisibility.Never,
IsSpellCheckEnabled = false,
IsTextPredictionEnabled = false,
Keyboard = Keyboard.Url,
MaxLength = AppViewModel.PATH_LENGTH_MAX,
ReturnType = ReturnType.Go,
};

readonly ListView UrlHistoryListView = new()
{
Margin = new(4),
};

readonly Label AdviceLabel = new()
{
Text = "URLを入力するか、履歴から選択してください。",
Margin = new(4),
HorizontalOptions = LayoutOptions.Start,
VerticalOptions = LayoutOptions.Center,
};

readonly ActivityIndicator LoadingIndicator = new()
{
IsRunning = false,
IsVisible = false,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
};

public SelectOnlineResourcePopup()
{
logger.Debug("New SelectOnlineResourcePopup()");
Size = new(480, 480);
RootStyles.BackgroundColor.Apply(this, Popup.ColorProperty);

RootStyles.BackgroundBlackWhite.Apply(UrlInput, Button.BackgroundColorProperty);
RootStyles.TableTextColor.Apply(UrlInput, Button.TextColorProperty);

RootStyles.BackgroundBlackWhite.ToBrushTheme().Apply(UrlHistoryListView, ListView.BackgroundProperty);

RootStyles.TableTextColor.Apply(AdviceLabel, Label.TextColorProperty);

UrlHistoryListView.ItemTemplate = new DataTemplate(() => {
TextCell cell = new();
RootStyles.TableTextColor.Apply(cell, TextCell.TextColorProperty);
cell.SetBinding(TextCell.TextProperty, ".");
return cell;
});

// 本当にiOS 15以前のみで有効なプロパティなのかは不明
bool isBeforeiOS15 = DeviceInfo.Platform == DevicePlatform.iOS && DeviceInfo.Version.Major < 15;
if (!isBeforeiOS15)
UrlInput.ClearButtonVisibility = ClearButtonVisibility.WhileEditing;

Grid grid = new()
{
RowDefinitions =
{
new(new(1, GridUnitType.Auto)),
new(new(1, GridUnitType.Star)),
new(new(1, GridUnitType.Auto)),
new(new(1, GridUnitType.Auto)),
},
ColumnDefinitions =
{
new(new(1, GridUnitType.Star)),
new(new(1, GridUnitType.Auto)),
},
Padding = new(8),
};

CloseButton.Clicked += (s, e) => CloseAsync();

Command DoLoadCommand = new(DoLoad);
UrlInput.ReturnCommand = DoLoadCommand;
LoadButton.Command = DoLoadCommand;

UrlHistoryListView.ItemTapped += (_, e) => {
logger.Debug("UrlHistoryListView.ItemTapped");
UrlInput.Text = e.Item as string;
logger.Trace("UrlInput.Text = {0}", UrlInput.Text);
};

UrlInput.TextChanged += (_, e) => {
logger.Debug("UrlInput.TextChanged -> set UrlHistoryListView.SelectedItem = {0}", e.NewTextValue);
UrlHistoryListView.SelectedItem = e.NewTextValue;
};

grid.Add(CloseButton, column: 1);
Grid.SetColumnSpan(UrlHistoryListView, 2);
Grid.SetRow(UrlHistoryListView, 1);
grid.Add(UrlHistoryListView);
Grid.SetColumnSpan(UrlInput, 2);
Grid.SetRow(UrlInput, 2);
grid.Add(UrlInput);
grid.Add(AdviceLabel, row: 3);
grid.Add(LoadButton, column: 1, row: 3);
grid.Add(LoadingIndicator, column: 1, row: 3);

Content = grid;

Opened += (_, _) =>
{
logger.Debug("SelectOnlineResourcePopup.Opened");
UrlHistoryListView.ItemsSource = InstanceManager.AppViewModel.ExternalResourceUrlHistory.Reverse();
};
Closed += (_, _) =>
{
logger.Debug("SelectOnlineResourcePopup.Closed");
};

logger.Debug("initialize completed");
}

private async void DoLoad()
{
try
{
LoadButton.IsEnabled = false;
CloseButton.IsEnabled = false;
LoadingIndicator.IsRunning = true;
LoadingIndicator.IsVisible = true;
if (string.IsNullOrEmpty(UrlInput.Text))
{
logger.Info("URL is null or empty");
await Utils.DisplayAlert("Cannot Load from Web", "URLを入力してください。", "OK");
return;
}

bool execResult = await InstanceManager.AppViewModel.LoadExternalFileAsync(UrlInput.Text, AppLinkType.Unknown, CancellationToken.None);
if (execResult)
{
await CloseAsync();
return;
}
}
catch (Exception ex)
{
logger.Error(ex, "DoLoad() failed");
}
finally
{
LoadButton.IsEnabled = true;
CloseButton.IsEnabled = true;
LoadingIndicator.IsRunning = false;
LoadingIndicator.IsVisible = false;
}
}
}
19 changes: 8 additions & 11 deletions TRViS/RootPages/SelectTrainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
using CommunityToolkit.Maui.Views;

using Microsoft.AppCenter.Crashes;

using TRViS.IO;
using TRViS.ViewModels;

Expand Down Expand Up @@ -48,20 +52,13 @@ async void LoadFromWebButton_Clicked(object sender, EventArgs e)

try
{
string? url = await DisplayPromptAsync("Load From Web", "ファイルへのリンクを入力してください", "OK", "Cancel", "https://");

if (string.IsNullOrEmpty(url))
{
logger.Info("URL is null or empty");
return;
}

await viewModel.LoadExternalFileAsync(url, AppLinkType.Unknown, CancellationToken.None);
await this.ShowPopupAsync(new SelectOnlineResourcePopup());
}
catch (Exception ex)
{
logger.Error(ex, "Load From Web Failed");
await Utils.DisplayAlert(this, "Cannot Load from Web", ex.ToString(), "OK");
Crashes.TrackError(ex);
logger.Error(ex, "ShowPopupAsync failed");
await Utils.DisplayAlert(this, "Open Popup Failed", ex.ToString(), "OK");
}

logger.Info("Load From Web Button Clicked Processing Complete");
Expand Down
1 change: 1 addition & 0 deletions TRViS/RootStyles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ public static class RootStyles
public static readonly AppThemeColorBindingExtension TableTextColor = new(new(0x11, 0x11, 0x11), new(0xEE, 0xEE, 0xEE));
public static readonly AppThemeColorBindingExtension TableDetailColor = new(new(0x55, 0x55, 0x55), new(0xAA, 0xAA, 0xAA));
public static readonly AppThemeColorBindingExtension BackgroundColor = new(new(0xDD, 0xDD, 0xDD), new(0x22, 0x22, 0x22));
public static readonly AppThemeColorBindingExtension BackgroundBlackWhite = new(new(1), new(0));
public static readonly AppThemeColorBindingExtension FrameBorderColor = new(new(0x22, 0x22, 0x22), new(0xDD, 0xDD, 0xDD));
}
12 changes: 12 additions & 0 deletions TRViS/Services/AppPreferenceService.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System.Text.Json;

namespace TRViS.Services;

public enum AppPreferenceKeys
{
IsAppCenterEnabled,
IsAppCenterAnalyticsEnabled,
IsAppCenterLogShareEnabled,
ExternalResourceUrlHistory,
}

public static class AppPreferenceService
Expand Down Expand Up @@ -50,6 +53,12 @@ public static string Get(in AppPreferenceKeys key, string defaultValue, out bool
return value;
}

public static T GetFromJson<T>(in AppPreferenceKeys key, T defaultValue, out bool hasKey)
{
string result = Get(in key, string.Empty, out hasKey);
return string.IsNullOrEmpty(result) ? defaultValue : JsonSerializer.Deserialize<T>(result) ?? defaultValue;
}

public static void Set(in AppPreferenceKeys key, bool value)
{
string keyStr = ToKeyString(key);
Expand All @@ -63,4 +72,7 @@ public static void Set(in AppPreferenceKeys key, string value)
logger.Trace("key: {0}, value:{1}", keyStr, value);
Preferences.Set(keyStr, value);
}

public static void SetToJson<T>(in AppPreferenceKeys key, T value)
=> Set(in key, JsonSerializer.Serialize(value));
}
Loading