Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion apps/automated/src/ui/list-view/list-view-tests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as TKUnit from '../../tk-unit';
import * as helper from '../../ui-helper';
import { UITest } from '../../ui-test';
import { isAndroid, Page, View, KeyedTemplate, Utils, Observable, EventData, ObservableArray, Label, Application, ListView, ItemEventData, StackLayout } from '@nativescript/core';
import { isAndroid, Page, View, KeyedTemplate, Utils, Observable, EventData, ObservableArray, Label, Application, ListView, ItemEventData, StackLayout, Builder } from '@nativescript/core';
import { MyButton, MyStackLayout } from '../layouts/layout-helper';

// >> article-item-tap
Expand Down Expand Up @@ -872,6 +872,45 @@ export class ListViewTest extends UITest<ListView> {
}
}

public test_stickyHeaderTemplate_xml_element_form_is_a_template() {
const listView = <ListView>Builder.parse(`<ListView><ListView.stickyHeaderTemplate><Label text="{{ title }}" /></ListView.stickyHeaderTemplate></ListView>`);

TKUnit.assert(Utils.isFunction(listView.stickyHeaderTemplate), 'stickyHeaderTemplate element form should produce a template function');
}

public test_stickyHeader_function_template_binds_section() {
this.setUp();

const listView = this.testView;
listView.sectioned = true;
listView.stickyHeader = true;
listView.stickyHeaderTemplate = () => {
const lbl = new Label();
lbl.bind({ sourceProperty: 'title', targetProperty: 'text' });

return lbl;
};
listView.items = [
{ title: 'First', items: ['a', 'b'] },
{ title: 'Second', items: ['c'] },
];

this.waitUntilTestElementIsLoaded();
this.waitUntilTestElementLayoutIsValid();

let text: string;
if (isAndroid) {
TKUnit.waitUntilReady(() => !!(<any>listView)._stickyHeaderView);
text = this.getStickyHeaderTextAndroid(listView);
} else {
const table = <UITableView>listView.ios;
const header = (<any>table.delegate).tableViewViewForHeaderInSection(table, 0);
text = this.getTextFromNativeHeaderForSection(listView, header);
}

TKUnit.assertEqual(text, 'First', 'sticky header built from a function template binds section data');
}

