Skip to content

Commit 73b094c

Browse files
committed
Implemented loading menu item (#290)
1 parent 03a0feb commit 73b094c

5 files changed

Lines changed: 55 additions & 7 deletions

File tree

source/main/user-interface/features/sitemap/base/menu-items/WidgetMenuItem.mc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,13 @@ class WidgetMenuItem extends StructuredMenuItem {
122122
// do not need to override this method.
123123
public function onSelect() as Boolean {
124124
if( _page != null ) {
125-
ViewStack.pushView( _page, PageMenuDelegate.get(), WatchUi.SLIDE_LEFT );
125+
var page = _page;
126+
Logger.debug( "WidgetMenuItem.onSelect: pushing page" );
127+
// Pushing a CustomMenu with no menu items leads to weird behavior.
128+
// Therefore ensureItems() adds a LoadingMenuItem if no other
129+
// items are present at this time. See ensureItems() for details.
130+
page.ensureItems();
131+
ViewStack.pushView( page, PageMenuDelegate.get(), WatchUi.SLIDE_LEFT );
126132
return true;
127133
} else {
128134
if( ! ConnectionManager.get().isConnected() ) {

source/main/user-interface/features/sitemap/pages/base/BasePageMenu.mc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class BasePageMenu extends BaseMenu {
7878

7979
// Create a menu item for each widget.
8080
if( processingMode == PROCESSING_TOP_LEVEL_SYNC ) {
81-
// Logger.debug( "BasePageMenu.initialize: synchronous processing" );
81+
Logger.debug( "BasePageMenu.initialize: synchronous processing" );
8282
// In top-level sync mode, menu items are created synchronously.
8383
// The next level is then initialized in PROCESSING_ASYNC_AFTER_SYNC mode.
8484
for( var i = 0; i < widgets.size(); i++ ) {

source/main/user-interface/features/sitemap/pages/base/UpdateTasks.mc

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ class AddOrUpdateMenuItemTask extends BaseSitemapProcessorTask {
6565
} else {
6666
// If the item is found, we check if the type of the menu
6767
// item is the same or has changed
68-
var item = _pageMenu.getItem( _index ) as WidgetMenuItem;
69-
if( item.isMyType( _sitemapWidget ) ) {
68+
var item = _pageMenu.getItem( _index );
69+
70+
if( item instanceof WidgetMenuItem && item.isMyType( _sitemapWidget ) ) {
7071
// If the type matches, the menu item is updated.
7172
// However, if the item was updated internally within the configured
7273
// post-command hold time, the update is ignored. Internal updates are used
@@ -79,15 +80,21 @@ class AddOrUpdateMenuItemTask extends BaseSitemapProcessorTask {
7980
item.updateWidget( _sitemapWidget );
8081
}
8182
} else {
82-
// If the type is not the same, we create a new item
83-
// and replace the existing menu item with it
83+
// If the item is not a WidgetMenuItem or the widget type
84+
// does not match, create a new item and replace the
85+
// existing menu item.
8486
var newItem = MenuItemFactory.createMenuItem(
8587
_sitemapWidget,
8688
_pageMenu,
8789
BasePageMenu.PROCESSING_ASYNC
8890
);
8991

90-
if( item.hasPage() || newItem.hasPage() ) {
92+
// If either the previous or the new menu item has sub-pages,
93+
// invalidate the structure. This causes the app to jump back
94+
// to the homepage menu.
95+
if( ( item instanceof WidgetMenuItem && item.hasPage() )
96+
|| newItem.hasPage()
97+
) {
9198
_pageMenu.invalidateStructure();
9299
// Logger.debug( "AddOrUpdateMenuItemTask: page '" + _pageMenu.getTitle() + "' invalid because item '" + item.getLabel() + "' changed type from/to page" );
93100
}

source/main/user-interface/features/sitemap/pages/page/PageMenu.mc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,31 @@ class PageMenu extends BasePageMenu {
2626
_weakParent = parent.weak();
2727
}
2828

29+
// Overrides the base class addItem() to remove the loading
30+
// item if present.
31+
public function addItem( item as CustomMenuItem ) as Void {
32+
Logger.debug( "PageMenu.addItem" );
33+
if( getItem( 0 ) instanceof LoadingMenuItem ) {
34+
Logger.debug( "PageMenu.addItem: removing loading item" );
35+
deleteItem( 0 );
36+
}
37+
BaseMenu.addItem( item );
38+
}
39+
40+
// Checks if menu items are present and adds the loading menu item
41+
// if none exist.
42+
// Pushing an empty menu as a view leads to weird side effects,
43+
// therefore this is called every time before a page menu is pushed.
44+
// The loading menu item is removed when the first real item
45+
// is added. See addItem() above.
46+
public function ensureItems() as Void {
47+
Logger.debug( "PageMenu.ensureItems" );
48+
if( getItemCount() < 1 ) {
49+
Logger.debug( "PageMenu.ensureItems: adding loading item" );
50+
BasePageMenu.addItem( new LoadingMenuItem() );
51+
}
52+
}
53+
2954
// See BasePageMenu.invalidateStructure for details
3055
public function invalidateStructure() as Void {
3156
var parent = _weakParent.get() as BasePageMenu?;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Toybox.Lang;
2+
import Toybox.WatchUi;
3+
4+
class LoadingMenuItem extends StructuredMenuItem {
5+
6+
// Constructor
7+
public function initialize() {
8+
StructuredMenuItem.initialize( { :label => "Loading..." } );
9+
}
10+
}

0 commit comments

Comments
 (0)