Prayer city saved on click
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { Pressable, ScrollView, StyleSheet, Text, View } from 'react-native';
|
import { Pressable, ScrollView, StyleSheet, Text, View } from 'react-native';
|
||||||
import { useCallback, useEffect, useState } from 'react';
|
import { useCallback, useEffect, useState } from 'react';
|
||||||
import { useSafeAreaInsets } from 'react-native-safe-area-context';
|
import { useSafeAreaInsets } from 'react-native-safe-area-context';
|
||||||
|
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||||
|
|
||||||
import { getPrayerTimes, cities } from '../../utils/prayerTimeCalculator';
|
import { getPrayerTimes, cities } from '../../utils/prayerTimeCalculator';
|
||||||
import i18n from '../../i18n';
|
import i18n from '../../i18n';
|
||||||
@@ -21,6 +22,21 @@ export default function TabIndex() {
|
|||||||
const [nextPrayerName, setNextPrayerName] = useState<string | null>(null);
|
const [nextPrayerName, setNextPrayerName] = useState<string | null>(null);
|
||||||
const insets = useSafeAreaInsets();
|
const insets = useSafeAreaInsets();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const loadSelectedCity = async () => {
|
||||||
|
try {
|
||||||
|
const city = (await AsyncStorage.getItem('selectedCity')) as City;
|
||||||
|
if (city) {
|
||||||
|
setSelectedCity(city);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to load selected city:', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
loadSelectedCity();
|
||||||
|
}, []);
|
||||||
|
|
||||||
const prayerNameMapping: { [key: string]: string } = {
|
const prayerNameMapping: { [key: string]: string } = {
|
||||||
fajr: i18n.t('fajr'),
|
fajr: i18n.t('fajr'),
|
||||||
dhuhr: i18n.t('dhuhr'),
|
dhuhr: i18n.t('dhuhr'),
|
||||||
@@ -66,6 +82,15 @@ export default function TabIndex() {
|
|||||||
return () => clearInterval(interval);
|
return () => clearInterval(interval);
|
||||||
}, [updatePrayerTimes]);
|
}, [updatePrayerTimes]);
|
||||||
|
|
||||||
|
const handleCityChange = async (city: City) => {
|
||||||
|
setSelectedCity(city);
|
||||||
|
try {
|
||||||
|
await AsyncStorage.setItem('selectedCity', city);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to save selected city:', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const renderPrayerTime = (prayer: Prayer) => {
|
const renderPrayerTime = (prayer: Prayer) => {
|
||||||
const isNextPrayer = prayer.name === nextPrayerName;
|
const isNextPrayer = prayer.name === nextPrayerName;
|
||||||
return (
|
return (
|
||||||
@@ -94,7 +119,7 @@ export default function TabIndex() {
|
|||||||
{(Object.keys(cities) as City[]).map((city) => (
|
{(Object.keys(cities) as City[]).map((city) => (
|
||||||
<Pressable
|
<Pressable
|
||||||
key={city}
|
key={city}
|
||||||
onPress={() => setSelectedCity(city)}
|
onPress={() => handleCityChange(city)}
|
||||||
style={[styles.cityButton, { backgroundColor: theme.secondary }, selectedCity === city && [styles.activeCityButton, { backgroundColor: theme.tint }]]}>
|
style={[styles.cityButton, { backgroundColor: theme.secondary }, selectedCity === city && [styles.activeCityButton, { backgroundColor: theme.tint }]]}>
|
||||||
<Text
|
<Text
|
||||||
style={[
|
style={[
|
||||||
|
|||||||
Reference in New Issue
Block a user