Skip to content
This repository has been archived by the owner on Jun 15, 2022. It is now read-only.

Commit

Permalink
feature: rewrite Root
Browse files Browse the repository at this point in the history
  • Loading branch information
radko93 committed Jun 18, 2020
1 parent 1536aa2 commit 977cf1b
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 85 deletions.
2 changes: 1 addition & 1 deletion src/lib/ApplicationState.ts
Expand Up @@ -329,7 +329,7 @@ export class ApplicationState {
return this.tabletMode;
}

private setTabletModeEnabled(enabled: boolean) {
setTabletModeEnabled(enabled: boolean) {
if (enabled !== this.tabletMode) {
this.tabletMode = enabled;
this.notifyEventObservers(
Expand Down
171 changes: 171 additions & 0 deletions src/screens/Root.tsx
@@ -0,0 +1,171 @@
import React, { useContext, useState, useEffect } from 'react';
import { LayoutChangeEvent } from 'react-native';
import styled from 'styled-components/native';
import { ApplicationContext } from '@Root/ApplicationContext';
import { Notes } from './Notes/Notes';
import {
AppStateEventType,
TabletModeChangeData,
NoteSideMenuToggleChange,
AppStateType,
} from '@Lib/ApplicationState';

const Container = styled.View`
flex: 1;
flex-direction: 'row';
`;
const NotesContainer = styled.View<{
shouldSplitLayout?: boolean;
notesListCollapsed?: boolean;
}>`
border-right-color: ${props =>
props.shouldSplitLayout ? 'black' : undefined};
border-right-width: ${props => (props.shouldSplitLayout ? 1 : undefined)}px;
width: ${props =>
props.shouldSplitLayout
? props.notesListCollapsed
? 0
: '40%'
: undefined};
flex: ${props => (props.shouldSplitLayout ? undefined : 1)};
`;
const ComposeContainer = styled.View<{ notesListCollapsed?: boolean }>`
width: ${props => (props.notesListCollapsed ? '100%' : '60%')};
`;

export const Root = (): JSX.Element => {
const application = useContext(ApplicationContext);
const [width, setWidth] = useState<number | undefined>(undefined);
const [height, setHeight] = useState<number | undefined>(undefined);
const [x, setX] = useState<number | undefined>(undefined);
const [y, setY] = useState<number | undefined>(undefined);
const [shouldSplitLayout, setShouldSplitLayout] = useState<
boolean | undefined
>(false);
const [keyboardHeight, setKeyboardHeight] = useState<number | undefined>(
undefined
);
const [notesListCollapsed, setNotesListCollapsed] = useState<
boolean | undefined
>();

/**
* Register observers
*/
useEffect(() => {
let removeApplicationStateEventHandler: (() => void) | undefined;
let removeStateObserver: (() => void) | undefined;
const startObservers = async () => {
removeStateObserver = application
?.getAppState()
.addStateChangeObserver(state => {
if (state === AppStateType.GainingFocus) {
application.sync();
}
});
removeApplicationStateEventHandler = application
?.getAppState()
.addStateEventObserver(
(
event: AppStateEventType,
data: TabletModeChangeData | NoteSideMenuToggleChange | undefined
) => {
if (event === AppStateEventType.AppStateEventNoteSideMenuToggle) {
// update state to toggle Notes side menu if we triggered the collapse
setNotesListCollapsed(
(data as NoteSideMenuToggleChange).new_isNoteSideMenuCollapsed
);
} else if (event === AppStateEventType.KeyboardChangeEvent) {
// need to refresh the height of the keyboard when it opens so that we can change the position
// of the sidebar collapse icon
if (application?.getAppState().isInTabletMode) {
setKeyboardHeight(
application?.getAppState().getKeyboardHeight()
);
}
}
}
);
};
startObservers();
return () => {
if (removeApplicationStateEventHandler) {
removeApplicationStateEventHandler();
}
if (removeStateObserver) {
removeStateObserver();
}
};
}, [application]);

const onLayout = (e: LayoutChangeEvent) => {
const tempWidth = e.nativeEvent.layout.width;
/**
If you're in tablet mode, but on an iPad where this app is running side by
side by another app, we only want to show the Compose window and not the
list, because there isn't enough space.
*/
const MinWidthToSplit = 450;
if (application?.getAppState().isTabletDevice) {
if (tempWidth < MinWidthToSplit) {
application?.getAppState().setTabletModeEnabled(false);
} else {
application?.getAppState().setTabletModeEnabled(true);
}
}
setWidth(tempWidth);
setHeight(e.nativeEvent.layout.height);
setX(e.nativeEvent.layout.x);
setShouldSplitLayout(application?.getAppState().isInTabletMode);
setNotesListCollapsed(application?.getAppState().isNoteSideMenuCollapsed);
setKeyboardHeight(application?.getAppState().getKeyboardHeight());
};
return (
<Container testID="rootView" onLayout={onLayout}>
<NotesContainer
shouldSplitLayout={shouldSplitLayout}
notesListCollapsed={notesListCollapsed}
>
<Notes
// onUnlockPress={this.onUnlockPress}
// onNoteSelect={
// shouldSplitLayout ? this.onNoteSelect : undefined /* tablet only */
// }
/>
</NotesContainer>

{shouldSplitLayout && (
<ComposeContainer notesListCollapsed={notesListCollapsed}>
{/* <Compose
ref={ref => {
this.composeRef = ref;
}}
selectedTagId={this.state.selectedTagId}
navigation={this.props.navigation}
/> */}
{/*
<TouchableHighlight
style={[
this.styles.toggleButtonContainer,
this.styles.toggleButton,
{ bottom: collapseIconBottomPosition },
]}
onPress={this.toggleNoteSideMenu}
>
<View>
<Icon
name={collapseIconName}
size={24}
color={hexToRGBA(
this.context!.getThemeService().variables.stylekitInfoColor,
0.85
)}
/>
</View>
</TouchableHighlight> */}
</ComposeContainer>
)}
</Container>
);
};
75 changes: 0 additions & 75 deletions src/screens2/Notes/NoteCell.tsx
Expand Up @@ -430,86 +430,11 @@ export default class NoteCell extends ThemedPureComponent<Props, State> {
// @ts-ignore ignore null return from hexToRGBA
const variables = this.context!.getThemeService().variables;
this.styles = StyleSheet.create({
noteCell: {
padding: padding,
paddingRight: padding * 2,
borderBottomColor: hexToRGBA(variables.stylekitBorderColor, 0.75),
borderBottomWidth: 1,
backgroundColor: variables.stylekitBackgroundColor,
},

noteCellSelected: {
backgroundColor: variables.stylekitInfoColor,
},

noteTagsContainer: {
flex: 1,
flexDirection: 'row',
marginTop: 7,
},

pinnedView: {
flex: 1,
flexDirection: 'row',
marginBottom: 5,
},

flagsContainer: {
flex: 1,
flexDirection: 'row',
marginBottom: 8,
},

noteTag: {
marginRight: 2,
fontSize: 12,
color: variables.stylekitForegroundColor,
opacity: 0.5,
},

noteTagSelected: {
color: variables.stylekitInfoContrastColor,
opacity: 0.8,
},

noteTitle: {
fontWeight: 'bold',
fontSize: 16,
color: variables.stylekitForegroundColor,
},

noteTitleSelected: {
color: variables.stylekitInfoContrastColor,
},

noteText: {
fontSize: 15,
marginTop: 4,
color: variables.stylekitForegroundColor,
opacity: 0.8,
lineHeight: 21,
},

noteTextSelected: {
color: variables.stylekitInfoContrastColor,
},

noteDate: {
marginTop: 5,
fontSize: 12,
color: variables.stylekitForegroundColor,
opacity: 0.5,
},

noteDateSelected: {
color: variables.stylekitInfoContrastColor,
opacity: 0.8,
},

deleting: {
color: variables.stylekitInfoColor,
marginBottom: 5,
},
});
}
}
10 changes: 1 addition & 9 deletions src/screens2/Root.tsx
Expand Up @@ -395,7 +395,7 @@ export default class Root extends Abstract<AbstractProps, State> {

render() {
/* Don't render LockedView here since we need this.notesRef as soon as we can (for componentWillFocus callback) */
console.log(this.context);

const { shouldSplitLayout, notesListCollapsed } = this.state;

const notesStyles = shouldSplitLayout
Expand Down Expand Up @@ -484,14 +484,6 @@ export default class Root extends Abstract<AbstractProps, State> {

loadStyles() {
this.styles = {
root: {
flex: 1,
flexDirection: 'row',
},
left: {
borderRightColor: 'black',
borderRightWidth: 1,
},
right: {},
toggleButtonContainer: {
backgroundColor: 'black',
Expand Down

0 comments on commit 977cf1b

Please sign in to comment.