public test_stickyHeader_Android_padding_after_garbage_collection() {
if (!isAndroid) {
return;
Expand Down
1 change: 1 addition & 0 deletions apps/ui/src/list-view/main-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function loadExamples() {
examples.set('list-view-templates', 'list-view/list-view-page');
examples.set('images-template', 'list-view/images-template-page');
examples.set('dynamic-templates', 'list-view/dynamic-templates-page');
examples.set('sectioned-listview', 'list-view/sectioned-listview-page');
examples.set('bindings', 'list-view/listview-binding-page');
examples.set('listview-bg-separator-color', 'list-view/listview-bg-separator-color-page');
examples.set('csslv', 'list-view/csslv-page');
Expand Down
34 changes: 34 additions & 0 deletions apps/ui/src/list-view/sectioned-listview-page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Page } from '@nativescript/core/ui/page';
import { ItemEventData, ListView } from '@nativescript/core/ui/list-view';
import { Item } from './main-view-model';

export function pageLoaded(args) {
const page = <Page>args.object;
page.bindingContext = {
sections: createSections(),
};
}

function createSections() {
const sections = [];
let id = 0;
for (let s = 0; s < 10; s++) {
const items: Item[] = [];
for (let i = 0; i < 8; i++) {
items.push(new Item('Item', id++, i));
}
sections.push({ title: `Section ${s + 1}`, items });
}

return sections;
}

export function onItemTap(args: ItemEventData) {
const item = args.view?.bindingContext;
if (!(item instanceof Item)) {
return;
}

item.age = item.age + 1;
(<ListView>args.object).refresh();
}
16 changes: 16 additions & 0 deletions apps/ui/src/list-view/sectioned-listview-page.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Page loaded="pageLoaded">
<ListView id="lv-sections" items="{{ sections }}" sectioned="true" stickyHeader="true" stickyHeaderHeight="36"
itemTemplateSelector="age % 2 === 0 ? 'green':'red'" itemTap="onItemTap">
<ListView.stickyHeaderTemplate>
<Label text="{{ title }}" backgroundColor="#333" color="white" fontWeight="bold" padding="8 12"/>
</ListView.stickyHeaderTemplate>
<ListView.itemTemplates>
<template key="red">
<Label text="{{ name + ' ' + id + ' (age ' + age + ')' }}" style.backgroundColor="red"/>
</template>
<template key="green">
<Label text="{{ name + ' ' + id + ' (age ' + age + ')' }}" style.backgroundColor="green"/>
</template>
</ListView.itemTemplates>
</ListView>
</Page>
2 changes: 1 addition & 1 deletion packages/core/ui/builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class Builder {
/**
* UI plugin developers can add to these to define their own custom types if needed
*/
static knownTemplates: Set<string> = new Set(['itemTemplate']);
static knownTemplates: Set<string> = new Set(['itemTemplate', 'stickyHeaderTemplate']);
static knownMultiTemplates: Set<string> = new Set(['itemTemplates']);
static knownCollections: Set<string> = new Set(['items', 'spans', 'actionItems']);

Expand Down
41 changes: 4 additions & 37 deletions packages/core/ui/list-view/index.android.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ItemEventData, ItemsSource, SearchEventData } from '.';
import { ListViewBase, separatorColorProperty, itemTemplatesProperty, stickyHeaderProperty, stickyHeaderTemplateProperty, stickyHeaderHeightProperty, sectionedProperty, showSearchProperty } from './list-view-common';
import { View, KeyedTemplate } from '../core/view';
import { View, KeyedTemplate, type Template } from '../core/view';
import { PercentLength } from '../styling/length-shared';
import { unsetValue } from '../core/properties/property-shared';
import { CoreTypes } from '../../core-types';
Expand All @@ -11,8 +11,6 @@ import { ProxyViewContainer } from '../proxy-view-container';
import { LayoutBase } from '../layouts/layout-base';
import { profile } from '../../profiling';
import { Trace } from '../../trace';
import { Builder } from '../builder';
import { Label } from '../label';

export * from './list-view-common';

Expand Down Expand Up @@ -348,22 +346,7 @@ export class ListView extends ListViewBase {
return; // Already created
}

// Create header view using the same template as section headers
if (typeof this.stickyHeaderTemplate === 'string') {
try {
this._stickyHeaderView = Builder.parse(this.stickyHeaderTemplate, this);
} catch (error) {
// Fallback to simple label
this._stickyHeaderView = new Label();
(this._stickyHeaderView as Label).text = 'Header Error';
}
}

if (!this._stickyHeaderView) {
// Default header
this._stickyHeaderView = new Label();
(this._stickyHeaderView as Label).text = 'Section 0';
}
this._stickyHeaderView = this._resolveStickyHeaderView(0);

// Set initial binding context (section 0)
this._updateStickyHeader(0);
Expand Down Expand Up @@ -638,7 +621,7 @@ export class ListView extends ListViewBase {
}
}

[stickyHeaderTemplateProperty.setNative](value: string) {
[stickyHeaderTemplateProperty.setNative](value: string | Template) {
// Refresh adapter when template changes
if (this.nativeViewProtected && this.nativeViewProtected.getAdapter()) {
this.nativeViewProtected.setAdapter(new ListViewAdapterClass(this));
Expand Down Expand Up @@ -1079,23 +1062,7 @@ function ensureListViewAdapterClass() {

// Create new header view if we can't reuse
if (!headerView) {
if (this.owner.stickyHeaderTemplate) {
if (typeof this.owner.stickyHeaderTemplate === 'string') {
try {
headerView = Builder.parse(this.owner.stickyHeaderTemplate, this.owner);
} catch (error) {
// Fallback to simple label
headerView = new Label();
(headerView as Label).text = 'Header Error';
}
}
}

if (!headerView) {
// Default header
headerView = new Label();
(headerView as Label).text = `Section ${section}`;
}
headerView = this.owner._resolveStickyHeaderView(section);

// Add to parent and get native view
if (!headerView.parent) {
Expand Down
63 changes: 1 addition & 62 deletions packages/core/ui/list-view/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import { StackLayout } from '../layouts/stack-layout';
import { ProxyViewContainer } from '../proxy-view-container';
import { profile } from '../../profiling';
import { Trace } from '../../trace';
import { Builder } from '../builder';
import { Label } from '../label';
import { isFunction } from '../../utils/types';

export * from './list-view-common';

Expand Down Expand Up @@ -459,7 +456,6 @@ export class ListView extends ListViewBase {
private _map: Map<ListViewCell, ItemView>;
private _headerMap: Map<ListViewHeaderCell, View>;
private _preparingHeader: boolean;
private _headerTemplateCache: View;
private _searchController: UISearchController;
private _searchDelegate: UISearchResultsUpdatingImpl;
_isSearchActive: boolean = false;
Expand Down Expand Up @@ -962,16 +958,7 @@ export class ListView extends ListViewBase {
this._preparingHeader = true;
let view: View = headerCell.view;
if (!view) {
view = this._getHeaderTemplate();
if (!view) {
if (Trace.isEnabled()) {
Trace.write(`ListView: Failed to create header view for section ${section}`, Trace.categories.Debug);
}
// Create a fallback view
const lbl = new Label();
lbl.text = `Section ${section}`;
view = lbl;
}
view = this._resolveStickyHeaderView(section);
}

// Handle header cell reuse
Expand Down Expand Up @@ -1050,52 +1037,6 @@ export class ListView extends ListViewBase {
return 44;
}

private _getHeaderTemplate(): View {
if (this.stickyHeaderTemplate) {
if (__UI_USE_EXTERNAL_RENDERER__) {
if (isFunction(this.stickyHeaderTemplate)) {
return (<Template>this.stickyHeaderTemplate)();
}
} else {
if (typeof this.stickyHeaderTemplate === 'string') {
try {
const parsed = Builder.parse(this.stickyHeaderTemplate, this);
if (!parsed) {
// Create a simple fallback
const fallbackLabel = new Label();
fallbackLabel.text = 'Parse Failed';
return fallbackLabel;
}
return parsed;
} catch (error) {
if (Trace.isEnabled()) {
Trace.write(`ListView: Template parsing error: ${error}`, Trace.categories.Debug);
}
// Create a simple fallback
const errorLabel = new Label();
errorLabel.text = 'Template Error';
return errorLabel;
}
} else {
const view = (<Template>this.stickyHeaderTemplate)();
if (Trace.isEnabled()) {
Trace.write(`ListView: Created header view from template function: ${!!view} (type: ${view?.constructor?.name})`, Trace.categories.Debug);
}
return view;
}
}
}

if (Trace.isEnabled()) {
Trace.write(`ListView: No sticky header template, creating default`, Trace.categories.Debug);
}

// Return a default header if no template is provided
const lbl = new Label();
lbl.text = 'Default Header';
return lbl;
}

public _removeHeaderContainer(headerCell: ListViewHeaderCell): void {
const view: View = headerCell.view;
const root = view.parent instanceof ListView ? view : view.parent;
Expand Down Expand Up @@ -1166,8 +1107,6 @@ export class ListView extends ListViewBase {
if (Trace.isEnabled()) {
Trace.write(`ListView: stickyHeaderTemplate set: ${typeof value} ${value ? '(has value)' : '(null)'}`, Trace.categories.Debug);
}
// Clear any cached template
this._headerTemplateCache = null;
// Immediately refresh to apply changes
if (this.isLoaded) {
this.refresh();
Expand Down
26 changes: 26 additions & 0 deletions packages/core/ui/list-view/list-view-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,32 @@ export abstract class ListViewBase extends ContainerView implements ListViewDefi
return lbl;
}

public _resolveStickyHeaderView(section: number): View {
const template = this.stickyHeaderTemplate;
let view: View;
try {
if (__UI_USE_EXTERNAL_RENDERER__) {
if (isFunction(template)) {
view = (<Template>template)();
}
} else if (template) {
view = Builder.parse(template, this);
}
} catch (error) {
if (Trace.isEnabled()) {
Trace.write(`ListView: sticky header template error: ${error}`, Trace.categories.Debug);
}
}

if (!view) {
const lbl = new Label();
lbl.text = `Section ${section}`;
view = lbl;
}

return view;
}

public _onItemsChanged(args: ChangedData<any>) {
this.refresh();
}
Expand Down