Skip to content

Commit 8b3e9a7

Browse files
authored
Merge pull request #1807 from wix/infra/constants
Constants cleanup
2 parents 4e36975 + 32acbff commit 8b3e9a7

File tree

12 files changed

+62
-44
lines changed

12 files changed

+62
-44
lines changed

example/src/screens/menu.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, {Component} from 'react';
2-
import {Platform, StyleSheet, View, ScrollView, TouchableOpacity, Text, Image, I18nManager} from 'react-native';
2+
import {Platform, StyleSheet, View, ScrollView, TouchableOpacity, Text, Image, I18nManager, Switch} from 'react-native';
33
import {Navigation} from 'react-native-navigation';
44
import testIDs from '../testIDs';
55

@@ -11,6 +11,15 @@ interface Props {
1111
}
1212

1313
export default class MenuScreen extends Component<Props> {
14+
state = {
15+
forceRTL: false
16+
}
17+
18+
toggleRTL = (value) => {
19+
I18nManager.forceRTL(value);
20+
this.setState({forceRTL: value});
21+
}
22+
1423
renderEntry(testID: string, title: string, screen: string, options?: any) {
1524
return (
1625
<TouchableOpacity
@@ -35,6 +44,10 @@ export default class MenuScreen extends Component<Props> {
3544
{this.renderEntry(testIDs.menu.EXPANDABLE_CALENDAR, 'Expandable Calendar', 'ExpandableCalendar')}
3645
{this.renderEntry(testIDs.menu.TIMELINE_CALENDAR, 'Timeline Calendar', 'TimelineCalendar')}
3746
{this.renderEntry(testIDs.menu.WEEK_CALENDAR, 'Week Calendar', 'ExpandableCalendar', {weekView: true})}
47+
<View style={styles.switchContainer}>
48+
<Text>Force RTL</Text>
49+
<Switch value={this.state.forceRTL} onValueChange={this.toggleRTL}/>
50+
</View>
3851
</View>
3952
</ScrollView>
4053
);
@@ -89,5 +102,8 @@ const styles = StyleSheet.create({
89102
menuText: {
90103
fontSize: 18,
91104
color: '#2d4150'
105+
},
106+
switchContainer: {
107+
margin: 20
92108
}
93109
});

src/calendar-list/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import PropTypes from 'prop-types';
22
import XDate from 'xdate';
33

44
import React, {Component} from 'react';
5-
import {FlatList, Platform, Dimensions, View, ViewStyle, LayoutChangeEvent, FlatListProps} from 'react-native';
5+
import {FlatList, View, ViewStyle, LayoutChangeEvent, FlatListProps} from 'react-native';
66

77
import {extractComponentProps} from '../componentUpdater';
88
import {xdateToData, parseDate} from '../interface';
@@ -13,10 +13,10 @@ import styleConstructor from './style';
1313
import Calendar, {CalendarProps} from '../calendar';
1414
import CalendarListItem from './item';
1515
import CalendarHeader from '../calendar/header/index';
16+
import constants from '../commons/constants';
1617

1718

18-
const {width} = Dimensions.get('window');
19-
const CALENDAR_WIDTH = width;
19+
const CALENDAR_WIDTH = constants.screenWidth;
2020
const CALENDAR_HEIGHT = 360;
2121
const PAST_SCROLL_RANGE = 50;
2222
const FUTURE_SCROLL_RANGE = 50;
@@ -108,7 +108,7 @@ class CalendarList extends Component<CalendarListProps, State> {
108108
horizontal: false,
109109
scrollsToTop: false,
110110
scrollEnabled: true,
111-
removeClippedSubviews: Platform.OS === 'android',
111+
removeClippedSubviews: constants.isAndroid,
112112
keyExtractor: (_: any, index: number) => String(index)
113113
};
114114

src/calendar/day/basic/style.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import {StyleSheet, Platform} from 'react-native';
1+
import {StyleSheet} from 'react-native';
22
import * as defaultStyle from '../../../style';
33
import {Theme} from '../../../types';
4+
import constants from '../../../commons/constants';
45

56
export default function styleConstructor(theme: Theme = {}) {
67
const appStyle = {...defaultStyle, ...theme};
@@ -15,7 +16,7 @@ export default function styleConstructor(theme: Theme = {}) {
1516
alignItems: 'center'
1617
},
1718
text: {
18-
marginTop: Platform.OS === 'android' ? 4 : 6,
19+
marginTop: constants.isAndroid ? 4 : 6,
1920
fontSize: appStyle.textDayFontSize,
2021
fontFamily: appStyle.textDayFontFamily,
2122
fontWeight: appStyle.textDayFontWeight,
@@ -24,7 +25,7 @@ export default function styleConstructor(theme: Theme = {}) {
2425
...appStyle.textDayStyle
2526
},
2627
alignedText: {
27-
marginTop: Platform.OS === 'android' ? 4 : 6
28+
marginTop: constants.isAndroid ? 4 : 6
2829
},
2930
selected: {
3031
backgroundColor: appStyle.selectedDayBackgroundColor,

src/commons/constants.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
import {Dimensions} from 'react-native';
1+
import {Dimensions, I18nManager, Platform, PlatformIOSStatic} from 'react-native';
22

3-
const {width: screenWidth, height: screenHeight} = Dimensions.get('screen');
3+
4+
const {width: screenWidth, height: screenHeight} = Dimensions.get('window');
5+
const isRTL = I18nManager.isRTL;
6+
const isAndroid = Platform.OS === 'android';
7+
const isIOS = Platform.OS === 'ios';
8+
const screenAspectRatio = screenWidth < screenHeight ? screenHeight / screenWidth : screenWidth / screenHeight;
9+
const isTablet = (Platform as PlatformIOSStatic).isPad || (screenAspectRatio < 1.6 && Math.max(screenWidth, screenHeight) >= 900);
410

511
export default {
612
screenWidth,
7-
screenHeight
13+
screenHeight,
14+
isRTL,
15+
isAndroid,
16+
isIOS,
17+
isTablet
818
};

src/expandableCalendar/WeekCalendar/index.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ import asCalendarConsumer from '../asCalendarConsumer';
1515
import CalendarList, {CalendarListProps} from '../../calendar-list';
1616
import Week from '../week';
1717
import Presenter from './presenter';
18+
import constants from '../../commons/constants';
1819

19-
20-
const commons = require('../commons');
2120
const NUMBER_OF_PAGES = 2; // must be a positive number
22-
const applyAndroidRtlFix = commons.isAndroid && commons.isRTL;
21+
const applyAndroidRtlFix = constants.isAndroid && constants.isRTL;
2322

2423
export interface WeekCalendarProps extends CalendarListProps {
2524
/** whether to have shadow/elevation for the calendar */
@@ -75,7 +74,7 @@ class WeekCalendar extends Component<WeekCalendarProps, State> {
7574
}
7675

7776
get containerWidth() {
78-
return this.props.calendarWidth || commons.screenWidth;
77+
return this.props.calendarWidth || constants.screenWidth;
7978
}
8079

8180
getDatesArray() {

src/expandableCalendar/WeekCalendar/presenter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {FlatList} from 'react-native';
66
import {toMarkingFormat} from '../../interface';
77
import {DateData} from '../../types';
88
import {WeekCalendarProps} from './index';
9-
9+
import constants from '../../commons/constants';
1010

1111
const commons = require('../commons');
1212
const updateSources = commons.UpdateSources;
@@ -15,7 +15,7 @@ const NUMBER_OF_PAGES = 2;
1515

1616
class Presenter {
1717

18-
private _applyAndroidRtlFix = commons.isAndroid && commons.isRTL;
18+
private _applyAndroidRtlFix = constants.isAndroid && constants.isRTL;
1919
// On Android+RTL there's an initial scroll that cause issues
2020
private _firstAndroidRTLScrollIgnored = !this._applyAndroidRtlFix;
2121
public list: React.RefObject<FlatList> = React.createRef();

src/expandableCalendar/agendaList.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {getDefaultLocale} from '../services';
2727
import {Theme} from '../types';
2828
import styleConstructor from './style';
2929
import asCalendarConsumer from './asCalendarConsumer';
30-
30+
import constants from '../commons/constants';
3131
const commons = require('./commons');
3232
const updateSources = commons.UpdateSources;
3333

@@ -180,7 +180,7 @@ class AgendaList extends Component<AgendaListProps> {
180180
sectionIndex: sectionIndex,
181181
itemIndex: 0,
182182
viewPosition: 0, // position at the top
183-
viewOffset: (commons.isAndroid ? this.sectionHeight : 0) + viewOffset
183+
viewOffset: (constants.isAndroid ? this.sectionHeight : 0) + viewOffset
184184
});
185185
}
186186
}
@@ -271,7 +271,7 @@ class AgendaList extends Component<AgendaListProps> {
271271
}
272272

273273
// getItemLayout = (data, index) => {
274-
// return {length: commons.screenWidth, offset: commons.screenWidth * index, index};
274+
// return {length: constants.screenWidth, offset: constants.screenWidth * index, index};
275275
// }
276276
}
277277

src/expandableCalendar/commons.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
import {Platform, Dimensions, I18nManager, PlatformIOSStatic} from 'react-native';
2-
3-
const {height, width} = Dimensions.get('window');
4-
5-
export const isRTL = I18nManager.isRTL;
6-
export const isAndroid = Platform.OS === 'android';
7-
export const isIos = Platform.OS === 'ios';
8-
export const screenWidth = width;
9-
export const screenHeight = height;
10-
export const screenAspectRatio = screenWidth < screenHeight ? screenHeight / screenWidth : screenWidth / screenHeight;
11-
export const isTablet = (Platform as PlatformIOSStatic).isPad || (screenAspectRatio < 1.6 && Math.max(screenWidth, screenHeight) >= 900);
121
export const todayString = 'today';
132

143
export enum UpdateSources {

src/expandableCalendar/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import asCalendarConsumer from './asCalendarConsumer';
3333
import WeekCalendar from './WeekCalendar';
3434
import Week from './week';
3535

36+
import constants from '../commons/constants';
3637
const commons = require('./commons');
3738
const updateSources = commons.UpdateSources;
3839
enum Positions {
@@ -258,9 +259,9 @@ class ExpandableCalendar extends Component<ExpandableCalendarProps, State> {
258259
/** Utils */
259260
getOpenHeight() {
260261
if (!this.props.horizontal) {
261-
return Math.max(commons.screenHeight, commons.screenWidth);
262+
return Math.max(constants.screenHeight, constants.screenWidth);
262263
}
263-
return CLOSED_HEIGHT + (WEEK_HEIGHT * (this.numberOfWeeks - 1)) + (this.props.hideKnob ? 12 : KNOB_CONTAINER_HEIGHT) + (commons.isAndroid ? 3 : 0);
264+
return CLOSED_HEIGHT + (WEEK_HEIGHT * (this.numberOfWeeks - 1)) + (this.props.hideKnob ? 12 : KNOB_CONTAINER_HEIGHT) + (constants.isAndroid ? 3 : 0);
264265
}
265266

266267
getYear(date: XDate) {

src/expandableCalendar/style.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {StyleSheet, Platform} from 'react-native';
22
import * as defaultStyle from '../style';
33
import {Theme} from '../types';
4+
import constants from '../commons/constants';
45

56

6-
const commons = require('./commons');
77
export const HEADER_HEIGHT = 68;
88
export const KNOB_CONTAINER_HEIGHT = 24;
99

@@ -96,7 +96,7 @@ export default function styleConstructor(theme: Theme = {}) {
9696
position: 'absolute',
9797
left: 0,
9898
right: 0,
99-
top: HEADER_HEIGHT + (commons.isAndroid ? 8 : 4), // align row on top of calendar's first row
99+
top: HEADER_HEIGHT + (constants.isAndroid ? 8 : 4), // align row on top of calendar's first row
100100
},
101101
hidden: {
102102
opacity: 0
@@ -133,7 +133,7 @@ export default function styleConstructor(theme: Theme = {}) {
133133
},
134134
arrowImage: {
135135
tintColor: appStyle.arrowColor,
136-
transform: commons.isRTL ? [{scaleX: -1}] : undefined
136+
transform: constants.isRTL ? [{scaleX: -1}] : undefined
137137
},
138138
todayButtonContainer: {
139139
alignItems: appStyle.todayButtonPosition === 'right' ? 'flex-end' : 'flex-start',
@@ -143,9 +143,9 @@ export default function styleConstructor(theme: Theme = {}) {
143143
bottom : 0
144144
},
145145
todayButton: {
146-
height: commons.isTablet ? 40 : 28,
147-
paddingHorizontal: commons.isTablet ? 20 : 12,
148-
borderRadius: commons.isTablet ? 20 : 14,
146+
height: constants.isTablet ? 40 : 28,
147+
paddingHorizontal: constants.isTablet ? 20 : 12,
148+
borderRadius: constants.isTablet ? 20 : 14,
149149
flexDirection: appStyle.todayButtonPosition === 'right' ? 'row-reverse' : 'row',
150150
justifyContent: 'center',
151151
alignItems: 'center',
@@ -164,7 +164,7 @@ export default function styleConstructor(theme: Theme = {}) {
164164
},
165165
todayButtonText: {
166166
color: appStyle.todayButtonTextColor,
167-
fontSize: commons.isTablet ? appStyle.todayButtonFontSize + 2 : appStyle.todayButtonFontSize,
167+
fontSize: constants.isTablet ? appStyle.todayButtonFontSize + 2 : appStyle.todayButtonFontSize,
168168
fontWeight: appStyle.todayButtonFontWeight,
169169
fontFamily: appStyle.todayButtonFontFamily
170170
},

0 commit comments

Comments
 (0)