Skip to content

Commit 8b97f06

Browse files
committed
fix #19
1 parent c3163f1 commit 8b97f06

File tree

6 files changed

+113
-110
lines changed

6 files changed

+113
-110
lines changed

demo/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
"nativescript": {
33
"id": "com.tangrainc.gridviewsample",
44
"tns-ios": {
5-
"version": "3.0.0-rc.1"
5+
"version": "3.1.0"
66
},
77
"tns-android": {
8-
"version": "3.0.0-rc.1"
8+
"version": "3.1.1"
99
}
1010
},
1111
"scripts": {
@@ -19,7 +19,7 @@
1919
},
2020
"dependencies": {
2121
"nativescript-grid-view": "file:../bin/dist",
22-
"tns-core-modules": "rc"
22+
"tns-core-modules": "3.0.1"
2323
},
2424
"devDependencies": {
2525
"awesome-typescript-loader": "~3.0.0-beta.9",

grid-view-common.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ export abstract class GridViewBase extends View implements GridViewDefinition {
3737
public itemTemplate: string | Template;
3838
public items: any[] | ItemsSource;
3939
public isItemsSourceIn: boolean;
40-
public rowHeight: Length;
41-
public colWidth: Length;
40+
public rowHeight: PercentLength;
41+
public colWidth: PercentLength;
4242
public verticalSpacing: Length;
4343
public horizontalSpacing: Length;
4444
public _innerWidth: number = 0;
@@ -52,10 +52,10 @@ export abstract class GridViewBase extends View implements GridViewDefinition {
5252
super.onLayout(left, top, right, bottom);
5353

5454
this._innerWidth = right - this.effectivePaddingLeft - this.effectivePaddingRight;
55-
this._effectiveColWidth = PercentLength.toDevicePixels(this.colWidth, 0, this._innerWidth);
55+
this._effectiveColWidth = PercentLength.toDevicePixels(this.colWidth, autoEffectiveColWidth, this._innerWidth); // We cannot use 0 for auto as it throws for android.
5656

5757
this._innerHeight = bottom - this.effectivePaddingTop - this.effectivePaddingBottom;
58-
this._effectiveRowHeight = PercentLength.toDevicePixels(this.rowHeight, 0, this._innerHeight);
58+
this._effectiveRowHeight = PercentLength.toDevicePixels(this.rowHeight, autoEffectiveRowHeight, this._innerHeight);
5959
}
6060

6161
public _getItemTemplateContent(): View {
@@ -118,7 +118,7 @@ rowHeightProperty.register(GridViewBase);
118118
const defaultColWidth: PercentLength = "auto";
119119
export const colWidthProperty = new CoercibleProperty<GridViewBase, PercentLength>({
120120
name: "colWidth",
121-
defaultValue: defaultColWidth,
121+
defaultValue: PercentLength.parse("100"),
122122
equalityComparer: PercentLength.equals,
123123
valueConverter: PercentLength.parse,
124124
coerceValue: (target, value) => {

grid-view.android.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ import * as utils from "utils/utils";
1919

2020
import {
2121
GridViewBase,
22+
colWidthProperty,
2223
paddingBottomProperty,
2324
paddingLeftProperty,
2425
paddingRightProperty,
25-
paddingTopProperty
26+
paddingTopProperty,
27+
rowHeightProperty,
2628
} from "./grid-view-common";
2729

2830
import { GridItemEventData } from ".";
@@ -59,6 +61,9 @@ export class GridView extends GridViewBase {
5961
nativeView.adapter.owner = new WeakRef(this);
6062
nativeView.scrollListener.owner = new WeakRef(this);
6163
nativeView.owner = new WeakRef(this);
64+
65+
colWidthProperty.coerce(this);
66+
rowHeightProperty.coerce(this);
6267
}
6368

6469
public disposeNativeView() {

grid-view.ios.ts

Lines changed: 95 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -32,103 +32,6 @@ export * from "./grid-view-common";
3232

3333
const CELLIDENTIFIER = "gridcell";
3434

35-
class GridViewCell extends UICollectionViewCell {
36-
public static new(): GridViewCell {
37-
return super.new() as GridViewCell;
38-
}
39-
public static class(): any {
40-
return GridViewCell;
41-
}
42-
43-
public owner: WeakRef<View>;
44-
45-
get view(): View {
46-
return this.owner ? this.owner.get() : null;
47-
}
48-
}
49-
50-
class GridViewDataSource extends NSObject implements UICollectionViewDataSource {
51-
public static ObjCProtocols = [UICollectionViewDataSource];
52-
53-
public static initWithOwner(owner: WeakRef<GridView>): GridViewDataSource {
54-
const dataSource = GridViewDataSource.new() as GridViewDataSource;
55-
dataSource._owner = owner;
56-
return dataSource;
57-
}
58-
59-
private _owner: WeakRef<GridView>;
60-
61-
public numberOfSectionsInCollectionView(collectionView: UICollectionView) {
62-
return 1;
63-
}
64-
65-
public collectionViewNumberOfItemsInSection(collectionView: UICollectionView, section: number) {
66-
const owner = this._owner.get();
67-
return owner.items ? owner.items.length : 0;
68-
}
69-
70-
public collectionViewCellForItemAtIndexPath(collectionView: UICollectionView, indexPath: NSIndexPath): UICollectionViewCell {
71-
const owner = this._owner.get();
72-
const cell: any = collectionView.dequeueReusableCellWithReuseIdentifierForIndexPath(CELLIDENTIFIER, indexPath) || GridViewCell.new();
73-
74-
owner._prepareCell(cell, indexPath);
75-
76-
const cellView: View = cell.view;
77-
if (cellView) {
78-
View.layoutChild(owner, cellView, 0, 0, owner._effectiveColWidth, owner._effectiveRowHeight);
79-
}
80-
81-
return cell;
82-
}
83-
}
84-
85-
class UICollectionViewDelegateImpl extends NSObject implements UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
86-
public static ObjCProtocols = [UICollectionViewDelegate, UICollectionViewDelegateFlowLayout];
87-
88-
public static initWithOwner(owner: WeakRef<GridView>): UICollectionViewDelegateImpl {
89-
const delegate = UICollectionViewDelegateImpl.new() as UICollectionViewDelegateImpl;
90-
delegate._owner = owner;
91-
return delegate;
92-
}
93-
94-
private _owner: WeakRef<GridView>;
95-
96-
public collectionViewWillDisplayCellForItemAtIndexPath(collectionView: UICollectionView, cell: UICollectionViewCell, indexPath: NSIndexPath) {
97-
const owner = this._owner.get();
98-
99-
if (indexPath.row === owner.items.length - 1) {
100-
owner.notify({
101-
eventName: GridViewBase.loadMoreItemsEvent,
102-
object: owner
103-
} as EventData);
104-
}
105-
106-
if (cell.preservesSuperviewLayoutMargins) {
107-
cell.preservesSuperviewLayoutMargins = false;
108-
}
109-
110-
if (cell.layoutMargins) {
111-
cell.layoutMargins = UIEdgeInsetsZero;
112-
}
113-
}
114-
115-
public collectionViewDidSelectItemAtIndexPath(collectionView: UICollectionView, indexPath: NSIndexPath) {
116-
const cell = collectionView.cellForItemAtIndexPath(indexPath);
117-
const owner = this._owner.get();
118-
119-
owner.notify({
120-
eventName: GridViewBase.itemTapEvent,
121-
object: owner,
122-
index: indexPath.row,
123-
view: (cell as GridViewCell).view
124-
} as GridItemEventData);
125-
126-
cell.highlighted = false;
127-
128-
return indexPath;
129-
}
130-
}
131-
13235
export class GridView extends GridViewBase {
13336
private _layout: UICollectionViewFlowLayout;
13437
private _dataSource: GridViewDataSource;
@@ -320,4 +223,99 @@ export class GridView extends GridViewBase {
320223
this._layout.sectionInset =
321224
UIEdgeInsetsFromString(`{${newValue.top},${newValue.left},${newValue.bottom},${newValue.right}}`);
322225
}
226+
}
227+
228+
class GridViewCell extends UICollectionViewCell {
229+
public static new(): GridViewCell {
230+
return super.new() as GridViewCell;
231+
}
232+
public static class(): any {
233+
return GridViewCell;
234+
}
235+
236+
public owner: WeakRef<View>;
237+
238+
get view(): View {
239+
return this.owner ? this.owner.get() : null;
240+
}
241+
}
242+
243+
@ObjCClass(UICollectionViewDataSource)
244+
class GridViewDataSource extends NSObject implements UICollectionViewDataSource {
245+
public static initWithOwner(owner: WeakRef<GridView>): GridViewDataSource {
246+
const dataSource = GridViewDataSource.new() as GridViewDataSource;
247+
dataSource._owner = owner;
248+
return dataSource;
249+
}
250+
251+
private _owner: WeakRef<GridView>;
252+
253+
public numberOfSectionsInCollectionView(collectionView: UICollectionView) {
254+
return 1;
255+
}
256+
257+
public collectionViewNumberOfItemsInSection(collectionView: UICollectionView, section: number) {
258+
const owner = this._owner.get();
259+
return owner.items ? owner.items.length : 0;
260+
}
261+
262+
public collectionViewCellForItemAtIndexPath(collectionView: UICollectionView, indexPath: NSIndexPath): UICollectionViewCell {
263+
const owner = this._owner.get();
264+
const cell: any = collectionView.dequeueReusableCellWithReuseIdentifierForIndexPath(CELLIDENTIFIER, indexPath) || GridViewCell.new();
265+
266+
owner._prepareCell(cell, indexPath);
267+
268+
const cellView: View = cell.view;
269+
if (cellView) {
270+
View.layoutChild(owner, cellView, 0, 0, owner._effectiveColWidth, owner._effectiveRowHeight);
271+
}
272+
273+
return cell;
274+
}
275+
}
276+
277+
@ObjCClass(UICollectionViewDelegate, UICollectionViewDelegateFlowLayout)
278+
class UICollectionViewDelegateImpl extends NSObject implements UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
279+
public static initWithOwner(owner: WeakRef<GridView>): UICollectionViewDelegateImpl {
280+
const delegate = UICollectionViewDelegateImpl.new() as UICollectionViewDelegateImpl;
281+
delegate._owner = owner;
282+
return delegate;
283+
}
284+
285+
private _owner: WeakRef<GridView>;
286+
287+
public collectionViewWillDisplayCellForItemAtIndexPath(collectionView: UICollectionView, cell: UICollectionViewCell, indexPath: NSIndexPath) {
288+
const owner = this._owner.get();
289+
290+
if (indexPath.row === owner.items.length - 1) {
291+
owner.notify({
292+
eventName: GridViewBase.loadMoreItemsEvent,
293+
object: owner
294+
} as EventData);
295+
}
296+
297+
if (cell.preservesSuperviewLayoutMargins) {
298+
cell.preservesSuperviewLayoutMargins = false;
299+
}
300+
301+
if (cell.layoutMargins) {
302+
cell.layoutMargins = UIEdgeInsetsZero;
303+
}
304+
}
305+
306+
public collectionViewDidSelectItemAtIndexPath(collectionView: UICollectionView, indexPath: NSIndexPath) {
307+
const cell = collectionView.cellForItemAtIndexPath(indexPath);
308+
const owner = this._owner.get();
309+
310+
owner.notify({
311+
eventName: GridViewBase.itemTapEvent,
312+
object: owner,
313+
index: indexPath.row,
314+
view: (cell as GridViewCell).view
315+
} as GridItemEventData);
316+
317+
cell.highlighted = false;
318+
319+
return indexPath;
320+
}
323321
}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-grid-view",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"description": "A NativeScript GridView widget.",
55
"main": "grid-view",
66
"typings": "grid-view.d.ts",
@@ -39,8 +39,8 @@
3939
"devDependencies": {
4040
"typescript": "~2.2.0",
4141
"tslint": "^5.1.0",
42-
"tns-core-modules": "3.0.0",
43-
"tns-platform-declarations": "3.0.0",
42+
"tns-core-modules": "3.1.0",
43+
"tns-platform-declarations": "3.1.0",
4444

4545
"grunt": "1.0.1",
4646
"grunt-contrib-copy": "1.0.0",

tslint.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"trailing-comma": [
1414
true,
1515
{
16-
"multiline": "never",
16+
"multiline": "ignore",
1717
"singleline": "never"
1818
}
1919
],

0 commit comments

Comments
 (0)