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 14 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
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
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,
},
})
71 changes: 71 additions & 0 deletions notifications/notificationHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import * as Notifications from 'expo-notifications'
import { Platform } from 'react-native'

const askPermissions = async () => {
Notifications.getPermissionsAsync().then((status) => {
if (!status.granted) {
console.log('Requesting Permission')
SalikSayyed marked this conversation as resolved.
Show resolved Hide resolved
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) => {
SalikSayyed marked this conversation as resolved.
Show resolved Hide resolved
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) => {
SalikSayyed marked this conversation as resolved.
Show resolved Hide resolved
if (Platform.OS === 'android') {
AndroidSchedular(weekday, time)
}
if (Platform.OS === 'ios') {
iosScheduler(weekday, time)
}
}

export default {
askPermission: askPermissions,
testSchedular: PlatformScheduler,
deleteNotification: deleteNotifications,
}
4 changes: 4 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 All @@ -42,9 +44,11 @@
"react-native-gesture-handler": "~1.10.2",
"react-native-get-random-values": "~1.7.0",
"react-native-paper": "^4.9.2",
"react-native-push-notification": "^8.0.0",
SalikSayyed marked this conversation as resolved.
Show resolved Hide resolved
"react-native-reanimated": "~2.2.0",
"react-native-safe-area-context": "3.2.0",
"react-native-screens": "~3.4.0",
"react-native-simple-toast": "^1.1.3",
SalikSayyed marked this conversation as resolved.
Show resolved Hide resolved
"react-native-web": "~0.13.12",
"react-redux": "^7.2.4",
"redux-persist": "^6.0.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
36 changes: 35 additions & 1 deletion screens/Settings/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
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 'react-native-simple-toast'
import NotificationSetter from './notificationSetter'
import Notify from '../../notifications/notificationHandler'

const Settings = () => {
const dispatch = useAppDispatch()
const [showNotification, toggleShowNotification] = React.useState(false)
const clearData = () => {
Alert.alert(
'Clear Data',
Expand All @@ -22,10 +27,39 @@ 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 />
</Dialog.Content>
<Dialog.Actions>
<Button onPress={() => toggleShowNotification(!showNotification)}>CANCEL</Button>
</Dialog.Actions>
</Dialog>
<Divider />
<List.Item
title="Clear All Reminders"
onPress={() => {
Notify.deleteNotification()
Toast.show('Deleted Reminders')
}}
/>
<Divider />
</>
)
}
Expand Down
102 changes: 102 additions & 0 deletions screens/Settings/notificationSetter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
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'
import Toast from 'react-native-simple-toast'

const NotificationSetter = () => {
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')
console.log(value?.getHours(), value?.getMinutes(), value?.getSeconds())
SalikSayyed marked this conversation as resolved.
Show resolved Hide resolved
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)
Toast.show('Reminders Set')
console.log(weekdays.toString())
SalikSayyed marked this conversation as resolved.
Show resolved Hide resolved
} else {
Toast.show('Please Set Timing and Weekdays')
}
}}
>
<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