Begin Switch to using QSharp for control

This commit is contained in:
Joel Wetzell
2020-06-19 17:18:38 -05:00
parent 3b60764a02
commit 6f416fa2b5
7 changed files with 453 additions and 4 deletions
+3 -2
View File
@@ -2,7 +2,8 @@
using Xamarin.Forms;
using Xamarin.Essentials;
using qController.Communication;
using qController.Pages;
namespace qController
{
public partial class App : Application
@@ -54,7 +55,7 @@ namespace qController
var menuPage = new MenuPage();
rootPage = new RootPage();
NavigationPage = new NavigationPage(new QConnectionPage());
NavigationPage = new NavigationPage(new QBrowserPage());
rootPage.Master = menuPage;
rootPage.Detail = NavigationPage;
MainPage = rootPage;
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="qController.Pages.QBrowserPage">
<ContentPage.Content>
<StackLayout x:Name="sLayout">
<ListView x:Name="storageListView"
HasUnevenRows="true"
BackgroundColor="#4A4A4A"
SeparatorVisibility="None">
</ListView>
<ListView x:Name="serverListView" GroupDisplayBinding="{Binding name}" IsGroupingEnabled="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid BackgroundColor="Blue">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Text="{Binding name}"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
VerticalTextAlignment="Center"
HorizontalTextAlignment="End"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
@@ -0,0 +1,90 @@
using QSharp;
using System.Collections.Generic;
using Xamarin.Forms;
using Serilog;
using System.Collections.ObjectModel;
using qController.Cell;
using Acr.UserDialogs;
using qController.Dialogs;
using Xamarin.Essentials;
using System;
namespace qController.Pages
{
public partial class QBrowserPage : ContentPage
{
public ObservableCollection<ServerGroup> servers = new ObservableCollection<ServerGroup>();
public QBrowserPage()
{
InitializeComponent();
App.rootPage.MenuItemSelected += OnMenuItemSelected;
serverListView.ItemsSource = servers;
QBrowser browser = new QBrowser();
browser.ServerUpdatedWorkspaces += Browser_ServerUpdatedWorkspaces;
serverListView.ItemSelected += ServerListView_ItemSelected;
storageListView.ItemsSource = QStorage.qInstances;
storageListView.ItemTemplate = new DataTemplate(typeof(QInstanceCell));
storageListView.ItemTapped += (object sender, ItemTappedEventArgs e) =>
{
// don't do anything if we just de-selected the row
if (e.Item == null) return;
// do something with e.SelectedItem
((ListView)sender).SelectedItem = null; // de-select the row
};
}
async void ServerListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
if (e.SelectedItem == null)
return;
QWorkspace selectedWorkspace = e.SelectedItem as QWorkspace;
Log.Debug($"[demo] workspace: {selectedWorkspace.nameWithoutPathExtension} has been selected");
((ListView)sender).SelectedItem = null;
await Navigation.PushAsync(new WorkspacePage(selectedWorkspace));
}
private void Browser_ServerUpdatedWorkspaces(object source, QServerUpdatedArgs args)
{
ServerGroup serverGroup = new ServerGroup(args.server.name);
serverGroup.AddRange(args.server.workspaces);
Device.BeginInvokeOnMainThread(() =>
{
servers.Add(serverGroup);
});
}
void AddWorkspace()
{
UserDialogs.Instance.Prompt(new AddInstancePrompt());
}
private void OnMenuItemSelected(object source, MenuEventArgs args)
{
if (args.Command == "add")
{
AddWorkspace();
}
else if (args.Command == "feedback")
{
Launcher.OpenAsync(new Uri("mailto:feedback@jwetzell.com?subject=qController%20feedback"));
}
else if (args.Command == "support")
{
UserDialogs.Instance.Confirm(new DonatePrompt());
}
}
}
public class ServerGroup : List<QWorkspace>
{
public string name { get; set; }
public ServerGroup(string name)
{
this.name = name;
}
}
}
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="qController.WorkspacePage">
<Grid RowSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="200"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ScrollView x:Name="cueListScrollView"
Grid.Row="1">
<Grid x:Name="cueListsGrid"
RowSpacing="0"
ColumnSpacing="0"
BackgroundColor="#808080">
<Grid.RowDefinitions></Grid.RowDefinitions>
</Grid>
</ScrollView>
<Grid x:Name="selectedCueGrid"
Grid.Row="0"
RowSpacing="0"
BackgroundColor="#D8D8D8">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Frame Grid.RowSpan="3" Grid.ColumnSpan="5" BorderColor="Black" BackgroundColor="Transparent"/>
<ActivityIndicator x:Name="workspaceLoadingIndicator" Grid.Row="1" HeightRequest="20"/>
<Label Grid.Row ="0"
Grid.Column="0"
Text="{Binding number}"
d:Text="100"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
FontSize="Medium"/>
<Label Grid.Row ="1"
Grid.Column="0"
Grid.ColumnSpan="5"
Text="{Binding name}"
d:Text="Cue 1"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
FontSize="Large"/>
</Grid>
</Grid>
</ContentPage>
@@ -0,0 +1,139 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using QSharp;
using qController.ViewModels;
using Serilog;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace qController
{
public partial class WorkspacePage : ContentPage
{
private QWorkspace connectedWorkspace;
private Dictionary<string, Grid> cueGridDict = new Dictionary<string, Grid>();
public WorkspacePage(QWorkspace workspace)
{
InitializeComponent();
connectedWorkspace = workspace;
connectedWorkspace.WorkspaceUpdated += Workspace_WorkspaceUpdated;
connectedWorkspace.connectWithPasscode("1234");
connectedWorkspace.defaultSendUpdatesOSC = true;
connectedWorkspace.CueListChangedPlaybackPosition += ConnectedWorkspace_CueListChangedPlaybackPosition;
}
void ConnectedWorkspace_CueListChangedPlaybackPosition(object source, QCueListChangedPlaybackPositionArgs args)
{
Device.BeginInvokeOnMainThread(() =>
{
selectedCueGrid.BindingContext = new QCueViewModel(connectedWorkspace.cueWithID(args.cueID), false);
if (cueGridDict.ContainsKey(args.cueID))
{
var cueGrid = cueGridDict[args.cueID]; //element to scroll to
cueListScrollView.ScrollToAsync(cueGrid, ScrollToPosition.Center, true);
}
});
}
void Workspace_WorkspaceUpdated(object source, QWorkspaceUpdatedArgs args)
{
Log.Debug("[workspacepage] Workspace Updated");
foreach (var cue in connectedWorkspace.cueLists)
{
if (cue.cues.Count > 0)
{
List<Task> cueAddTasks = new List<Task>();
foreach (var aCue in cue.cues)
{
Grid cueGrid = cueToGrid(aCue);
cueGridDict.Add(aCue.uid, cueGrid);
MainThread.InvokeOnMainThreadAsync(() =>
{
cueListsGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
cueListsGrid.Children.Add(cueGrid, 0, aCue.sortIndex);
}).Wait();
}
connectedWorkspace.valueForKey(cue, QOSCKey.PlaybackPositionId); //fetch playback position for cueList once all cue loading is done.
break; //only load first cue list with cues.
}
}
}
protected override void OnDisappearing()
{
base.OnDisappearing();
//unsubscribe from events
connectedWorkspace.WorkspaceUpdated -= Workspace_WorkspaceUpdated;
connectedWorkspace.CueListChangedPlaybackPosition -= ConnectedWorkspace_CueListChangedPlaybackPosition;
//disconnect
connectedWorkspace.disconnect();
}
Grid cueToGrid(QCue cue)
{
Grid cueGrid = new Grid { RowSpacing = 0 };
QCueViewModel qCueViewModel = new QCueViewModel(cue, true);
cueGrid.RowDefinitions = new RowDefinitionCollection();
cueGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(40) });
var cueLabel = new Label
{
BindingContext = qCueViewModel,
HorizontalOptions = LayoutOptions.StartAndExpand,
VerticalTextAlignment = TextAlignment.Center,
};
cueLabel.SetBinding(Label.TextProperty, "name", BindingMode.OneWay);
var cueBackground = new Frame
{
BindingContext = qCueViewModel,
Opacity = 0.50,
HasShadow = false,
CornerRadius = 0,
BorderColor = Color.Black
};
cueBackground.SetBinding(BackgroundColorProperty, "color", BindingMode.OneWay);
var cueSelectedIndicator = new Frame
{
BindingContext = qCueViewModel,
BackgroundColor = Color.Blue,
HasShadow = false
};
cueSelectedIndicator.SetBinding(IsVisibleProperty, "IsSelected");
cueGrid.Children.Add(cueSelectedIndicator, 0, 0);
cueGrid.Children.Add(cueBackground, 0, 0);
cueGrid.Children.Add(cueLabel, 0, 0);
int rows = 1;
if (cue.cues.Count > 0)
{
foreach (var aCue in cue.cues)
{
cueGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
var aCueGrid = cueToGrid(aCue);
cueGridDict.Add(aCue.uid, aCueGrid);
aCueGrid.Margin = new Thickness(10, 0, 0, 0);
cueGrid.Children.Add(aCueGrid, 0, aCue.sortIndex + 1);
rows++;
}
var cueFrame = new Frame
{
BackgroundColor = Color.Transparent,
BorderColor = Color.Black
};
cueGrid.Children.Add(cueFrame);
Grid.SetRowSpan(cueFrame, rows);
}
return cueGrid;
}
}
}
@@ -0,0 +1,100 @@
using QSharp;
using Serilog;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Xamarin.Forms;
namespace qController.ViewModels
{
public class QCueViewModel : INotifyPropertyChanged
{
QCue cue;
bool isSelected = false;
public event PropertyChangedEventHandler PropertyChanged;
public QCueViewModel(QCue cue, bool checkPlayback)
{
this.cue = cue;
if (checkPlayback)
this.cue.workspace.CueListChangedPlaybackPosition += Workspace_CueListChangedPlaybackPosition;
this.cue.CuePropertiesUpdated += OnCuePropertiesUpdated;
}
private void OnCuePropertiesUpdated(object source, QCuePropertiesUpdatedArgs args)
{
foreach (var property in args.properties)
{
Log.Debug($"[cueviewmodel] property <{property}> has been updated.");
if (property.Equals(QOSCKey.Name))
{
OnPropertyChanged("name");
}
else if (property.Equals(QOSCKey.ColorName))
{
OnPropertyChanged("color");
}
}
}
void OnPropertyChanged([CallerMemberName] string name = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
private void Workspace_CueListChangedPlaybackPosition(object source, QCueListChangedPlaybackPositionArgs args)
{
IsSelected = args.cueID == cue.uid;
}
public string name
{
get
{
return cue.displayName;
}
set
{
cue.name = value;
}
}
public string number
{
get
{
return cue.number;
}
set
{
cue.number = value;
}
}
public Color color
{
get
{
return Color.FromHex(cue.color.Hex);
}
set
{
cue.color = new QColor("none");
}
}
public bool IsSelected
{
get
{
return isSelected;
}
set
{
isSelected = value;
OnPropertyChanged();
}
}
}
}
+12 -2
View File
@@ -40,7 +40,6 @@
<Compile Remove="Classes\Cues\QDefinitions.cs" />
<Compile Remove="Classes\Communication\QSender.cs" />
<Compile Remove="Classes\Communication\QSender.cs" />
<Compile Remove="ViewModels\MenuPageViewModel.cs" />
<Compile Remove="Pages\RootPageMenuItem.cs" />
<Compile Remove="Pages\RootPageMaster.xaml.cs" />
<Compile Remove="Pages\RootPageDetail.xaml.cs" />
@@ -50,12 +49,16 @@
<Compile Condition=" '$(EnableDefaultCompileItems)' == 'true' " Update="Pages\ControlPage.xaml.cs">
<DependentUpon>*.xaml</DependentUpon>
</Compile>
<Compile Update="Pages\QBrowserPage.xaml.cs">
<DependentUpon>QBrowserPage.xaml</DependentUpon>
</Compile>
<Compile Condition=" '$(EnableDefaultCompileItems)' == 'true' " Update="Pages\qControllerPage.xaml.cs">
<DependentUpon>*.xaml</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Acr.UserDialogs" Version="7.1.0.442" />
<PackageReference Include="QSharp" Version="1.0.0" />
<PackageReference Include="Zeroconf" Version="3.3.16" />
<PackageReference Include="Xamarin.Forms" Version="4.6.0.847" />
<PackageReference Include="Acr.Settings" Version="9.0.1" />
@@ -83,7 +86,6 @@
<EmbeddedResource Include="App.xaml" />
</ItemGroup>-->
<ItemGroup>
<Folder Include="Pages\" />
<Folder Include="Classes\QItems\" />
<Folder Include="Classes\Cell\" />
<Folder Include="Classes\Settings\" />
@@ -95,4 +97,12 @@
<EmbeddedResource Remove="Pages\RootPageDetail.xaml" />
<EmbeddedResource Remove="Pages\HomePage.xaml" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Pages\QBrowserPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Pages\WorkspacePage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
</Project>