Skip to content

Commit

Permalink
Merge pull request #98 from flexn-io/feat/move-viewport-top-top
Browse files Browse the repository at this point in the history
Feat/move viewport top top
  • Loading branch information
aurimasmi committed Jun 7, 2023
2 parents 087c660 + 416013c commit 900835a
Show file tree
Hide file tree
Showing 14 changed files with 230 additions and 56 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.21.2-alpha.10",
"version": "0.21.2-alpha.13",
"packages": [
"packages/*"
],
Expand Down
2 changes: 1 addition & 1 deletion packages/app-harness/metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "app-harness",
"version": "0.21.2-alpha.10",
"version": "0.21.2-alpha.13",
"identifier": "io.flexn.app.harness",
"icon": "./static/icon.png"
}
8 changes: 4 additions & 4 deletions packages/app-harness/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@flexn/app-harness",
"version": "0.21.2-alpha.10",
"version": "0.21.2-alpha.13",
"scripts": {
"bootstrap": "cd ../.. && yarn bootstrap",
"build": "yarn clean && yarn compile",
Expand Down Expand Up @@ -40,7 +40,7 @@
"web:release": "npx rnv build -p web -c harness -s release && vercel ./platformBuilds/harness_web/project"
},
"dependencies": {
"@flexn/create": "0.21.2-alpha.10",
"@flexn/create": "0.21.2-alpha.13",
"@flexn/recyclerlistview": "4.2.6",
"@flexn/shopify-flash-list": "1.4.7",
"@lightningjs/core": "2.8.0",
Expand Down Expand Up @@ -81,9 +81,9 @@
"react-native-windows": "0.67.3"
},
"devDependencies": {
"@flexn/graybox": "0.21.2-alpha.10",
"@flexn/graybox": "0.21.2-alpha.13",
"@flexn/plugins": "1.0.7",
"@flexn/template": "0.21.2-alpha.10",
"@flexn/template": "0.21.2-alpha.13",
"@lightningjs/cli": "2.11.0",
"@react-native-community/cli": "^6.0.0",
"@react-native-community/cli-platform-ios": "^6.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/app-harness/renative.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"projectName": "app-harness",
"templates": {
"@flexn/template": {
"version": "0.21.2-alpha.10"
"version": "0.21.2-alpha.13"
}
},
"workspaceID": "flexn"
Expand Down
2 changes: 2 additions & 0 deletions packages/app-harness/src/navigation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Overflow from '../screens/overflow';
import PressableAnimations from '../screens/pressableAnimations';
import ViewGroup from '../screens/viewGroup';
import Grid from '../screens/grid';
import ScrollToTop from '../screens/scrollToTop';

const RootStack = createNativeStackNavigator();

Expand All @@ -36,6 +37,7 @@ const Navigation = () => {
<RootStack.Screen name="Overflow" component={Overflow} />
<RootStack.Screen name="PressableAnimations" component={PressableAnimations} />
<RootStack.Screen name="ViewGroup" component={ViewGroup} />
<RootStack.Screen name="ScrollToTop" component={ScrollToTop} />
</RootStack.Navigator>
</NavigationContainer>
);
Expand Down
177 changes: 177 additions & 0 deletions packages/app-harness/src/screens/scrollToTop.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import React from 'react';
import { StyleSheet, Text } from 'react-native';
import { ScrollView, View, Pressable, Image, CreateListRenderItemInfo, FlashList } from '@flexn/create';
import { getScaledValue } from '@rnv/renative';
import Screen from './screen';
import { Button } from '../components/Button';
import { Ratio } from '../utils';

const border = {
type: 'border',
focus: {
borderWidth: 5,
borderColor: 'yellow',
},
blur: {
borderWidth: 4,
borderColor: 'grey',
},
};

const kittyNames = ['Abby', 'Angel', 'Annie', 'Baby', 'Bailey', 'Bandit'];

function interval(min = 0, max = kittyNames.length - 1) {
return Math.floor(Math.random() * (max - min + 1) + min);
}

function generateData(width: number, height: number, items = 30) {
const temp: any = [];
for (let index = 0; index < items; index++) {
temp.push({
index,
backgroundImage: `https://placekitten.com/${width}/${height}`,
title: `${kittyNames[interval()]} ${kittyNames[interval()]} ${kittyNames[interval()]}`,
});
}

return temp;
}

const ScrollToTop = () => {
const [data] = React.useState(generateData(200, 200, 12));
const [scrollViewData] = React.useState(generateData(200, 200, 30));

const rowRenderer = ({ item, focusRepeatContext }: CreateListRenderItemInfo<any>) => {
return (
<Pressable
style={styles.packshot}
focusRepeatContext={focusRepeatContext}
focusOptions={{ animatorOptions: border }}
>
<Image source={{ uri: item.backgroundImage }} style={styles.image} />
</Pressable>
);
};

return (
<Screen style={{ backgroundColor: '#222222' }} focusOptions={{ verticalViewportOffset: Ratio(100) }}>
<ScrollView>
<View style={{ borderColor: 'red', borderWidth: 3 }}>
<Text style={{ color: 'white', textAlign: 'center' }}>
If you see me while moving up test works
</Text>
</View>
<View style={{ marginTop: Ratio(20), flexDirection: 'row' }}>
<View>
<Button
style={{ ...styles.button, ...styles.button1 }}
title="Button1"
textStyle={styles.buttonTextStyle}
focusOptions={{ animatorOptions: border }}
/>
<Button
style={{ ...styles.button, ...styles.button2 }}
title="Button2"
textStyle={styles.buttonTextStyle}
focusOptions={{ animatorOptions: border }}
/>
<Button
style={{ ...styles.button, ...styles.button3 }}
title="Button3"
textStyle={styles.buttonTextStyle}
focusOptions={{ animatorOptions: border }}
/>
<Button
style={{ ...styles.button, ...styles.button4 }}
title="Button4"
textStyle={styles.buttonTextStyle}
focusOptions={{ animatorOptions: border }}
/>
<Button
style={{ ...styles.button, ...styles.button5 }}
title="Button5"
textStyle={styles.buttonTextStyle}
focusOptions={{ animatorOptions: border, forbiddenFocusDirections: ['down'] }}
/>
</View>
<View>
{scrollViewData.map((_: any, index: number) => (
<Button
key={index}
style={{
...styles.button,
marginTop: Ratio(index === 0 ? 250 : 20),
height: Ratio(50),
}}
title={`Button${index + 1}`}
textStyle={styles.buttonTextStyle}
focusOptions={{ animatorOptions: border }}
/>
))}
</View>
<View style={{ flex: 1, marginTop: Ratio(240), left: Ratio(50) }}>
<FlashList
data={data}
renderItem={rowRenderer}
horizontal={false}
type="row"
estimatedItemSize={Ratio(150)}
style={{ flex: 1 }}
focusOptions={{ forbiddenFocusDirections: ['down'] }}
/>
</View>
</View>
</ScrollView>
</Screen>
);
};

const styles = StyleSheet.create({
button: {
marginHorizontal: getScaledValue(20),
borderWidth: getScaledValue(2),
borderRadius: getScaledValue(25),
borderColor: '#62DBFB',
height: getScaledValue(50),
width: Ratio(500),
marginTop: Ratio(100),
},
packshot: {
width: Ratio(150),
height: Ratio(150),
// borderColor: 'red',
// borderWidth: 1,
margin: 5,
// borderWidth: 2,
},
image: {
width: '100%',
height: '100%',
},
buttonTextStyle: {
color: '#ffffff',
fontSize: Ratio(20),
},
button1: {
marginTop: Ratio(250),
},
button2: {
marginTop: Ratio(250),
},
button3: {
marginTop: Ratio(250),
},
button4: {
marginTop: Ratio(250),
},
button5: {
marginTop: Ratio(250),
},
button6: {},
button7: {},
button8: {},
button9: {},
button10: {},
});

export default ScrollToTop;
7 changes: 7 additions & 0 deletions packages/app-harness/src/testsList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ import overflow from './screens/overflow';
import pressableAnimations from './screens/pressableAnimations';
import verticalScroll from './screens/verticalScroll';
import grid from './screens/grid';
import scrollToTop from './screens/scrollToTop';

const testsList = [
{
component: scrollToTop,
title: 'Scroll To Top',
route: 'ScrollToTop',
platform: ['androidtv', 'firetv', 'tvos'],
},
{
component: viewGroup,
title: 'View Group',
Expand Down
2 changes: 1 addition & 1 deletion packages/create/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@flexn/create",
"version": "0.21.2-alpha.10",
"version": "0.21.2-alpha.13",
"description": "Flexn Create SDK",
"main": "lib/index",
"types": "lib/index",
Expand Down
11 changes: 3 additions & 8 deletions packages/create/src/focusManager/layoutManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,17 @@ const measure = ({
scrollContentHeight: 0,
xCenter: pgX + Math.floor(width / 2),
yCenter: pgY + Math.floor(height / 2),
innerView: {
yMin: 0,
yMax: 0,
xMin: 0,
xMax: 0,
},
};

model.setLayout(layout);

// Order matters first recalculate layout then find lowest possible relative coordinates
recalculateLayout(model, remeasure);

if (model.getType() === TYPE_VIEW) {
findLowestRelativeCoordinates(model as View);
}

recalculateLayout(model, remeasure);

if (callback) callback();
if (resolve) resolve();
}
Expand Down
61 changes: 27 additions & 34 deletions packages/create/src/focusManager/service/scroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '../constants';
import ScrollView from '../model/scrollview';

const { width: screenWidth } = Dimensions.get('screen');
const { width: screenWidth, height: screenHeight } = Dimensions.get('screen');

class Scroller {
public calculateAndScrollToTarget(direction: string, contextParameters: any) {
Expand Down Expand Up @@ -63,57 +63,50 @@ class Scroller {
y: scrollView.getScrollOffsetY(),
};

const x = [
currentLayout.xMin - scrollView.getLayout().xMin - horizontalViewportOffset,
scrollView.getScrollOffsetX(),
];

const y = [
currentLayout.yMin - scrollView.getLayout().yMin - verticalViewportOffset,
scrollView.getScrollOffsetY(),
];

switch (true) {
case DIRECTION_RIGHT.includes(direction):
{
if (!scrollView.isHorizontal() && currentLayout.yMin < scrollView.getScrollOffsetY()) {
scrollTarget.y = currentLayout.yMin - verticalViewportOffset - scrollView.getLayout().yMin;
}

scrollTarget.x = Math.max(
currentLayout.xMin - scrollView.getLayout().xMin - horizontalViewportOffset,
scrollView.getScrollOffsetX()
);
const mathFunc = currentFocus.getLayout().absolute.yMax >= screenHeight ? Math.max : Math.min;
scrollTarget.x = Math.max(...x);
scrollTarget.y = mathFunc(...y);
}
break;
case DIRECTION_LEFT.includes(direction):
{
if (!scrollView.isHorizontal() && currentLayout.yMin < scrollView.getScrollOffsetY()) {
scrollTarget.y = currentLayout.yMin - verticalViewportOffset - scrollView.getLayout().yMin;
}

scrollTarget.x = Math.min(
currentLayout.xMin - scrollView.getLayout().xMin - horizontalViewportOffset,
scrollView.getScrollOffsetX()
);
const mathFunc = currentFocus.getLayout().absolute.yMax >= screenHeight ? Math.max : Math.min;
scrollTarget.x = Math.min(...x);
scrollTarget.y = mathFunc(...y);
}
break;
case DIRECTION_UP.includes(direction):
{
scrollTarget.y = Math.min(
currentLayout.yMin - verticalViewportOffset - scrollView.getLayout().yMin,
scrollView.getScrollOffsetY()
);

const mathFunc = currentFocus.getLayout().absolute.xMax >= screenWidth ? Math.max : Math.min;
scrollTarget.x = mathFunc(
currentLayout.xMin - scrollView.getLayout().xMin - horizontalViewportOffset,
scrollView.getScrollOffsetX()
// If element is on the top and there is no more elements above which is higher than verticalViewportOffset
// then we move whole viewport to the 0 position
const lowestFocusableYMin = currentFocus.getScreen()?.getPrecalculatedFocus()?.getLayout()?.yMin;

scrollTarget.y = Math.min(
y[0] < lowestFocusableYMin && currentLayout.yMax < screenHeight ? 0 : y[0],
y[1]
);
scrollTarget.x = mathFunc(...x);
}
break;
case DIRECTION_DOWN.includes(direction):
{
scrollTarget.y = Math.max(
currentLayout.yMin - verticalViewportOffset - scrollView.getLayout().yMin,
scrollView.getScrollOffsetY()
);

const mathFunc = currentFocus.getLayout().absolute.xMax >= screenWidth ? Math.max : Math.min;
scrollTarget.x = mathFunc(
currentLayout.xMin - scrollView.getLayout().xMin - horizontalViewportOffset,
scrollView.getScrollOffsetX()
);
scrollTarget.y = Math.max(...y);
scrollTarget.x = mathFunc(...x);
}
break;
default:
Expand Down
2 changes: 1 addition & 1 deletion packages/graybox/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@flexn/graybox",
"version": "0.21.2-alpha.10",
"version": "0.21.2-alpha.13",
"description": "Flexn Graybox Tester",
"main": "lib/index.js",
"scripts": {
Expand Down
Loading

0 comments on commit 900835a

Please sign in to comment.