Skip to content

Commit 2a22e9f

Browse files
authored
Merge pull request #1165 from StoDevX/dedupe-calendar
Deduplicate calendar time-finding logic
2 parents b6a8fcf + e4d5491 commit 2a22e9f

File tree

3 files changed

+65
-76
lines changed

3 files changed

+65
-76
lines changed

source/views/calendar/event-detail.js

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// @flow
22
import React from 'react'
33
import {Text, ScrollView, StyleSheet, Share} from 'react-native'
4-
import moment from 'moment-timezone'
54
import {fastGetTrimmedText} from '../../lib/html'
65
import {Cell, Section, TableView} from 'react-native-tableview-simple'
76
import type {EventType} from './types'
87
import {ShareButton} from '../components/nav-buttons'
8+
import {times} from './times'
99

1010
const styles = StyleSheet.create({
1111
chunk: {
@@ -14,73 +14,52 @@ const styles = StyleSheet.create({
1414
})
1515

1616
const shareItem = (event: EventType) => {
17-
Share.share({
18-
message: `${event.summary}: ${event.startTime.toString()}${event.endTime.toString()}`,
19-
})
17+
const message = `${event.summary}: ${event.startTime.toString()}${event.endTime.toString()}`
18+
Share.share({message})
2019
.then(result => console.log(result))
2120
.catch(error => console.log(error.message))
2221
}
2322

24-
function display(title: string, data: string) {
25-
return data.trim()
26-
? <Section header={title}>
23+
function MaybeSection({header, content}: {header: string, content: string}) {
24+
return content.trim()
25+
? <Section header={header}>
2726
<Cell
28-
cellContentView={
27+
title={
2928
<Text selectable={true} style={styles.chunk}>
30-
{data}
29+
{content}
3130
</Text>
3231
}
3332
/>
3433
</Section>
3534
: null
3635
}
3736

38-
function displayTimes(title: string, event: EventType) {
39-
const eventLength = moment
40-
.duration(event.endTime.diff(event.startTime))
41-
.asHours()
42-
43-
const allDay = eventLength === 24
44-
const multiDay = event.startTime.dayOfYear() !== event.endTime.dayOfYear()
45-
const sillyZeroLength = event.startTime.isSame(event.endTime, 'minute')
37+
function getTimes(event: EventType) {
38+
const {allDay, start, end} = times(event)
4639

4740
if (allDay) {
48-
return display(title, 'All-Day')
49-
}
50-
51-
let start, end
52-
if (event.isOngoing) {
53-
start = event.startTime.format('MMM. D')
54-
end = event.endTime.format('MMM. D')
55-
} else if (multiDay) {
56-
start = event.startTime.format('h:mm A')
57-
end = `${event.endTime.format('MMM. D h:mm A')}`
58-
} else if (sillyZeroLength) {
59-
start = event.startTime.format('h:mm A')
60-
end = '???'
61-
} else {
62-
start = event.startTime.format('h:mm A')
63-
end = event.endTime.format('h:mm A')
41+
return 'All-Day'
6442
}
6543

66-
return display(title, start + ' — ' + end)
44+
return `${start}${end}`
6745
}
6846

6947
export function EventDetail(props: {
7048
navigation: {state: {params: {event: EventType}}},
7149
}) {
7250
const {event} = props.navigation.state.params
73-
let title = fastGetTrimmedText(event.summary || '')
74-
let summary = fastGetTrimmedText(event.extra.data.description || '')
75-
let location = fastGetTrimmedText(event.location || '')
51+
const title = fastGetTrimmedText(event.summary || '')
52+
const summary = fastGetTrimmedText(event.extra.data.description || '')
53+
const location = fastGetTrimmedText(event.location || '')
54+
const times = getTimes(event)
7655

7756
return (
7857
<ScrollView>
7958
<TableView>
80-
{display('EVENT', title)}
81-
{displayTimes('TIME', event)}
82-
{display('LOCATION', location)}
83-
{display('DESCRIPTION', summary)}
59+
<MaybeSection header="EVENT" content={title} />
60+
<MaybeSection header="TIME" content={times} />
61+
<MaybeSection header="LOCATION" content={location} />
62+
<MaybeSection header="DESCRIPTION" content={summary} />
8463
</TableView>
8564
</ScrollView>
8665
)

source/views/calendar/event-row.js

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
import React from 'react'
33
import {StyleSheet, Text} from 'react-native'
44
import type {EventType} from './types'
5-
import moment from 'moment-timezone'
65
import * as c from '../components/colors'
76
import {Row, Column} from '../components/layout'
87
import {ListRow, Detail, Title} from '../components/list'
98
import {fastGetTrimmedText} from '../../lib/html'
109
import {Bar} from './vertical-bar'
10+
import {times} from './times'
1111

1212
const styles = StyleSheet.create({
1313
row: {
@@ -72,13 +72,7 @@ export default function EventRow({
7272
}
7373

7474
function CalendarTimes({event, style}: {event: EventType, style: any}) {
75-
const eventLength = moment
76-
.duration(event.endTime.diff(event.startTime))
77-
.asHours()
78-
79-
const allDay = eventLength === 24
80-
const multiDay = event.startTime.dayOfYear() !== event.endTime.dayOfYear()
81-
const sillyZeroLength = event.startTime.isSame(event.endTime, 'minute')
75+
const {allDay, start, end} = times(event)
8276

8377
if (allDay) {
8478
return (
@@ -88,33 +82,6 @@ function CalendarTimes({event, style}: {event: EventType, style: any}) {
8882
)
8983
}
9084

91-
let startTimeFormatted = event.startTime.format('h:mm A')
92-
let endTimeFormatted = event.endTime.format('h:mm A')
93-
let midnightTime = '12:00 AM'
94-
95-
let start, end
96-
if (event.isOngoing) {
97-
start = event.startTime.format('MMM. D')
98-
end = event.endTime.format('MMM. D')
99-
} else if (multiDay) {
100-
// 12:00 PM to Jun. 25 3:00pm
101-
// Midnight to Jun. 25 <-- assuming the end time is also midnight
102-
start = startTimeFormatted
103-
const endFormat = endTimeFormatted === midnightTime
104-
? 'MMM. D'
105-
: 'MMM. D h:mm A'
106-
end = `to ${event.endTime.format(endFormat)}`
107-
} else if (sillyZeroLength) {
108-
start = startTimeFormatted
109-
end = 'until ???'
110-
} else {
111-
start = startTimeFormatted
112-
end = endTimeFormatted
113-
}
114-
115-
start = start === midnightTime ? 'Midnight' : start
116-
end = end === midnightTime ? 'Midnight' : end
117-
11885
return (
11986
<Column style={style}>
12087
<Text style={[styles.time, styles.start]}>{start}</Text>

source/views/calendar/times.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// @flow
2+
3+
import moment from 'moment-timezone'
4+
import type {EventType} from './types'
5+
6+
export function times(event: EventType) {
7+
const eventLength = moment
8+
.duration(event.endTime.diff(event.startTime))
9+
.asHours()
10+
11+
const allDay = eventLength === 24
12+
const multiDay = event.startTime.dayOfYear() !== event.endTime.dayOfYear()
13+
const sillyZeroLength = event.startTime.isSame(event.endTime, 'minute')
14+
15+
let startTimeFormatted = event.startTime.format('h:mm A')
16+
let endTimeFormatted = event.endTime.format('h:mm A')
17+
let midnightTime = '12:00 AM'
18+
19+
let start, end
20+
if (event.isOngoing) {
21+
start = event.startTime.format('MMM. D')
22+
end = event.endTime.format('MMM. D')
23+
} else if (multiDay) {
24+
// 12:00 PM to Jun. 25 3:00pm
25+
// Midnight to Jun. 25 <-- assuming the end time is also midnight
26+
start = startTimeFormatted
27+
const endFormat = endTimeFormatted === midnightTime
28+
? 'MMM. D'
29+
: 'MMM. D h:mm A'
30+
end = `to ${event.endTime.format(endFormat)}`
31+
} else if (sillyZeroLength) {
32+
start = startTimeFormatted
33+
end = 'until ???'
34+
} else {
35+
start = startTimeFormatted
36+
end = endTimeFormatted
37+
}
38+
39+
start = start === midnightTime ? 'Midnight' : start
40+
end = end === midnightTime ? 'Midnight' : end
41+
42+
return {start, end, allDay}
43+
}

0 commit comments

Comments
 (0)