Skip to content
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

Added Notifications with repeating Schedules Solves #55 #57

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c390c1f
Added Notification Schedular
SalikSayyed Aug 24, 2021
cfc832e
Added Notification Schedular
SalikSayyed Aug 24, 2021
37deef3
Check correction
SalikSayyed Aug 24, 2021
26767ed
Notification Cleaner
SalikSayyed Aug 24, 2021
7939844
Time Show formated
SalikSayyed Aug 24, 2021
124fb34
Swiped Buttons
SalikSayyed Aug 24, 2021
5a5d5ec
Set Notification Panel Toggle added
SalikSayyed Aug 24, 2021
e271c6d
Update screens/Settings/index.tsx
SalikSayyed Aug 24, 2021
2852ffd
Update screens/Settings/index.tsx
SalikSayyed Aug 24, 2021
ab7e359
Update screens/Settings/index.tsx
SalikSayyed Aug 24, 2021
b6603ac
Theme Color, Non-repetative Code, Dialog Box Added
SalikSayyed Aug 24, 2021
8d75552
Theme Color, Non-repetative Code, Dialog Box Added
SalikSayyed Aug 24, 2021
0a725a9
Modified Permission for IOS and Android
SalikSayyed Aug 25, 2021
0856c2c
Added Notifier on HomeScreen
SalikSayyed Aug 27, 2021
7a85a1f
Update screens/Settings/notificationSetter.tsx
SalikSayyed Sep 2, 2021
90a3e7b
Update screens/Settings/notificationSetter.tsx
SalikSayyed Sep 2, 2021
850cc4a
Removed Toast using SnackBar, removed logs and camelCase Correction
SalikSayyed Sep 2, 2021
893e663
merge conflict resolved
SalikSayyed Sep 22, 2021
12ac4b9
changes
SalikSayyed Sep 22, 2021
f373575
clear data clearning set notification
SalikSayyed Sep 22, 2021
3716f9a
notificationSetter updated UI for IOS
SalikSayyed Sep 22, 2021
f97c075
Auto Close UI
SalikSayyed Sep 22, 2021
f5fc7a4
space between days
SalikSayyed Sep 22, 2021
8413da3
setShow correction
SalikSayyed Sep 22, 2021
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ web-build/
.eslintcache

