2024-09-21 13:57:26 +02:00
|
|
|
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
|
|
|
|
2024-09-21 13:57:26 +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
|
|
|
}
|
|
|
|
|
2024-09-21 13:57:26 +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_ = {};
|
|
|
|
|
2024-09-21 13:57:26 +02:00
|
|
|
const styles: Record<string, TextStyle|ViewStyle> = {
|
2017-08-01 19:59:01 +02:00
|
|
|
listItem: {
|
2018-03-09 22:59:12 +02:00
|
|
|
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,
|
2018-03-09 22:59:12 +02:00
|
|
|
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,
|
|
|
|
},
|
2017-11-23 20:47:51 +02:00
|
|
|
selectionWrapper: {
|
|
|
|
backgroundColor: theme.backgroundColor,
|
|
|
|
},
|
2024-09-21 13:57:26 +02:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2023-06-01 13:02:36 +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;
|
|
|
|
|
2023-06-01 13:02:36 +02:00
|
|
|
styles.listItemTextWithCheckbox = { ...styles.listItemText };
|
2024-09-21 13:57:26 +02:00
|
|
|
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
|
|
|
|
2023-06-01 13:02:36 +02:00
|
|
|
styles.selectionWrapperSelected = { ...styles.selectionWrapper };
|
2017-11-23 20:47:51 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-09-21 13:57:26 +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);
|
2024-02-20 01:09:34 +02:00
|
|
|
|
|
|
|
this.props.dispatch({ type: 'NOTE_SORT' });
|
2024-09-21 13:57:26 +02:00
|
|
|
};
|
2017-07-25 20:36:52 +02:00
|
|
|
|
2024-09-21 13:57:26 +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
|
|
|
|
2017-11-23 20:47:51 +02:00
|
|
|
if (this.props.noteSelectionEnabled) {
|
|
|
|
this.props.dispatch({
|
2018-03-09 22:59:12 +02:00
|
|
|
type: 'NOTE_SELECTION_TOGGLE',
|
2017-11-23 20:47:51 +02:00
|
|
|
id: this.props.note.id,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.props.dispatch({
|
2018-03-09 22:59:12 +02:00
|
|
|
type: 'NAV_GO',
|
|
|
|
routeName: 'Note',
|
2017-11-23 20:47:51 +02:00
|
|
|
noteId: this.props.note.id,
|
|
|
|
});
|
|
|
|
}
|
2024-09-21 13:57:26 +02:00
|
|
|
};
|
2017-11-23 20:47:51 +02:00
|
|
|
|
2024-09-21 13:57:26 +02:00
|
|
|
private onLongPress = () => {
|
2017-11-23 20:47:51 +02:00
|
|
|
if (!this.props.note) return;
|
|
|
|
|
2017-07-25 20:36:52 +02:00
|
|
|
this.props.dispatch({
|
2018-03-09 22:59:12 +02:00
|
|
|
type: this.props.noteSelectionEnabled ? 'NOTE_SELECTION_TOGGLE' : 'NOTE_SELECTION_START',
|
2017-11-23 20:47:51 +02:00
|
|
|
id: this.props.note.id,
|
2017-07-25 20:36:52 +02:00
|
|
|
});
|
2024-09-21 13:57:26 +02:00
|
|
|
};
|
2017-07-25 20:36:52 +02:00
|
|
|
|
2024-09-21 13:57:26 +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);
|
|
|
|
|
2024-09-21 13:57:26 +02:00
|
|
|
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;
|
2024-09-21 13:57:26 +02:00
|
|
|
const opacityStyle = isTodo && checkboxChecked ? this.styles().checkedOpacityStyle : this.styles().uncheckedOpacityStyle;
|
2017-11-23 20:47:51 +02:00
|
|
|
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
|
|
|
|
2022-06-26 19:23:41 +02:00
|
|
|
const noteTitle = Note.displayTitle(note);
|
|
|
|
|
2017-07-23 00:52:24 +02:00
|
|
|
return (
|
2024-11-20 13:35:35 +02:00
|
|
|
<TouchableOpacity
|
|
|
|
onPress={this.onPress}
|
|
|
|
onLongPress={this.onLongPress}
|
|
|
|
activeOpacity={0.5}
|
|
|
|
accessibilityHint={_('Opens note')}
|
|
|
|
accessibilityRole='button'
|
|
|
|
>
|
2019-07-29 15:43:53 +02:00
|
|
|
<View style={selectionWrapperStyle}>
|
|
|
|
<View style={opacityStyle}>
|
|
|
|
<View style={listItemStyle}>
|
2024-09-21 13:57:26 +02:00
|
|
|
{isTodo ? <Checkbox
|
2022-06-26 19:23:41 +02:00
|
|
|
style={checkboxStyle}
|
|
|
|
checked={checkboxChecked}
|
2024-09-21 13:57:26 +02:00
|
|
|
onChange={this.todoCheckbox_change}
|
2022-06-26 19:23:41 +02:00
|
|
|
accessibilityLabel={_('to-do: %s', noteTitle)}
|
2024-09-21 13:57:26 +02:00
|
|
|
/> : null }
|
2022-06-26 19:23:41 +02:00
|
|
|
<Text style={listItemTextStyle}>{noteTitle}</Text>
|
2017-11-23 20:41:35 +02:00
|
|
|
</View>
|
2017-11-23 20:47:51 +02:00
|
|
|
</View>
|
2017-07-23 00:52:24 +02:00
|
|
|
</View>
|
2017-11-23 20:47:51 +02:00
|
|
|
</TouchableOpacity>
|
2017-07-23 00:52:24 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-21 13:57:26 +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
|
|
|
|