All files / src/app/(home-page) enable-location.tsx

85.71% Statements 36/42
90.47% Branches 19/21
75% Functions 9/12
85% Lines 34/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223                4x 4x 4x           4x 4x 4x 4x 4x       4x   4x     4x 1x 1x 1x   1x                                 1x 1x                                   4x 1x 1x     4x 4x     4x     4x     4x     4x 2x 1x 1x   1x     4x                                                                                 1x                                   12x                             1x                                                                                                        
import React from "react";
import { StyleSheet, Text, View, Pressable, Alert } from "react-native";
import { useRouter, useLocalSearchParams } from "expo-router";
import useLocationService from "../../hooks/useLocationService";
import useLocationStore from "../../hooks/useLocationStore";
import { useTheme } from "../../hooks/useTheme";
 
export default function EnableLocation() {
  const router = useRouter();
  const params = useLocalSearchParams<{ reason?: string }>();
  const { colors } = useTheme();
  const {
    requestPermission,
    markPermissionScreenSeen,
    markUserSkipped,
    openSettings,
  } = useLocationService();
  const permissionStatus = useLocationStore((state) => state.permissionStatus);
  const canAskAgain = useLocationStore((state) => state.canAskAgain);
  const userSkippedPermission = useLocationStore(
    (state) => state.userSkippedPermission,
  );
 
  const isRevoked =
    params.reason === "revoked" || permissionStatus === "revoked";
  const shouldShowOSPrompt =
    !userSkippedPermission &&
    (canAskAgain || permissionStatus === "undetermined");
 
  const onEnableLocation = async () => {
    Eif (shouldShowOSPrompt) {
      const granted = await requestPermission();
      await markPermissionScreenSeen();
 
      Iif (!granted) {
        Alert.alert(
          "Permission Denied",
          "You can enable location later in device settings.",
          [
            { text: "Continue", onPress: () => router.replace("/(home-page)") },
            {
              text: "Open Settings",
              onPress: () => {
                void openSettings();
              },
            },
          ],
        );
        return;
      }
 
      router.replace("/(home-page)");
      return;
    }
 
    Alert.alert(
      "Permission Required",
      "Location permission was previously denied. Please enable it in your device settings.",
      [
        { text: "Cancel", style: "cancel" },
        {
          text: "Open Settings",
          onPress: () => {
            void openSettings();
          },
        },
      ],
    );
  };
 
  const onSkipLocation = async () => {
    await markUserSkipped();
    router.replace("/(home-page)");
  };
 
  const headerEmoji = isRevoked ? "⚠️" : "📍";
  const titleText = isRevoked
    ? "Location Permission Revoked"
    : "Enable Location Services";
  const subtitleText = isRevoked
    ? "Location access was previously granted but has been revoked. To continue using location features, please re-enable permission in your device settings."
    : "To help you navigate Concordia's campus, we need access to your location. This allows us to show your position on the map and provide accurate directions.";
  const skipLabelText = isRevoked
    ? "Continue without location"
    : "Skip for now";
  const primaryButtonLabel = shouldShowOSPrompt
    ? "Enable Location"
    : "Open Settings";
  const onPrimaryButtonPress = () => {
    if (shouldShowOSPrompt) {
      void onEnableLocation();
      return;
    }
    void openSettings();
  };
 
  return (
    <View style={[styles.container, { backgroundColor: colors.background }]}>
      <View
        style={[styles.iconCircle, { backgroundColor: colors.primary + "2E" }]}
      >
        <Text style={styles.icon}>{headerEmoji}</Text>
      </View>
 
      <Text style={[styles.title, { color: colors.text }]}>{titleText}</Text>
      <Text style={[styles.subtitle, { color: colors.textMuted }]}>
        {subtitleText}
      </Text>
 
      <View style={styles.bullets}>
        <Bullet
          colors={colors}
          title="Real-time positioning"
          desc="See your exact location on campus"
        />
        <Bullet
          colors={colors}
          title="Turn-by-turn directions"
          desc="Navigate between buildings easily"
        />
        <Bullet
          colors={colors}
          title="Nearby points of interest"
          desc="Find cafeterias, libraries, and more"
        />
      </View>
 
      <Pressable
        style={[styles.enableBtn, { backgroundColor: colors.primary }]}
        onPress={onPrimaryButtonPress}
      >
        <Text style={styles.enableText}>{primaryButtonLabel}</Text>
      </Pressable>
 
      <Pressable
        style={styles.skipBtn}
        onPress={() => {
          void onSkipLocation();
        }}
      >
        <Text style={[styles.skipText, { color: colors.textMuted }]}>
          {skipLabelText}
        </Text>
      </Pressable>
    </View>
  );
}
 
type BulletProps = Readonly<{
  colors: { text: string; textMuted: string; primary: string };
  title: string;
  desc: string;
}>;
 
function Bullet({ colors, title, desc }: BulletProps) {
  return (
    <View style={styles.bulletRow}>
      <View style={[styles.dot, { backgroundColor: colors.primary }]} />
      <View style={{ flex: 1 }}>
        <Text style={[styles.bulletTitle, { color: colors.text }]}>
          {title}
        </Text>
        <Text style={[styles.bulletDesc, { color: colors.textMuted }]}>
          {desc}
        </Text>
      </View>
    </View>
  );
}
 
const styles = StyleSheet.create({
  container: {
    flex: 1,
    paddingTop: 80,
    paddingHorizontal: 24,
  },
  iconCircle: {
    width: 120,
    height: 120,
    borderRadius: 60,
    alignSelf: "center",
    alignItems: "center",
    justifyContent: "center",
    marginBottom: 22,
  },
  icon: { fontSize: 40 },
 
  title: {
    fontSize: 22,
    fontWeight: "700",
    textAlign: "center",
    marginBottom: 10,
  },
  subtitle: {
    textAlign: "center",
    lineHeight: 20,
    marginBottom: 24,
  },
 
  bullets: { gap: 18, marginBottom: 28 },
 
  bulletRow: { flexDirection: "row", gap: 12, alignItems: "flex-start" },
  dot: {
    width: 10,
    height: 10,
    borderRadius: 5,
    marginTop: 6,
  },
  bulletTitle: { fontWeight: "700", marginBottom: 2 },
  bulletDesc: {},
 
  enableBtn: {
    borderRadius: 14,
    paddingVertical: 16,
    alignItems: "center",
    marginTop: 10,
  },
  enableText: { color: "#fff", fontWeight: "700", fontSize: 16 },
 
  skipBtn: { paddingVertical: 14, alignItems: "center" },
  skipText: { fontWeight: "600" },
});