Skip to content

Commit

Permalink
refactor master page and page stack
Browse files Browse the repository at this point in the history
-- Refactor app.NewMasterPage to accept and handle a start page.
-- Refactor *app.MasterPage.Display()
-- Refactor app.PageStack.Push to only prepare the next page.
-- Refactor pages that embed app.Masterpage.
-- Fix some TODOs.

Signed-off-by: Philemon Ukane <[email protected]>
  • Loading branch information
ukane-philemon committed Dec 4, 2023
1 parent 1acb6cc commit 0342764
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 79 deletions.
20 changes: 17 additions & 3 deletions app/masterpage.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,21 @@ type MasterPage struct {
subPages *PageStack
}

// NewMasterPage returns an instance of MasterPage.
func NewMasterPage(id string) *MasterPage {
return &MasterPage{
// NewMasterPage returns an instance of MasterPage. startPage is optional.
func NewMasterPage(id string, startPage Page) *MasterPage {
mp := &MasterPage{
GenericPageModal: NewGenericPageModal(id),
subPages: NewPageStack(id),
}

if startPage == nil {
return mp
}

// Bind the navigator to the page.
startPage.OnAttachedToNavigator(mp)
mp.subPages.pages = append(mp.subPages.pages, startPage)
return mp
}

// CurrentPage returns the page that is at the top of the stack. Returns nil if
Expand Down Expand Up @@ -47,6 +56,11 @@ func (masterPage *MasterPage) Display(newPage Page) {
if pushed {
masterPage.ParentWindow().Reload()
}

// Page is ready to be displayed.
newPage.OnAttachedToNavigator(masterPage)
newPage.OnNavigatedTo()
masterPage.ParentWindow().Reload()
}

// CloseCurrentPage dismisses the page at the top of the stack and gets the next
Expand Down
10 changes: 0 additions & 10 deletions app/pagestack.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@ func (pageStack *PageStack) PushAndNavigate(newPage Page, navigator PageNavigato
pageStack.mtx.Lock()
defer pageStack.mtx.Unlock()

if l := len(pageStack.pages); l > 0 {
currentPage := pageStack.pages[l-1]
if currentPage.ID() == newPage.ID() {
return false
}
currentPage.OnNavigatedFrom()
}

// Close any previous instance of this type. Use the Closed() method if
// implemented, to signal that the instance will never be re-displayed.
for i, existingPage := range pageStack.pages {
Expand All @@ -63,8 +55,6 @@ func (pageStack *PageStack) PushAndNavigate(newPage Page, navigator PageNavigato
}

pageStack.pages = append(pageStack.pages, newPage)
newPage.OnAttachedToNavigator(navigator)
newPage.OnNavigatedTo()
return true
}

Expand Down
5 changes: 5 additions & 0 deletions app/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func (window *SimpleWindowNavigator) Display(newPage Page) {
if pushed {
window.Reload()
}

// Page is ready to be displayed.
newPage.OnAttachedToNavigator(window)
newPage.OnNavigatedTo()
window.Reload()
}

// CloseCurrentPage dismisses the page at the top of the stack and gets the next
Expand Down
10 changes: 5 additions & 5 deletions ui/page/dcrdex/dcrdex_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/crypto-power/cryptopower/ui/load"
)

const DCRDEXID = "DCRDEXID"
const DCRDEXPageID = "DCRDEXID"

type (
C = layout.Context
Expand All @@ -31,7 +31,7 @@ type DEXPage struct {
func NewDEXPage(l *load.Load) *DEXPage {
dp := &DEXPage{
Load: l,
MasterPage: app.NewMasterPage(DCRDEXID),
MasterPage: app.NewMasterPage(DCRDEXPageID, nil),
openTradeMainPage: l.Theme.NewClickable(false),
}
return dp
Expand All @@ -41,7 +41,7 @@ func NewDEXPage(l *load.Load) *DEXPage {
// differentiate this page from other pages.
// Part of the load.Page interface.
func (pg *DEXPage) ID() string {
return DCRDEXID
return DCRDEXPageID
}

// OnNavigatedTo is called when the page is about to be displayed and may be
Expand All @@ -54,9 +54,9 @@ func (pg *DEXPage) OnNavigatedTo() {
if pg.CurrentPage() == nil {
// TODO: Handle pg.inited
pg.Display(NewDEXOnboarding(pg.Load))
} else {
pg.CurrentPage().OnNavigatedTo()
}

pg.CurrentPage().OnNavigatedTo()
}

// Layout draws the page UI components into the provided layout context to be
Expand Down
29 changes: 9 additions & 20 deletions ui/page/governance/governance_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var governanceTabTitles = []string{
func NewGovernancePage(l *load.Load) *Page {
pg := &Page{
Load: l,
MasterPage: app.NewMasterPage(GovernancePageID),
MasterPage: app.NewMasterPage(GovernancePageID, NewProposalsPage(l)),
modal: l.Theme.ModalFloatTitle(values.String(values.StrSettings)),
tabCategoryList: l.Theme.NewClickableList(layout.Horizontal),
}
Expand All @@ -58,11 +58,7 @@ func NewGovernancePage(l *load.Load) *Page {
// the page is displayed.
// Part of the load.Page interface.
func (pg *Page) OnNavigatedTo() {
if activeTab := pg.CurrentPage(); activeTab != nil {
activeTab.OnNavigatedTo()
} else {
pg.Display(NewProposalsPage(pg.Load))
}
pg.CurrentPage().OnNavigatedTo()
}

func (pg *Page) isGovernanceAPIAllowed() bool {
Expand All @@ -77,16 +73,10 @@ func (pg *Page) isGovernanceAPIAllowed() bool {
// components unless they'll be recreated in the OnNavigatedTo() method.
// Part of the load.Page interface.
func (pg *Page) OnNavigatedFrom() {
if activeTab := pg.CurrentPage(); activeTab != nil {
activeTab.OnNavigatedFrom()
}
pg.CurrentPage().OnNavigatedFrom()
}

func (pg *Page) HandleUserInteractions() {
if activeTab := pg.CurrentPage(); activeTab != nil {
activeTab.HandleUserInteractions()
}

if pg.navigateToSettingsBtn.Button.Clicked() {
pg.ParentWindow().Display(settings.NewSettingsPage(pg.Load))
}
Expand All @@ -96,19 +86,18 @@ func (pg *Page) HandleUserInteractions() {
}

if tabItemClicked, clickedTabIndex := pg.tabCategoryList.ItemClicked(); tabItemClicked {
if clickedTabIndex == 0 {
pg.Display(NewProposalsPage(pg.Load)) // Display should do nothing if the page is already displayed.
} else if clickedTabIndex == 1 {
currentPageID := pg.CurrentPageID()
if clickedTabIndex == 0 && currentPageID != ProposalsPageID {
pg.Display(NewProposalsPage(pg.Load))
} else if clickedTabIndex == 1 && currentPageID != ConsensusPageID {
pg.Display(NewConsensusPage(pg.Load))
} else {
} else if currentPageID != TreasuryPageID {
pg.Display(NewTreasuryPage(pg.Load))
}
}

// Handle individual page user interactions.
if activeTab := pg.CurrentPage(); activeTab != nil {
activeTab.HandleUserInteractions()
}
pg.CurrentPage().HandleUserInteractions()
}

func (pg *Page) Layout(gtx C) D {
Expand Down
32 changes: 15 additions & 17 deletions ui/page/root/home_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,19 @@ var navigationTabTitles = []string{
func NewHomePage(l *load.Load) *HomePage {
hp := &HomePage{
Load: l,
MasterPage: app.NewMasterPage(HomePageID),
isConnected: new(atomic.Bool),
}

// Initialize wallet page
hp.walletSelectorPage = NewWalletSelectorPage(l)
hp.showNavigationFunc = func(isHiddenNavigation bool) {
hp.isHiddenNavigation = isHiddenNavigation
}
hp.walletSelectorPage.showNavigationFunc = hp.showNavigationFunc

// Initialize master page.
hp.MasterPage = app.NewMasterPage(HomePageID, NewOverviewPage(l, hp.showNavigationFunc))

hp.hideBalanceButton = hp.Theme.NewClickable(false)
hp.appLevelSettingsButton = hp.Theme.NewClickable(false)
hp.appNotificationButton = hp.Theme.NewClickable(false)
Expand Down Expand Up @@ -113,13 +122,6 @@ func NewHomePage(l *load.Load) *HomePage {
}
l.ToggleSync = toggleSync

// initialize wallet page
hp.walletSelectorPage = NewWalletSelectorPage(l)
hp.showNavigationFunc = func(isHiddenNavigation bool) {
hp.isHiddenNavigation = isHiddenNavigation
}
hp.walletSelectorPage.showNavigationFunc = hp.showNavigationFunc

hp.initBottomNavItems()
hp.bottomNavigationBar.OnViewCreated()

Expand All @@ -138,6 +140,8 @@ func (hp *HomePage) ID() string {
// the page is displayed.
// Part of the load.Page interface.
func (hp *HomePage) OnNavigatedTo() {
hp.CurrentPage().OnNavigatedTo()

hp.ctx, hp.ctxCancel = context.WithCancel(context.TODO())

go hp.CalculateAssetsUSDBalance()
Expand Down Expand Up @@ -176,10 +180,6 @@ func (hp *HomePage) OnDarkModeChanged(isDarkModeOn bool) {
// displayed.
// Part of the load.Page interface.
func (hp *HomePage) HandleUserInteractions() {
if hp.CurrentPage() != nil {
hp.CurrentPage().HandleUserInteractions()
}

if hp.navigationTab.Changed() {
var pg app.Page
switch hp.navigationTab.SelectedTab() {
Expand Down Expand Up @@ -286,6 +286,8 @@ func (hp *HomePage) HandleUserInteractions() {
}
}
}

hp.CurrentPage().HandleUserInteractions()
}

func (hp *HomePage) showWarningNoWallet() {
Expand Down Expand Up @@ -334,11 +336,7 @@ func (hp *HomePage) OnCurrencyChanged() {
// components unless they'll be recreated in the OnNavigatedTo() method.
// Part of the load.Page interface.
func (hp *HomePage) OnNavigatedFrom() {
// Also remove all child pages.
if activeTab := hp.CurrentPage(); activeTab != nil {
activeTab.OnNavigatedFrom()
}

hp.CurrentPage().OnNavigatedFrom()
hp.ctxCancel()
}

Expand Down
10 changes: 4 additions & 6 deletions ui/page/root/main_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ type SingleWalletMasterPage struct {
func NewSingleWalletMasterPage(l *load.Load, wallet sharedW.Asset, showNavigationFunc func()) *SingleWalletMasterPage {
swmp := &SingleWalletMasterPage{
Load: l,
MasterPage: app.NewMasterPage(MainPageID),
selectedWallet: wallet,
MasterPage: app.NewMasterPage(MainPageID, info.NewInfoPage(l, wallet)),
checkBox: l.Theme.CheckBox(new(widget.Bool), values.String(values.StrAwareOfRisk)),
showNavigationFunc: showNavigationFunc,
selectedWallet: wallet,
}

swmp.activeTab = make(map[string]string)
Expand Down Expand Up @@ -262,10 +262,6 @@ func (swmp *SingleWalletMasterPage) OnCurrencyChanged() {
// displayed.
// Part of the load.Page interface.
func (swmp *SingleWalletMasterPage) HandleUserInteractions() {
if swmp.CurrentPage() != nil {
swmp.CurrentPage().HandleUserInteractions()
}

if swmp.refreshExchangeRateBtn.Clicked() {
go swmp.fetchExchangeRate()
}
Expand Down Expand Up @@ -332,6 +328,8 @@ func (swmp *SingleWalletMasterPage) HandleUserInteractions() {
swmp.isBalanceHidden = !swmp.isBalanceHidden
swmp.selectedWallet.SetBoolConfigValueForKey(sharedW.HideBalanceConfigKey, swmp.isBalanceHidden)
}

swmp.CurrentPage().HandleUserInteractions()
}

// KeysToHandle returns an expression that describes a set of key combinations
Expand Down
1 change: 0 additions & 1 deletion ui/page/root/overview_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ func (pg *OverviewPage) OnNavigatedTo() {
}

pg.listenForMixerNotifications() // listeners are stopped in OnNavigatedFrom().

}

// HandleUserInteractions is called just before Layout() to determine
Expand Down
27 changes: 10 additions & 17 deletions ui/page/root/trade_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type TradePage struct {
func NewTradePage(l *load.Load) *TradePage {
pg := &TradePage{
Load: l,
MasterPage: app.NewMasterPage(TradePageID),
MasterPage: app.NewMasterPage(TradePageID, dcrdex.NewDEXPage(l)),

shadowBox: l.Theme.Shadow(),
scrollContainer: &widget.List{
Expand Down Expand Up @@ -73,11 +73,7 @@ func (pg *TradePage) ID() string {
// the page is displayed.
// Part of the load.Page interface.
func (pg *TradePage) OnNavigatedTo() {
if activeTab := pg.CurrentPage(); activeTab != nil {
activeTab.OnNavigatedTo()
} else {
pg.Display(dcrdex.NewDEXPage(pg.Load))
}
pg.CurrentPage().OnNavigatedTo()
}

// HandleUserInteractions is called just before Layout() to determine
Expand All @@ -86,16 +82,15 @@ func (pg *TradePage) OnNavigatedTo() {
// displayed.
// Part of the load.Page interface.
func (pg *TradePage) HandleUserInteractions() {
if activeTab := pg.CurrentPage(); activeTab != nil {
activeTab.HandleUserInteractions()
if pg.tab.Changed() {
if pg.tab.SelectedIndex() == 1 {
pg.Display(exchange.NewCreateOrderPage(pg.Load))
} else if pg.CurrentPageID() != dcrdex.DCRDEXPageID {
pg.Display(dcrdex.NewDEXPage(pg.Load))
}
}

if pg.tab.SelectedIndex() == 0 {
pg.Display(dcrdex.NewDEXPage(pg.Load))
}
if pg.tab.SelectedIndex() == 1 {
pg.Display(exchange.NewCreateOrderPage(pg.Load))
}
pg.CurrentPage().HandleUserInteractions()
}

// OnNavigatedFrom is called when the page is about to be removed from
Expand All @@ -106,9 +101,7 @@ func (pg *TradePage) HandleUserInteractions() {
// components unless they'll be recreated in the OnNavigatedTo() method.
// Part of the load.Page interface.
func (pg *TradePage) OnNavigatedFrom() {
if activeTab := pg.CurrentPage(); activeTab != nil {
activeTab.OnNavigatedFrom()
}
pg.CurrentPage().OnNavigatedFrom()
}

// Layout draws the page UI components into the provided layout context
Expand Down

0 comments on commit 0342764

Please sign in to comment.