# other files
NOTES.md
NOTES.md
yarn-error.log
1 change: 1 addition & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"foregroundImage": "./assets/images/adaptive-icon.png",
"backgroundColor": "#ffffff"
},
"useNextNotificationsApi": true,
"package": "com.heylinda.meditation"
},
"web": {
Expand Down
23 changes: 23 additions & 0 deletions components/ToastSnackBar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react'
import { Snackbar } from 'react-native-paper'
import { Text, useThemeColor } from '../components/Themed'

interface Props {
message: string
show: boolean
changeShow: React.Dispatch<React.SetStateAction<boolean>>
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: call this setShow

}

export default function ToastBar({ message, show, changeShow }: Props) {
const onDismissSnackBar = () => changeShow(false)
return (
<Snackbar
visible={show}
duration={2000}
onDismiss={onDismissSnackBar}
wrapperStyle={{ backgroundColor: useThemeColor({}, 'black') }}
>
<Text style={{ color: useThemeColor({}, 'white') }}>{message}</Text>
</Snackbar>
)
}
56 changes: 56 additions & 0 deletions components/WeekdayPicker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React from 'react'
import { StyleSheet, Text, View, TouchableOpacity } from 'react-native'
import { useThemeColor } from '../components/Themed'

interface Props {
weekdays: number[]
setWeekdays: React.Dispatch<React.SetStateAction<number[]>>
}
export default function DayPicker({ weekdays, setWeekdays }: Props) {
function daysIO(v: number) {
if (weekdays.includes(v)) {
const weekdayRemoved = weekdays.filter((element) => element !== v)
setWeekdays(weekdayRemoved)
} else {
setWeekdays([...weekdays, v])
}
}

const activeColor = useThemeColor({}, 'primary')
const inactiveColor = useThemeColor({}, 'gray800')
const whiteColor = useThemeColor({}, 'white')
const days = ['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT']
return (
<View style={[styles.boxContainer]}>
{days.map((value, index) => (
<TouchableOpacity key={value} onPress={() => daysIO(index + 1)}>
<View
style={[
styles.box,
{ backgroundColor: weekdays.includes(index + 1) ? activeColor : inactiveColor },
]}
>
<Text style={{ color: whiteColor }}>{value}</Text>
</View>
</TouchableOpacity>
))}
</View>
)
}

const styles = StyleSheet.create({
box: {
width: 40,
height: 40,
borderRadius: 20,
justifyContent: 'center',
alignItems: 'center',
marginVertical: 30,
},
boxContainer: {
flexDirection: 'row',
justifyContent: 'space-evenly',
alignItems: 'center',
marginTop: 20,
},
})
70 changes: 70 additions & 0 deletions notifications/notificationHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import * as Notifications from 'expo-notifications'
import { Platform } from 'react-native'

const askPermissions = async () => {
Notifications.getPermissionsAsync().then((status) => {
if (!status.granted) {
Notifications.requestPermissionsAsync()
}
})
}

const deleteNotifications = async () => {
Notifications.cancelAllScheduledNotificationsAsync()
}

const iosScheduler = (weekday: number[], time: Date) => {
if (weekday !== [-1]) {
Notifications.cancelAllScheduledNotificationsAsync()
weekday.forEach((v) => {
if (v !== -1) {
Notifications.scheduleNotificationAsync({
content: {
title: 'Practice your meditation today 🙇‍♂️',
},
trigger: {
hour: time.getHours(),
minute: time.getMinutes(),
weekday: v,
},
})
}
})
}
}

const androidSchedular = (weekday: number[], time: Date) => {
Notifications.cancelAllScheduledNotificationsAsync()
if (weekday !== [-1]) {
weekday.forEach((v) => {
if (v !== -1) {
Notifications.scheduleNotificationAsync({
content: {
title: 'Practice your meditation today 🙇‍♂️',
},
trigger: {
hour: time.getHours(),
minute: time.getMinutes(),
weekday: v,
repeats: true,
},
})
}
})
}
}

const platformScheduler = (weekday: number[], time: Date) => {
if (Platform.OS === 'android') {
androidSchedular(weekday, time)
}
if (Platform.OS === 'ios') {
iosScheduler(weekday, time)
}
}

export default {
askPermission: askPermissions,
testSchedular: platformScheduler,
deleteNotification: deleteNotifications,
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"dependencies": {
"@expo/vector-icons": "^12.0.0",
"@react-native-async-storage/async-storage": "~1.15.0",
"@react-native-community/datetimepicker": "^3.5.2",
"@react-native-community/masked-view": "0.1.10",
"@react-navigation/bottom-tabs": "5.11.2",
"@react-navigation/native": "~5.8.10",
Expand All @@ -32,6 +33,7 @@
"expo-file-system": "~11.1.3",
"expo-font": "~9.2.1",
"expo-linking": "~2.3.1",
"expo-notifications": "^0.12.3",
"expo-splash-screen": "~0.11.2",
"expo-status-bar": "~1.0.4",
"expo-web-browser": "~9.2.0",
Expand Down
3 changes: 2 additions & 1 deletion screens/Home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import { Text, useThemeColor } from '../../components/Themed'
import Colors from '../../constants/Colors'
import { meditations, MeditationItem } from '../../data/meditations'
import { HomeParamList } from '../../types'
import Notify from '../../notifications/notificationHandler'

interface Props {
navigation: StackNavigationProp<HomeParamList, 'HomeScreen'>
}

export default function Home({ navigation }: Props) {
Notify.askPermission()
const textColor = useThemeColor({}, 'text')

const renderPopularCard = ({ item }: MeditationItem) => {
return (
<Card
Expand Down
40 changes: 39 additions & 1 deletion screens/Settings/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import * as React from 'react'
import { Alert } from 'react-native'
import { Divider, List } from 'react-native-paper'
import { Divider, List, Dialog, Button } from 'react-native-paper'
import { useAppDispatch } from '../../hooks'
import { reset } from '../../redux/meditationSlice'

import Toast from '../../components/ToastSnackBar'
import NotificationSetter from './notificationSetter'
import Notify from '../../notifications/notificationHandler'

const Settings = () => {
const dispatch = useAppDispatch()
const [showNotification, toggleShowNotification] = React.useState(false)
const [toastShow, setToastShow] = React.useState(false)
const [toastMessage, setToastMessage] = React.useState('')
const clearData = () => {
Alert.alert(
'Clear Data',
Expand All @@ -22,10 +29,41 @@ const Settings = () => {
]
)
}

return (
<>
<List.Item title="Clear Data" onPress={clearData} />
<Divider />
<List.Item
title="Set Daily Reminder"
onPress={() => toggleShowNotification(!showNotification)}
/>
<Dialog
visible={showNotification}
onDismiss={() => {
toggleShowNotification(!showNotification)
}}
dismissable={true}
>
<Dialog.Title>Set Reminders</Dialog.Title>
<Dialog.Content>
<NotificationSetter setToastMessage={setToastMessage} setToastShow={setToastShow} />
</Dialog.Content>
<Dialog.Actions>
<Button onPress={() => toggleShowNotification(!showNotification)}>CANCEL</Button>
</Dialog.Actions>
</Dialog>
<Divider />
<List.Item
title="Clear All Reminders"
onPress={() => {
Notify.deleteNotification()
setToastMessage('All Reminders Cleared')
setToastShow(true)
}}
/>
<Divider />
<Toast message={toastMessage} show={toastShow} changeShow={setToastShow} />
</>
)
}
Expand Down
106 changes: 106 additions & 0 deletions screens/Settings/notificationSetter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import * as React from 'react'
import { Platform, StyleSheet, View, TouchableOpacity, Text } from 'react-native'
import { useThemeColor } from '../../components/Themed'

import DateTimePicker from '@react-native-community/datetimepicker'
import Notify from '../../notifications/notificationHandler'
import WeekdayPicker from '../../components/WeekdayPicker'

interface Props {
setToastMessage: React.Dispatch<React.SetStateAction<string>>
setToastShow: React.Dispatch<React.SetStateAction<boolean>>
}

const NotificationSetter = ({ setToastMessage, setToastShow }: Props) => {
const [time, setTime] = React.useState(new Date())
const [pickedTime, setPickedTime] = React.useState(false)
const [weekdays, setWeekdays] = React.useState([-1])
const [show, setShow] = React.useState(false)
const changeTimeHandler = (event: Event, value?: Date) => {
setShow(Platform.OS === 'ios')
if (value) {
setPickedTime(true)
} else {
setPickedTime(false)
}
setTime(value || new Date())
}

const timeString = (timeSet: Date) => {
const timeHours = timeSet.getHours()
const timeMinutes = timeSet.getMinutes()
const hours =
timeHours === 0 ? '00' : timeHours <= 9 ? '0' + timeHours.toString() : timeHours.toString()
const minutes =
timeMinutes === 0
? '00'
: timeMinutes <= 9
? '0' + timeMinutes.toString()
: timeMinutes.toString()
return hours + ' : ' + minutes
}

const textColor = useThemeColor({}, 'text')
const textWhite = useThemeColor({}, 'white')

return (
<>
SalikSayyed marked this conversation as resolved.
Show resolved Hide resolved
<WeekdayPicker weekdays={weekdays} setWeekdays={setWeekdays} />
<View style={styles.buttonContainer}>
<TouchableOpacity onPress={() => setShow(!show)}>
<View style={[styles.pickTime, { backgroundColor: useThemeColor({}, 'primary') }]}>
<Text style={{ color: textWhite }}>PICK TIME</Text>
</View>
</TouchableOpacity>
{pickedTime && (
<Text style={[styles.selectedTime, { color: textColor }]}>At {timeString(time)} </Text>
)}
<TouchableOpacity
onPress={() => {
if (weekdays.length !== 1 && pickedTime) {
Notify.testSchedular(weekdays, time)
setToastMessage('Reminders Set')
setToastShow(true)
} else {
setToastMessage('Please Pick Day and Time')
setToastShow(true)
}
}}
>
<View style={[styles.pickTime, { backgroundColor: useThemeColor({}, 'primary') }]}>
<Text style={{ color: textWhite }}>NOTIFY</Text>
</View>
</TouchableOpacity>
</View>
{show && (
<DateTimePicker
value={time}
mode="time"
is24Hour={true}
display="clock"
onChange={changeTimeHandler}
/>
)}
</>
)
}

const styles = StyleSheet.create({
pickTime: {
width: 90,
height: 25,
justifyContent: 'center',
alignItems: 'center',
borderRadius: 10,
},
selectedTime: {
fontSize: 20,
},
buttonContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
marginHorizontal: 1,
},
})

export default NotificationSetter
Loading