Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: dimension initial changes #14

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
registry=http://artifactory.fkinternal.com/artifactory/v1.0/artifacts/npm/
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "recyclerlistview-gridlayoutprovider",
"version": "1.0.5",
"version": "1.0.5-filter.1",
"description": "Grid Layout Provider built on top of RecyclerListView!",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -35,9 +35,11 @@
},
"homepage": "https://github.com/muskeinsingh/recyclerlistview-gridlayoutmanager",
"devDependencies": {
"recyclerlistview": "^2.0.0-beta.4"
"recyclerlistview": "^3.0.5-offsetFix",
"tslint": "^6.1.2",
"typescript": "3.7.2"
},
"peerDependencies": {
"recyclerlistview": ">=2.0.0-beta.4"
"recyclerlistview": ">=3.0.5-offsetFix"
}
}
60 changes: 46 additions & 14 deletions src/GridLayoutProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@

import { Dimension, Layout, LayoutProvider, LayoutManager } from "recyclerlistview";
import {
Dimension,
Layout,
LayoutProvider,
LayoutManager,
} from "recyclerlistview";
import { GridLayoutManager } from "./GridLayoutManager";

export class GridLayoutProvider extends LayoutProvider {
Expand All @@ -15,43 +19,71 @@ export class GridLayoutProvider extends LayoutProvider {
getSpan: (index: number) => number,
// If horizonal return width while spans will be rowspans. Opposite holds true if not horizontal
getHeightOrWidth: (index: number) => number,
acceptableRelayoutDelta?: number,
acceptableRelayoutDelta?: number
) {
super(
getLayoutType,
(type: string | number, dimension: Dimension, index: number) => {
this.setLayout(dimension, index);
},
}
);
this._getHeightOrWidth = getHeightOrWidth;
this._getSpan = getSpan;
this._maxSpan = maxSpan;
this._acceptableRelayoutDelta = ((acceptableRelayoutDelta === undefined) || (acceptableRelayoutDelta === null)) ? 1 : acceptableRelayoutDelta;
this._acceptableRelayoutDelta =
acceptableRelayoutDelta === undefined || acceptableRelayoutDelta === null
? 1
: acceptableRelayoutDelta;
}

public newLayoutManager(renderWindowSize: Dimension, isHorizontal?: boolean, cachedLayouts?: Layout[]): LayoutManager {
public newLayoutManager(
renderWindowSize: Dimension,
isHorizontal?: boolean,
cachedLayouts?: Layout[]
): LayoutManager {
this._isHorizontal = isHorizontal;
this._renderWindowSize = renderWindowSize;
return new GridLayoutManager(this, renderWindowSize, this._getSpan, this._maxSpan, this._acceptableRelayoutDelta, this._isHorizontal, cachedLayouts);
return new GridLayoutManager(
this,
renderWindowSize,
this._getSpan,
this._maxSpan,
this._acceptableRelayoutDelta,
this._isHorizontal,
cachedLayouts
);
}

private setLayout(dimension: Dimension, index: number): void {
const maxSpan: number = this._maxSpan;
const itemSpan: number = this._getSpan(index);
if (itemSpan > maxSpan) {
throw new Error("Item span for index " + index + " is more than the max span");
throw new Error(
"Item span for index " + index + " is more than the max span"
);
}
if (this._renderWindowSize) {
if (this._isHorizontal) {
dimension.width = this._getHeightOrWidth(index);
dimension.height = (this._renderWindowSize.height / maxSpan) * itemSpan;

const itemDim = this._renderWindowSize.height / maxSpan;
dimension.width = +this._getHeightOrWidth(index).toFixed(2);
dimension.height = parseInt(
(itemDim * itemSpan).toString().split(".")[0],
10
);
} else {
dimension.height = this._getHeightOrWidth(index);
dimension.width = (this._renderWindowSize.width / maxSpan) * itemSpan;
const itemDim = this._renderWindowSize.width / maxSpan;
dimension.height = +this._getHeightOrWidth(index).toFixed(2);
dimension.width = parseInt(
(itemDim * itemSpan).toString().split(".")[0],
10
);
}
// tslint:disable-next-line:no-console
console.log("RECYCLER_VIEW_WIDTH! ", { dimension });
} else {
throw new Error("setLayout called before layoutmanager was created, cannot be handled");
throw new Error(
"setLayout called before layoutmanager was created, cannot be handled"
);
}
}
}