Skip to content

Add recent screens chips to our demo app for easy access #3261

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

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
53 changes: 49 additions & 4 deletions demo/src/screens/MainScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {Navigation} from 'react-native-navigation';
import {
Assets,
Colors,
Typography,
Spacings,
View,
Text,
Expand All @@ -19,7 +20,7 @@ import {
Dividers
} from 'react-native-ui-lib'; //eslint-disable-line
import {navigationData} from './MenuStructure';
import Storage, {DEFAULT_SCREEN} from '../storage';
import Storage, {DEFAULT_SCREEN, RECENT_SCREENS} from '../storage';

const settingsIcon = require('../assets/icons/settings.png');
const chevronIcon = require('../assets/icons/chevronRight.png');
Expand Down Expand Up @@ -52,12 +53,15 @@ class MainScreen extends Component {
constructor(props) {
super(props);

const recentScreens = Storage.getString(RECENT_SCREENS);

const data = props.navigationData || navigationData;
this.state = {
currentPage: 0,
filteredNavigationData: data,
chipsLabels: _.map(data, section => section.title),
sectionsData: _.map(data, section => ({title: section.title, data: section.screens})),
recentScreens: recentScreens ? JSON.parse(recentScreens) : [],
selectedSection: 0,
faderStart: false,
faderEnd: true
Expand Down Expand Up @@ -155,8 +159,21 @@ class MainScreen extends Component {
this.openScreen({customValue: item});
};

updateRecentScreens(screen) {
const {recentScreens} = this.state;
recentScreens.unshift(screen);
const uniqueArr = [...new Set(recentScreens.map(item => JSON.stringify(item)))].map(item => JSON.parse(item));

Storage.set(RECENT_SCREENS, JSON.stringify(uniqueArr));

this.setState({
recentScreens: uniqueArr
});
}

openScreen = ({customValue: row}) => {
this.closeSearchBox();
this.updateRecentScreens(row);

setTimeout(() => {
this.pushScreen(row);
Expand Down Expand Up @@ -303,6 +320,34 @@ class MainScreen extends Component {
return <SectionHeader section={section}/>;
};

renderRecentScreens = () => {
const {recentScreens} = this.state;

if (recentScreens.length > 0) {
return (
<View row paddingV-s2 paddingH-s5 centerV>
<Text text90BO marginR-s2>
Recent:
</Text>
<ScrollView horizontal showsHorizontalScrollIndicator={false}>
{recentScreens.map(screen => {
return (
<Chip
marginR-s2
label={screen.title}
key={screen.title}
onPress={this.openScreen}
customValue={screen}
labelStyle={Typography.text100BO}
/>
);
})}
</ScrollView>
</View>
);
}
};

renderItem = ({item}) => {
const {renderItem} = this.props;

Expand Down Expand Up @@ -337,12 +382,10 @@ class MainScreen extends Component {

render() {
const {containerStyle} = this.props;
const {filteredNavigationData, filterText} = this.state;
const {filteredNavigationData, filterText, chipsLabels, sectionsData} = this.state;
const showNoResults = _.isEmpty(filteredNavigationData) && !!filterText;
const showResults = !_.isEmpty(filteredNavigationData) && !!filterText;
const showSectionList = !filterText;
const chipsLabels = this.state.chipsLabels;
const sectionsData = this.state.sectionsData;

return (
<View testID="demo_main_screen" flex style={containerStyle} useSafeArea>
Expand All @@ -366,6 +409,8 @@ class MainScreen extends Component {
</View>
)}

{this.renderRecentScreens()}

{showSectionList && (
<SectionList
sections={sectionsData}
Expand Down
17 changes: 15 additions & 2 deletions demo/src/screens/SettingsScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, {Component} from 'react';
import {StyleSheet, I18nManager} from 'react-native';
import {Colors, View, Text, Picker, Incubator, Switch} from 'react-native-ui-lib'; //eslint-disable-line
import {navigationData} from './MenuStructure';
import Storage, {DEFAULT_SCREEN, IS_RTL} from '../storage';
import Storage, {DEFAULT_SCREEN, IS_RTL, AUTO_SET_DEFAULT} from '../storage';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a little weird that there's a configuration that's not working; I understand it's for private, but still...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed, I will add it only in private
I also clean up a little the settings screen


const none = {label: '[None]', value: ''};

Expand All @@ -28,6 +28,7 @@ class SettingsScreen extends Component {

this.state = {
showRefreshMessage: false,
autoSetDefault: Storage.getBoolean(AUTO_SET_DEFAULT) ?? true,
isRTL: false,
screens
};
Expand Down Expand Up @@ -61,8 +62,15 @@ class SettingsScreen extends Component {
}, 1000);
};

setAutoSetDefault = value => {
Storage.set(AUTO_SET_DEFAULT, value);
this.setState({
autoSetDefault: value
});
};

render() {
const {defaultScreen, showRefreshMessage, isRTL, screens} = this.state;
const {defaultScreen, showRefreshMessage, autoSetDefault, isRTL, screens} = this.state;
const {extraSettingsUI} = this.props;
const filteredScreens = _.filter(screens, screen => !_.isUndefined(screen.value));

Expand All @@ -83,6 +91,11 @@ class SettingsScreen extends Component {
items={filteredScreens}
/>

<View row spread>
<Switch value={autoSetDefault} onValueChange={this.setAutoSetDefault}/>
<Text>Auto set default screen</Text>
</View>

<View style={{borderWidth: 1, borderColor: Colors.grey70, marginTop: 40}}>
<View style={[{padding: 5, borderBottomWidth: 1}, styles.block]}>
<Text text80 grey20>
Expand Down
3 changes: 3 additions & 0 deletions demo/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ const Storage = new MMKV();

export const DEFAULT_SCREEN = 'uilib.defaultScreen';
export const IS_RTL = 'uilib.isRTL';
export const RECENT_SCREENS = 'uilib.recentScreens';
export const AUTO_SET_DEFAULT = 'uilib.autoSetDefaultScreen';


export default Storage;