-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
At the bottom a bar was added, which shows the connection status to the server and the application version.
- Loading branch information
Showing
8 changed files
with
237 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package controller | ||
|
||
import ( | ||
"slices" | ||
"sync" | ||
"time" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/seternate/go-lanty/pkg/util" | ||
) | ||
|
||
type ConnectionStatus int | ||
|
||
const ( | ||
Connected ConnectionStatus = iota | ||
Disconnected | ||
) | ||
|
||
type ConnectionController struct { | ||
parent *Controller | ||
subscriber []chan struct{} | ||
refreshinterval time.Duration | ||
mutex sync.RWMutex | ||
Status ConnectionStatus | ||
} | ||
|
||
func NewConnectionController(parent *Controller, refreshinterval time.Duration) (controller *ConnectionController) { | ||
controller = &ConnectionController{ | ||
parent: parent, | ||
subscriber: make([]chan struct{}, 0, 50), | ||
refreshinterval: refreshinterval, | ||
Status: Disconnected, | ||
} | ||
parent.WaitGroup().Add(1) | ||
go controller.run() | ||
return | ||
} | ||
|
||
func (controller *ConnectionController) run() { | ||
defer controller.parent.WaitGroup().Done() | ||
ticker := time.NewTicker(controller.refreshinterval) | ||
controller.updateStatus() | ||
for { | ||
select { | ||
case <-controller.parent.Context().Done(): | ||
log.Trace().Err(controller.parent.Context().Err()).Msg("exiting connectioncontroller run()") | ||
return | ||
case <-ticker.C: | ||
controller.updateStatus() | ||
} | ||
} | ||
} | ||
|
||
func (controller *ConnectionController) updateStatus() { | ||
controller.mutex.Lock() | ||
oldstatus := controller.Status | ||
if controller.parent.client.Health.Health() != nil { | ||
controller.Status = Disconnected | ||
} else { | ||
controller.Status = Connected | ||
} | ||
newstatus := controller.Status | ||
controller.mutex.Unlock() | ||
if newstatus != oldstatus { | ||
controller.notifySubcriber() | ||
} | ||
} | ||
|
||
func (controller *ConnectionController) Subscribe(subscriber chan struct{}) { | ||
defer controller.mutex.Unlock() | ||
controller.mutex.Lock() | ||
controller.subscriber = append(controller.subscriber, subscriber) | ||
} | ||
|
||
func (controller *ConnectionController) Unsubscribe(subscriber chan struct{}) { | ||
defer controller.mutex.Unlock() | ||
controller.mutex.Lock() | ||
index := slices.Index(controller.subscriber, subscriber) | ||
_ = slices.Delete(controller.subscriber, index, index+1) | ||
} | ||
|
||
func (controller *ConnectionController) notifySubcriber() { | ||
defer controller.mutex.RUnlock() | ||
controller.mutex.RLock() | ||
for _, subscriber := range controller.subscriber { | ||
util.ChannelWriteNonBlocking(subscriber, struct{}{}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package widget | ||
|
||
import ( | ||
"fmt" | ||
|
||
"fyne.io/fyne/v2" | ||
"fyne.io/fyne/v2/canvas" | ||
fynetheme "fyne.io/fyne/v2/theme" | ||
"fyne.io/fyne/v2/widget" | ||
"github.com/rs/zerolog/log" | ||
"github.com/seternate/go-lanty-client/pkg/controller" | ||
"github.com/seternate/go-lanty-client/pkg/setting" | ||
"github.com/seternate/go-lanty-client/pkg/theme" | ||
) | ||
|
||
type Connectionbar struct { | ||
widget.BaseWidget | ||
|
||
controller *controller.Controller | ||
statusupdated chan struct{} | ||
statustext string | ||
} | ||
|
||
func NewConnectionbar(controller *controller.Controller) *Connectionbar { | ||
connectionbar := &Connectionbar{ | ||
controller: controller, | ||
statusupdated: make(chan struct{}, 50), | ||
statustext: "UNKNOWN", | ||
} | ||
connectionbar.ExtendBaseWidget(connectionbar) | ||
|
||
controller.Connection.Subscribe(connectionbar.statusupdated) | ||
controller.WaitGroup().Add(1) | ||
go connectionbar.statusUpdater() | ||
|
||
return connectionbar | ||
} | ||
|
||
func (widget *Connectionbar) statusUpdater() { | ||
defer widget.controller.WaitGroup().Done() | ||
widget.updateStatus() | ||
for { | ||
select { | ||
case <-widget.controller.Context().Done(): | ||
log.Trace().Msg("exiting connectionbar statusUpdater()") | ||
return | ||
case <-widget.statusupdated: | ||
widget.updateStatus() | ||
widget.Refresh() | ||
} | ||
} | ||
} | ||
|
||
func (widget *Connectionbar) updateStatus() { | ||
status := widget.controller.Connection.Status | ||
if status == controller.Connected { | ||
widget.statustext = fmt.Sprintf("Connected to server: %s", widget.controller.Settings.Settings().ServerURL) | ||
} else if status == controller.Disconnected { | ||
widget.statustext = fmt.Sprintf("Error connecting to server: %s", widget.controller.Settings.Settings().ServerURL) | ||
} | ||
} | ||
|
||
func (widget *Connectionbar) CreateRenderer() fyne.WidgetRenderer { | ||
return newConnectionbarRenderer(widget) | ||
} | ||
|
||
type connectionbarRenderer struct { | ||
widget *Connectionbar | ||
line *canvas.Line | ||
status *canvas.Text | ||
version *canvas.Text | ||
} | ||
|
||
func newConnectionbarRenderer(widget *Connectionbar) *connectionbarRenderer { | ||
renderer := &connectionbarRenderer{ | ||
widget: widget, | ||
line: canvas.NewLine(fynetheme.InputBorderColor()), | ||
status: canvas.NewText(widget.statustext, theme.ForegroundColor()), | ||
version: canvas.NewText(setting.VERSION, theme.ForegroundColor()), | ||
} | ||
renderer.line.StrokeWidth = 2.0 | ||
return renderer | ||
} | ||
|
||
func (renderer *connectionbarRenderer) Objects() []fyne.CanvasObject { | ||
objects := []fyne.CanvasObject{ | ||
renderer.line, | ||
renderer.status, | ||
renderer.version, | ||
} | ||
return objects | ||
} | ||
|
||
func (renderer *connectionbarRenderer) Layout(size fyne.Size) { | ||
renderer.line.Resize(fyne.NewSize(size.Width, 0)) | ||
|
||
versiontextsize := fyne.MeasureText(renderer.version.Text, renderer.version.TextSize, renderer.version.TextStyle) | ||
statustextsize := fyne.MeasureText(renderer.status.Text, renderer.status.TextSize, renderer.status.TextStyle) | ||
|
||
renderer.version.Move(fyne.NewPos(theme.InnerPadding(), (size.Height-versiontextsize.Height)/2)) | ||
renderer.status.Move(fyne.NewPos(size.Width-statustextsize.Width-theme.InnerPadding(), (size.Height-statustextsize.Height)/2)) | ||
} | ||
|
||
func (renderer *connectionbarRenderer) MinSize() fyne.Size { | ||
versiontextsize := fyne.MeasureText(renderer.version.Text, renderer.version.TextSize, renderer.version.TextStyle) | ||
statustextsize := fyne.MeasureText(renderer.status.Text, renderer.status.TextSize, renderer.status.TextStyle) | ||
|
||
minHeight := fyne.Max(versiontextsize.Height, statustextsize.Height) + theme.InnerPadding() | ||
minWidth := versiontextsize.Width + statustextsize.Width + 3*theme.InnerPadding() | ||
|
||
return fyne.NewSize(minWidth, minHeight) | ||
} | ||
|
||
func (renderer *connectionbarRenderer) Refresh() { | ||
renderer.line.Refresh() | ||
renderer.status.Text = renderer.widget.statustext | ||
renderer.status.Refresh() | ||
renderer.version.Refresh() | ||
} | ||
|
||
func (renderer *connectionbarRenderer) Destroy() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters