From 31d565bc777850a164e2a16a3b932aa7a1f4cf2c Mon Sep 17 00:00:00 2001 From: tznind Date: Sun, 14 Apr 2024 19:45:10 +0100 Subject: [PATCH] Add base classes and refactor tab management --- nfirestore-cli/Tabs/CollectionTab.cs | 33 +++++++++++ nfirestore-cli/Tabs/DocumentTab.cs | 44 ++++++++++++++ nfirestore-cli/Tabs/FirestoreTab.cs | 39 ++++++++++++ nfirestore-cli/Tabs/IFirestoreTab.cs | 12 ++++ nfirestore-cli/TabsPane.cs | 89 ++++++---------------------- 5 files changed, 147 insertions(+), 70 deletions(-) create mode 100644 nfirestore-cli/Tabs/CollectionTab.cs create mode 100644 nfirestore-cli/Tabs/DocumentTab.cs create mode 100644 nfirestore-cli/Tabs/FirestoreTab.cs create mode 100644 nfirestore-cli/Tabs/IFirestoreTab.cs diff --git a/nfirestore-cli/Tabs/CollectionTab.cs b/nfirestore-cli/Tabs/CollectionTab.cs new file mode 100644 index 0000000..b0da627 --- /dev/null +++ b/nfirestore-cli/Tabs/CollectionTab.cs @@ -0,0 +1,33 @@ +using Google.Cloud.Firestore; +using Terminal.Gui; + +namespace nfirestore_cli.Tabs +{ + internal class CollectionTab : FirestoreTab + { + public CollectionReference CollectionReference { get; } + + public CollectionTab(CollectionReference cr, IEnumerable children) + { + this.CollectionReference = cr; + var view = new TableView + { + Width = Dim.Fill(), + Height = Dim.Fill(), + Table = new TableFromCollection(cr, children) + }; + + SetTab(cr.Id, view); + } + + public override bool Is(DocumentReference dr) + { + return false; + } + + public override bool Is(CollectionReference cr) + { + return cr == CollectionReference; + } + } +} diff --git a/nfirestore-cli/Tabs/DocumentTab.cs b/nfirestore-cli/Tabs/DocumentTab.cs new file mode 100644 index 0000000..475349d --- /dev/null +++ b/nfirestore-cli/Tabs/DocumentTab.cs @@ -0,0 +1,44 @@ +using Google.Cloud.Firestore; +using Newtonsoft.Json; +using Terminal.Gui; + +namespace nfirestore_cli.Tabs +{ + internal class DocumentTab : FirestoreTab + { + public DocumentReference DocumentReference { get; } + + public DocumentTab(DocumentSnapshot snap) + { + this.DocumentReference = snap.Reference; + + var view = new TextView + { + Width = Dim.Fill(), + Height = Dim.Fill(), + WordWrap = false, + AllowsReturn = false, + Multiline = true, + }; + + OpenDocumentIn(view, snap); + + SetTab(snap.Id, view); + } + + internal static void OpenDocumentIn(TextView currentDocumentTextView, DocumentSnapshot snap) + { + currentDocumentTextView.Text = JsonConvert.SerializeObject(snap.ToDictionary(), Formatting.Indented); + } + + public override bool Is(DocumentReference dr) + { + return dr == DocumentReference; + } + + public override bool Is(CollectionReference cr) + { + return false; + } + } +} diff --git a/nfirestore-cli/Tabs/FirestoreTab.cs b/nfirestore-cli/Tabs/FirestoreTab.cs new file mode 100644 index 0000000..32c74ac --- /dev/null +++ b/nfirestore-cli/Tabs/FirestoreTab.cs @@ -0,0 +1,39 @@ +using Google.Cloud.Firestore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Terminal.Gui; + +namespace nfirestore_cli.Tabs +{ + abstract class FirestoreTab : IFirestoreTab + { + public Tab Tab { get; private set; } + + public abstract bool Is(DocumentReference dr); + + public abstract bool Is(CollectionReference cr); + + protected void SetTab(string name, View view) + { + Tab = new Tab() + { + Text = GetTabName(name), + View = view + }; + } + + private string GetTabName(string name) + { + if (name.Length > 8) + { + name = name.Substring(0, 6) + "…"; + } + + return "[X]" + name; + } + + } +} diff --git a/nfirestore-cli/Tabs/IFirestoreTab.cs b/nfirestore-cli/Tabs/IFirestoreTab.cs new file mode 100644 index 0000000..31933ff --- /dev/null +++ b/nfirestore-cli/Tabs/IFirestoreTab.cs @@ -0,0 +1,12 @@ +using Google.Cloud.Firestore; +using Terminal.Gui; + +namespace nfirestore_cli.Tabs +{ + internal interface IFirestoreTab + { + public Tab Tab { get; } + public bool Is(DocumentReference dr); + public bool Is(CollectionReference cr); + } +} diff --git a/nfirestore-cli/TabsPane.cs b/nfirestore-cli/TabsPane.cs index 0151c6c..49faf7e 100644 --- a/nfirestore-cli/TabsPane.cs +++ b/nfirestore-cli/TabsPane.cs @@ -10,6 +10,7 @@ namespace nfirestore_cli { using Google.Cloud.Firestore; using Newtonsoft.Json; + using nfirestore_cli.Tabs; using System; using System.Collections.Generic; using Terminal.Gui; @@ -23,10 +24,7 @@ public TabsPane() { this.tabView.TabClicked += TabView_TabClicked; } - /// - /// Open or - /// - Dictionary openTabs = new Dictionary(); + List openTabs = new (); private void TabView_TabClicked(object sender, TabMouseEventArgs e) { @@ -41,12 +39,12 @@ private void TabView_TabClicked(object sender, TabMouseEventArgs e) private bool CloseTab(Tab tab) { - var kvp = openTabs.FirstOrDefault(kvp => kvp.Value == tab); + var found = openTabs.FirstOrDefault(kvp => kvp.Tab == tab); // if we can close it - if (kvp.Value != default(Tab)) + if (found != null) { - openTabs.Remove(kvp.Key); + openTabs.Remove(found); tabView.RemoveTab(tab); tabView.SetNeedsDisplay(); return true; @@ -59,91 +57,42 @@ internal void OpenDocument(DocumentSnapshot snap, bool newTab) { if(newTab) { + var existing = openTabs.FirstOrDefault(kvp => kvp.Is(snap.Reference)); + // If document is already open in another tab - if (openTabs.ContainsKey(snap.Reference)) + if (existing != null) { // switch to it - tabView.SelectedTab = openTabs[snap.Reference]; + tabView.SelectedTab = existing.Tab; return; } - var name = GetTabName(snap); - var view = new TextView - { - Width = Dim.Fill(), - Height = Dim.Fill(), - WordWrap = false, - AllowsReturn = false, - Multiline = true, - }; - - var tab = new Tab() - { - Text = name, - View = view - }; + var dt = new DocumentTab(snap); + openTabs.Add(dt); + tabView.AddTab(dt.Tab, true); - tabView.AddTab(tab, true); - openTabs.Add(snap.Reference, tab); - OpenDocumentIn(view, snap); } else { - OpenDocumentIn(currentDocumentTextView, snap); + DocumentTab.OpenDocumentIn(currentDocumentTextView, snap); } } - private string GetTabName(DocumentSnapshot snap) - { - return GetTabName(snap.Id); - } - private string GetTabName(CollectionReference cr) - { - return GetTabName(cr.Id); - } - - private string GetTabName(string name) - { - if (name.Length > 8) - { - name = name.Substring(0, 6) + "…"; - } - - return "[X]" + name; - } - - private void OpenDocumentIn(TextView currentDocumentTextView, DocumentSnapshot snap) - { - currentDocumentTextView.Text = JsonConvert.SerializeObject(snap.ToDictionary(), Formatting.Indented); - } - internal void OpenCollection(CollectionReference cr, IEnumerable children) { + var existing = openTabs.FirstOrDefault(kvp => kvp.Is(cr)); // If collection is already open in another tab - if (openTabs.ContainsKey(cr)) + if (existing != null) { // switch to it - tabView.SelectedTab = openTabs[cr]; + tabView.SelectedTab = existing.Tab; return; } - var name = GetTabName(cr); - var view = new TableView - { - Width = Dim.Fill(), - Height = Dim.Fill(), - Table = new TableFromCollection(cr, children) - }; - - var tab = new Tab() - { - Text = name, - View = view - }; - - tabView.AddTab(tab, true); - openTabs.Add(cr, tab); + var ct = new CollectionTab(cr, children); + openTabs.Add(ct); + tabView.AddTab(ct.Tab, true); } } }