Skip to content

Commit

Permalink
Add base classes and refactor tab management
Browse files Browse the repository at this point in the history
  • Loading branch information
tznind committed Apr 14, 2024
1 parent fa211d6 commit 31d565b
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 70 deletions.
33 changes: 33 additions & 0 deletions nfirestore-cli/Tabs/CollectionTab.cs
Original file line number Diff line number Diff line change
@@ -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<DocumentReference> 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;
}
}
}
44 changes: 44 additions & 0 deletions nfirestore-cli/Tabs/DocumentTab.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
39 changes: 39 additions & 0 deletions nfirestore-cli/Tabs/FirestoreTab.cs
Original file line number Diff line number Diff line change
@@ -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;
}

}
}
12 changes: 12 additions & 0 deletions nfirestore-cli/Tabs/IFirestoreTab.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
89 changes: 19 additions & 70 deletions nfirestore-cli/TabsPane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,10 +24,7 @@ public TabsPane() {
this.tabView.TabClicked += TabView_TabClicked;
}

/// <summary>
/// Open <see cref="DocumentReference"/> or <see cref="CollectionReference"/>
/// </summary>
Dictionary<object, Tab> openTabs = new Dictionary<object, Tab>();
List<IFirestoreTab> openTabs = new ();

private void TabView_TabClicked(object sender, TabMouseEventArgs e)
{
Expand All @@ -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;
Expand All @@ -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<DocumentReference> 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);
}
}
}

0 comments on commit 31d565b

Please sign in to comment.