1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00
joplin/packages/app-mobile/components/NoteItem.tsx

169 lines
5.0 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import { PureComponent } from 'react';
import { connect } from 'react-redux';
import { Text, TouchableOpacity, View, StyleSheet, TextStyle, ViewStyle } from 'react-native';
import Checkbox from './Checkbox';
import Note from '@joplin/lib/models/Note';
import time from '@joplin/lib/time';
import { themeStyle } from './global-style';
import { _ } from '@joplin/lib/locale';
import { AppState } from '../utils/types';
import { Dispatch } from 'redux';
import { NoteEntity } from '@joplin/lib/services/database/types';
interface Props {
dispatch: Dispatch;
themeId: number;
note: NoteEntity;
noteSelectionEnabled: boolean;
selectedNoteIds: string[];
}
2017-08-01 19:59:01 +02:00
interface State {}
type Styles = Record<string, TextStyle|ViewStyle>;
class NoteItemComponent extends PureComponent<Props, State> {
private styles_: Record<string, Styles> = {};
public constructor(props: Props) {
super(props);
2017-07-23 00:52:24 +02:00
}
private styles() {
2020-09-15 15:01:07 +02:00
const theme = themeStyle(this.props.themeId);
if (this.styles_[this.props.themeId]) return this.styles_[this.props.themeId];
2017-08-01 19:59:01 +02:00
this.styles_ = {};
const styles: Record<string, TextStyle|ViewStyle> = {
2017-08-01 19:59:01 +02:00
listItem: {
flexDirection: 'row',
2019-10-09 21:35:13 +02:00
// height: 40,
2017-08-01 19:59:01 +02:00
borderBottomWidth: 1,
borderBottomColor: theme.dividerColor,
alignItems: 'flex-start',
2017-08-01 19:59:01 +02:00
paddingLeft: theme.marginLeft,
paddingRight: theme.marginRight,
paddingTop: theme.itemMarginTop,
paddingBottom: theme.itemMarginBottom,
2019-10-09 21:35:13 +02:00
// backgroundColor: theme.backgroundColor,
2017-08-01 19:59:01 +02:00
},
listItemText: {
flex: 1,
color: theme.color,
fontSize: theme.fontSize,
},
selectionWrapper: {
backgroundColor: theme.backgroundColor,
},
checkboxStyle: {
color: theme.color,
paddingRight: 10,
paddingTop: theme.itemMarginTop,
paddingBottom: theme.itemMarginBottom,
paddingLeft: theme.marginLeft,
},
checkedOpacityStyle: {
opacity: 0.4,
},
uncheckedOpacityStyle: { },
2017-08-01 19:59:01 +02:00
};
styles.listItemWithCheckbox = { ...styles.listItem };
2017-08-19 23:59:08 +02:00
delete styles.listItemWithCheckbox.paddingTop;
delete styles.listItemWithCheckbox.paddingBottom;
delete styles.listItemWithCheckbox.paddingLeft;
styles.listItemTextWithCheckbox = { ...styles.listItemText };
styles.listItemTextWithCheckbox.marginTop = theme.itemMarginTop - 1;
2017-08-19 23:59:08 +02:00
styles.listItemTextWithCheckbox.marginBottom = styles.listItem.paddingBottom;
2017-08-01 19:59:01 +02:00
styles.selectionWrapperSelected = { ...styles.selectionWrapper };
styles.selectionWrapperSelected.backgroundColor = theme.selectedColor;
2020-09-15 15:01:07 +02:00
this.styles_[this.props.themeId] = StyleSheet.create(styles);
return this.styles_[this.props.themeId];
2017-08-01 19:59:01 +02:00
}
private todoCheckbox_change = async (checked: boolean) => {
2017-07-25 20:36:52 +02:00
if (!this.props.note) return;
const newNote = {
id: this.props.note.id,
todo_completed: checked ? time.unixMs() : 0,
2019-07-29 15:43:53 +02:00
};
2017-07-25 20:36:52 +02:00
await Note.save(newNote);
this.props.dispatch({ type: 'NOTE_SORT' });
};
2017-07-25 20:36:52 +02:00
private onPress = () => {
2017-07-25 20:36:52 +02:00
if (!this.props.note) return;
2019-07-29 15:43:53 +02:00
if (this.props.note.encryption_applied) return;
2017-07-25 20:36:52 +02:00
if (this.props.noteSelectionEnabled) {
this.props.dispatch({
type: 'NOTE_SELECTION_TOGGLE',
id: this.props.note.id,
});
} else {
this.props.dispatch({
type: 'NAV_GO',
routeName: 'Note',
noteId: this.props.note.id,
});
}
};
private onLongPress = () => {
if (!this.props.note) return;
2017-07-25 20:36:52 +02:00
this.props.dispatch({
type: this.props.noteSelectionEnabled ? 'NOTE_SELECTION_TOGGLE' : 'NOTE_SELECTION_START',
id: this.props.note.id,
2017-07-25 20:36:52 +02:00
});
};
2017-07-25 20:36:52 +02:00
public render() {
2017-07-23 00:52:24 +02:00
const note = this.props.note ? this.props.note : {};
2017-08-19 23:59:08 +02:00
const isTodo = !!Number(note.is_todo);
2017-07-23 00:52:24 +02:00
const checkboxChecked = !!Number(note.todo_completed);
const checkboxStyle = this.styles().checkboxStyle;
2017-08-19 23:59:08 +02:00
const listItemStyle = isTodo ? this.styles().listItemWithCheckbox : this.styles().listItem;
const listItemTextStyle = isTodo ? this.styles().listItemTextWithCheckbox : this.styles().listItemText;
const opacityStyle = isTodo && checkboxChecked ? this.styles().checkedOpacityStyle : this.styles().uncheckedOpacityStyle;
const isSelected = this.props.noteSelectionEnabled && this.props.selectedNoteIds.indexOf(note.id) >= 0;
const selectionWrapperStyle = isSelected ? this.styles().selectionWrapperSelected : this.styles().selectionWrapper;
2017-07-23 20:26:50 +02:00
const noteTitle = Note.displayTitle(note);
2017-07-23 00:52:24 +02:00
return (
<TouchableOpacity onPress={this.onPress} onLongPress={this.onLongPress} activeOpacity={0.5}>
2019-07-29 15:43:53 +02:00
<View style={selectionWrapperStyle}>
<View style={opacityStyle}>
<View style={listItemStyle}>
{isTodo ? <Checkbox
style={checkboxStyle}
checked={checkboxChecked}
onChange={this.todoCheckbox_change}
accessibilityLabel={_('to-do: %s', noteTitle)}
/> : null }
<Text style={listItemTextStyle}>{noteTitle}</Text>
</View>
</View>
2017-07-23 00:52:24 +02:00
</View>
</TouchableOpacity>
2017-07-23 00:52:24 +02:00
);
}
}
export default connect((state: AppState) => {
2019-07-29 15:43:53 +02:00
return {
2020-09-15 15:01:07 +02:00
themeId: state.settings.theme,
2019-07-29 15:43:53 +02:00
noteSelectionEnabled: state.noteSelectionEnabled,
selectedNoteIds: state.selectedNoteIds,
};
})(NoteItemComponent);
2017-07-23 00:52:24 +02:00