// import React, { useState, useRef, useEffect, useCallback } from "react";
// import {
// View,
// Text,
// StyleSheet,
// TouchableOpacity,
// Alert,
// Dimensions,
// Platform,
// StatusBar,
// Animated,
// Image,
// ScrollView,
// ActivityIndicator,
// Vibration,
// Modal,
// PanResponder,
// GestureResponderEvent,
// PanResponderGestureState,
// } from "react-native";
// import { CameraView, CameraType, useCameraPermissions } from "expo-camera";
// import { Ionicons } from "@expo/vector-icons";
// import { LinearGradient } from "expo-linear-gradient";
// import * as ImageManipulator from "expo-image-manipulator";
// import * as ImagePicker from "expo-image-picker";
// import { useAuth } from "@/contexts/AuthContext";
// import { API_BASE } from "@/constants/config";
// // ─── Pose Types ─────────────────────────────────────────────────────────────
// type PoseType = 'center' | 'right' | 'left';
// interface PoseConfig {
// id: PoseType;
// label: string;
// icon: string;
// description: string;
// color: string;
// }
// const POSES: PoseConfig[] = [
// { id: 'center', label: 'Hadap Depan', icon: '👤', description: 'Lihat kamera secara langsung', color: '#2563EB' },
// { id: 'right', label: 'Hadap Kanan', icon: '👉', description: 'Putar kepala ke kanan ~30°', color: '#8B5CF6' },
// { id: 'left', label: 'Hadap Kiri', icon: '👈', description: 'Putar kepala ke kiri ~30°', color: '#EC4899' },
// ];
// const CAPTURES_PER_POSE = 2; // burst: 2 foto per klik
// // ─── Constants ──────────────────────────────────────────────────────────────
// const { width: SCREEN_WIDTH, height: SCREEN_HEIGHT } = Dimensions.get("window");
// const CAMERA_SIZE = SCREEN_WIDTH * 0.85;
// const OVAL_WIDTH = CAMERA_SIZE * 0.55;
// const OVAL_HEIGHT = CAMERA_SIZE * 0.75;
// const OVAL_TOP = (CAMERA_SIZE - OVAL_HEIGHT) / 2;
// const OVAL_LEFT = (CAMERA_SIZE - OVAL_WIDTH) / 2;
// // Crop Editor constants
// const EDITOR_SIZE = SCREEN_WIDTH * 0.9;
// const EDITOR_OVAL_WIDTH = EDITOR_SIZE * 0.6;
// const EDITOR_OVAL_HEIGHT = EDITOR_SIZE * 0.8;
// // ─── Theme Colors ───────────────────────────────────────────────────────────
// const THEME = {
// bg: "#FFFFFF",
// bgSecondary: "#F8FAFC",
// bgCard: "#FFFFFF",
// bgInput: "#F1F5F9",
// border: "#E2E8F0",
// borderLight: "#F1F5F9",
// textPrimary: "#1E293B",
// textSecondary: "#64748B",
// textMuted: "#94A3B8",
// primary: "#2563EB",
// primaryLight: "#3B82F6",
// primaryDark: "#1D4ED8",
// primaryBg: "#EFF6FF",
// primaryBgDark: "#DBEAFE",
// success: "#10B981",
// successBg: "#ECFDF5",
// warning: "#F59E0B",
// warningBg: "#FFFBEB",
// danger: "#EF4444",
// dangerBg: "#FEF2F2",
// shadow: "rgba(0, 0, 0, 0.06)",
// shadowDark: "rgba(0, 0, 0, 0.08)",
// };
// // ─── Oval Mask Overlay ──────────────────────────────────────────────────────
// function OvalMaskOverlay() {
// return ;
// }
// // ─── Crop Editor Modal Component ───────────────────────────────────────────
// // Approach: fixed oval mask, user drags/zooms the IMAGE behind it.
// // This ensures what-you-see-is-what-you-get when confirming the crop.
// interface CropEditorProps {
// visible: boolean;
// imageUri: string;
// onConfirm: (croppedUri: string) => void;
// onCancel: () => void;
// }
// function CropEditor({ visible, imageUri, onConfirm, onCancel }: CropEditorProps) {
// const [isProcessing, setIsProcessing] = useState(false);
// // Original image pixel dimensions
// const [originalWidth, setOriginalWidth] = useState(0);
// const [originalHeight, setOriginalHeight] = useState(0);
// // Display dimensions (fit image into EDITOR_SIZE, covering the oval)
// const [displayWidth, setDisplayWidth] = useState(EDITOR_SIZE);
// const [displayHeight, setDisplayHeight] = useState(EDITOR_SIZE);
// // Image offset (how much the image is dragged)
// const [imageOffsetX, setImageOffsetX] = useState(0);
// const [imageOffsetY, setImageOffsetY] = useState(0);
// const [scale, setScale] = useState(1);
// // Refs to track current values in gesture handlers (avoids stale closures)
// const offsetXRef = useRef(0);
// const offsetYRef = useRef(0);
// const scaleRef = useRef(1);
// const displayWRef = useRef(EDITOR_SIZE);
// const displayHRef = useRef(EDITOR_SIZE);
// const gestureStartOffset = useRef({ x: 0, y: 0 });
// // Load image dimensions
// useEffect(() => {
// if (imageUri) {
// Image.getSize(
// imageUri,
// (w, h) => {
// setOriginalWidth(w);
// setOriginalHeight(h);
// // Display size: scale to COVER the oval area (so the image is always at least as large as the oval)
// const scaleX = EDITOR_SIZE / w;
// const scaleY = EDITOR_SIZE / h;
// const fitScale = Math.max(scaleX, scaleY);
// const dw = w * fitScale;
// const dh = h * fitScale;
// setDisplayWidth(dw);
// setDisplayHeight(dh);
// displayWRef.current = dw;
// displayHRef.current = dh;
// },
// (error) => console.warn('Failed to get image size:', error)
// );
// }
// }, [imageUri]);
// // Clamp helper: ensures the oval window always stays within the scaled image
// const clampOffset = (ox: number, oy: number, currentScale: number) => {
// const scaledW = displayWRef.current * currentScale;
// const scaledH = displayHRef.current * currentScale;
// // How far the image can move from center before the oval sees outside the image
// const maxOffsetX = Math.max(0, (scaledW - EDITOR_OVAL_WIDTH) / 2);
// const maxOffsetY = Math.max(0, (scaledH - EDITOR_OVAL_HEIGHT) / 2);
// return {
// x: Math.max(-maxOffsetX, Math.min(maxOffsetX, ox)),
// y: Math.max(-maxOffsetY, Math.min(maxOffsetY, oy)),
// };
// };
// // PanResponder — moves the image behind the fixed oval
// const panResponder = useRef(
// PanResponder.create({
// onStartShouldSetPanResponder: () => true,
// onMoveShouldSetPanResponder: (_, gs) =>
// Math.abs(gs.dx) > 2 || Math.abs(gs.dy) > 2,
// onPanResponderGrant: () => {
// gestureStartOffset.current = {
// x: offsetXRef.current,
// y: offsetYRef.current,
// };
// },
// onPanResponderMove: (_, gestureState) => {
// const newX = gestureStartOffset.current.x + gestureState.dx;
// const newY = gestureStartOffset.current.y + gestureState.dy;
// const clamped = clampOffset(newX, newY, scaleRef.current);
// offsetXRef.current = clamped.x;
// offsetYRef.current = clamped.y;
// setImageOffsetX(clamped.x);
// setImageOffsetY(clamped.y);
// },
// onPanResponderRelease: () => {},
// })
// ).current;
// const applyZoom = (newScale: number) => {
// const clamped = clampOffset(offsetXRef.current, offsetYRef.current, newScale);
// offsetXRef.current = clamped.x;
// offsetYRef.current = clamped.y;
// scaleRef.current = newScale;
// setScale(newScale);
// setImageOffsetX(clamped.x);
// setImageOffsetY(clamped.y);
// };
// const zoomIn = () => applyZoom(Math.min(3, scale + 0.2));
// const zoomOut = () => applyZoom(Math.max(0.5, scale - 0.2));
// const resetPosition = () => {
// offsetXRef.current = 0;
// offsetYRef.current = 0;
// scaleRef.current = 1;
// setImageOffsetX(0);
// setImageOffsetY(0);
// setScale(1);
// };
// const handleConfirm = async () => {
// if (originalWidth === 0 || originalHeight === 0) return;
// setIsProcessing(true);
// try {
// // The image is displayed at (displayWidth * scale) x (displayHeight * scale)
// // centered in the EDITOR_SIZE container, then offset by (imageOffsetX, imageOffsetY).
// //
// // The oval is fixed at the center of the container.
// // We need to find which region of the ORIGINAL image is visible through the oval.
// const scaledW = displayWidth * scale;
// const scaledH = displayHeight * scale;
// // Ratio from scaled-display pixels to original pixels
// const ratioX = originalWidth / scaledW;
// const ratioY = originalHeight / scaledH;
// // The oval's top-left in container coordinates
// const ovalLeft = (EDITOR_SIZE - EDITOR_OVAL_WIDTH) / 2;
// const ovalTop = (EDITOR_SIZE - EDITOR_OVAL_HEIGHT) / 2;
// // The image's top-left in container coordinates
// // (image is centered, then shifted by offset)
// const imgLeft = (EDITOR_SIZE - scaledW) / 2 + imageOffsetX;
// const imgTop = (EDITOR_SIZE - scaledH) / 2 + imageOffsetY;
// // The oval's position relative to the image's top-left (in scaled-display coords)
// const ovalInImgX = ovalLeft - imgLeft;
// const ovalInImgY = ovalTop - imgTop;
// // Convert to original image pixel coordinates
// let cropOriginX = Math.round(ovalInImgX * ratioX);
// let cropOriginY = Math.round(ovalInImgY * ratioY);
// let cropWidth = Math.round(EDITOR_OVAL_WIDTH * ratioX);
// let cropHeight = Math.round(EDITOR_OVAL_HEIGHT * ratioY);
// // Clamp to original image bounds
// cropOriginX = Math.max(0, Math.min(cropOriginX, originalWidth - 1));
// cropOriginY = Math.max(0, Math.min(cropOriginY, originalHeight - 1));
// cropWidth = Math.max(10, Math.min(cropWidth, originalWidth - cropOriginX));
// cropHeight = Math.max(10, Math.min(cropHeight, originalHeight - cropOriginY));
// const cropRegion = {
// originX: cropOriginX,
// originY: cropOriginY,
// width: cropWidth,
// height: cropHeight,
// };
// const result = await ImageManipulator.manipulateAsync(
// imageUri,
// [
// { crop: cropRegion },
// { resize: { width: 800 } },
// ],
// { compress: 0.9, format: ImageManipulator.SaveFormat.JPEG }
// );
// onConfirm(result.uri);
// } catch (error) {
// console.error('Crop error:', error);
// Alert.alert('Error', 'Gagal memproses foto. Silakan coba lagi.');
// } finally {
// setIsProcessing(false);
// }
// };
// // Reset state when modal opens
// useEffect(() => {
// if (visible) {
// offsetXRef.current = 0;
// offsetYRef.current = 0;
// scaleRef.current = 1;
// setImageOffsetX(0);
// setImageOffsetY(0);
// setScale(1);
// }
// }, [visible, imageUri]);
// return (
//
//
//
//
//
//
// Atur Posisi Wajah
//
// {isProcessing ? (
//
// ) : (
//
// )}
//
//
//
//
// Geser foto untuk menyesuaikan posisi wajah di dalam oval
//
//
//
//
// {/* Image — draggable & zoomable behind the fixed oval */}
//
// {/* Dark overlay with transparent oval cutout */}
//
// {/* Top dark band */}
//
// {/* Middle row: left dark | oval hole | right dark */}
//
//
//
//
//
//
//
// {/* Bottom dark band */}
//
//
//
//
//
//
//
//
//
// {Math.round(scale * 100)}%
//
//
//
//
//
//
// Reset
//
//
//
//
// );
// }
// const OVAL_TOP_OFFSET = (EDITOR_SIZE - EDITOR_OVAL_HEIGHT) / 2;
// const OVAL_LEFT_OFFSET = (EDITOR_SIZE - EDITOR_OVAL_WIDTH) / 2;
// const cropStyles = StyleSheet.create({
// modalContainer: {
// flex: 1,
// backgroundColor: '#000',
// },
// header: {
// flexDirection: 'row',
// alignItems: 'center',
// justifyContent: 'space-between',
// paddingTop: Platform.OS === 'android' ? (StatusBar.currentHeight ?? 0) + 10 : 50,
// paddingHorizontal: 16,
// paddingBottom: 12,
// backgroundColor: 'rgba(0,0,0,0.9)',
// },
// headerButton: {
// width: 44,
// height: 44,
// alignItems: 'center',
// justifyContent: 'center',
// },
// headerTitle: {
// fontSize: 18,
// fontWeight: '700',
// color: '#fff',
// },
// instructionContainer: {
// paddingHorizontal: 20,
// paddingVertical: 12,
// backgroundColor: 'rgba(0,0,0,0.7)',
// },
// instructionText: {
// fontSize: 14,
// color: 'rgba(255,255,255,0.8)',
// textAlign: 'center',
// },
// editorContainer: {
// flex: 1,
// alignItems: 'center',
// justifyContent: 'center',
// padding: 10,
// },
// imageWrapper: {
// width: EDITOR_SIZE,
// height: EDITOR_SIZE,
// position: 'relative',
// alignItems: 'center',
// justifyContent: 'center',
// overflow: 'hidden',
// borderRadius: 12,
// },
// fullImage: {
// position: 'absolute',
// },
// // Dark overlay pieces around the oval cutout
// overlayContainer: {
// ...StyleSheet.absoluteFillObject,
// },
// darkBandHorizontal: {
// width: '100%',
// height: OVAL_TOP_OFFSET,
// backgroundColor: 'rgba(0,0,0,0.55)',
// },
// middleRow: {
// flexDirection: 'row',
// height: EDITOR_OVAL_HEIGHT,
// },
// darkBandVertical: {
// width: OVAL_LEFT_OFFSET,
// height: '100%',
// backgroundColor: 'rgba(0,0,0,0.55)',
// },
// ovalHole: {
// width: EDITOR_OVAL_WIDTH,
// height: EDITOR_OVAL_HEIGHT,
// borderRadius: EDITOR_OVAL_WIDTH / 2,
// overflow: 'hidden',
// // transparent — lets the image show through
// },
// ovalBorder: {
// ...StyleSheet.absoluteFillObject,
// borderRadius: EDITOR_OVAL_WIDTH / 2,
// borderWidth: 3,
// borderColor: '#fff',
// },
// controlsContainer: {
// paddingHorizontal: 20,
// paddingVertical: 20,
// backgroundColor: 'rgba(0,0,0,0.9)',
// borderTopWidth: 1,
// borderTopColor: 'rgba(255,255,255,0.1)',
// },
// zoomControls: {
// flexDirection: 'row',
// alignItems: 'center',
// justifyContent: 'center',
// gap: 24,
// marginBottom: 16,
// },
// zoomButton: {
// width: 50,
// height: 50,
// borderRadius: 25,
// backgroundColor: 'rgba(255,255,255,0.15)',
// alignItems: 'center',
// justifyContent: 'center',
// },
// zoomLabel: {
// fontSize: 16,
// fontWeight: '600',
// color: '#fff',
// minWidth: 60,
// textAlign: 'center',
// },
// resetButton: {
// flexDirection: 'row',
// alignItems: 'center',
// justifyContent: 'center',
// gap: 8,
// paddingVertical: 12,
// paddingHorizontal: 24,
// backgroundColor: 'rgba(255,255,255,0.1)',
// borderRadius: 12,
// },
// resetText: {
// fontSize: 14,
// color: THEME.textSecondary,
// fontWeight: '500',
// },
// });
// // ─── Main Component ────────────────────────────────────────────────────────────
// export default function RegisterFotoScreen() {
// const { user, token } = useAuth();
// // ── State ─────────────────────────────────────────────────────────────────
// const [permission, requestPermission] = useCameraPermissions();
// const [facing] = useState("front");
// const [isCapturing, setIsCapturing] = useState(false);
// const [isSubmitting, setIsSubmitting] = useState(false);
// const [capturedImages, setCapturedImages] = useState([]);
// const [showPreview, setShowPreview] = useState(false);
// const [captureAnim] = useState(new Animated.Value(1));
// const [flashAnim] = useState(new Animated.Value(0));
// const [isAlreadyRegistered, setIsAlreadyRegistered] = useState(false);
// const [isCheckingStatus, setIsCheckingStatus] = useState(true);
// const cameraRef = useRef(null);
// // ── Crop Editor State ─────────────────────────────────────────────────────
// const [showCropEditor, setShowCropEditor] = useState(false);
// const [selectedImageUri, setSelectedImageUri] = useState('');
// // ── Guided Pose State ─────────────────────────────────────────────────────
// const [currentPoseIndex, setCurrentPoseIndex] = useState(0);
// const currentPose = POSES[currentPoseIndex];
// const totalRequired = POSES.length * CAPTURES_PER_POSE;
// const allCaptured = capturedImages.length >= totalRequired;
// // ── Reset ─────────────────────────────────────────────────────────────────
// const resetCapture = useCallback(() => {
// setCapturedImages([]);
// setCurrentPoseIndex(0);
// setShowPreview(false);
// }, []);
// // ── Check registration status ─────────────────────────────────────────────
// useEffect(() => {
// const checkRegistrationStatus = async () => {
// if (!token || !user?.nim) {
// setIsCheckingStatus(false);
// return;
// }
// try {
// const response = await fetch(`${API_BASE}/arcface/status/${user.nim}`, {
// method: "GET",
// headers: { "Authorization": `Bearer ${token}` },
// });
// if (response.ok || response.status === 200) {
// const result = await response.json();
// setIsAlreadyRegistered(result.is_registered === true);
// } else {
// setIsAlreadyRegistered(false);
// }
// } catch (error) {
// console.warn("Gagal cek status registrasi:", error);
// setIsAlreadyRegistered(false);
// } finally {
// setIsCheckingStatus(false);
// }
// };
// checkRegistrationStatus();
// }, [token, user?.nim]);
// // ── Crop to oval region ───────────────────────────────────────────────────
// const cropToOvalRegion = async (
// photoUri: string,
// photoWidth: number,
// photoHeight: number,
// isFront: boolean
// ): Promise => {
// try {
// const containerSize = CAMERA_SIZE;
// const scale = Math.max(containerSize / photoWidth, containerSize / photoHeight);
// const displayedWidth = photoWidth * scale;
// const displayedHeight = photoHeight * scale;
// const offsetX = (displayedWidth - containerSize) / 2;
// const offsetY = (displayedHeight - containerSize) / 2;
// let originX = (OVAL_LEFT + offsetX) / scale;
// let originY = (OVAL_TOP + offsetY) / scale;
// let width = OVAL_WIDTH / scale;
// let height = OVAL_HEIGHT / scale;
// originX = Math.max(0, Math.min(originX, photoWidth - 1));
// originY = Math.max(0, Math.min(originY, photoHeight - 1));
// width = Math.max(1, Math.min(width, photoWidth - originX));
// height = Math.max(1, Math.min(height, photoHeight - originY));
// const cropRegion = {
// originX: Math.round(originX),
// originY: Math.round(originY),
// width: Math.round(width),
// height: Math.round(height),
// };
// const actions: ImageManipulator.Action[] = [];
// if (isFront) {
// actions.push({ flip: ImageManipulator.FlipType.Horizontal });
// }
// actions.push({ crop: cropRegion });
// actions.push({ resize: { width: 800 } });
// const manipResult = await ImageManipulator.manipulateAsync(
// photoUri,
// actions,
// { compress: 0.9, format: ImageManipulator.SaveFormat.JPEG }
// );
// return manipResult.uri;
// } catch (error) {
// console.warn("Gagal melakukan crop oval:", error);
// return photoUri;
// }
// };
// // ── Burst capture: 1 klik = 2 foto ───────────────────────────────────────
// const handleBurstCapture = async () => {
// if (!cameraRef.current || isCapturing || allCaptured) return;
// // Capture animation
// Animated.sequence([
// Animated.timing(captureAnim, { toValue: 0.92, duration: 80, useNativeDriver: true }),
// Animated.spring(captureAnim, { toValue: 1, friction: 4, useNativeDriver: true }),
// ]).start();
// setIsCapturing(true);
// try {
// const newPhotos: string[] = [];
// for (let i = 0; i < CAPTURES_PER_POSE; i++) {
// // Flash effect
// Animated.sequence([
// Animated.timing(flashAnim, { toValue: 1, duration: 60, useNativeDriver: true }),
// Animated.timing(flashAnim, { toValue: 0, duration: 150, useNativeDriver: true }),
// ]).start();
// const photo = await cameraRef.current.takePictureAsync({
// quality: 0.85,
// shutterSound: false,
// });
// if (photo?.uri) {
// const croppedUri = photo.width && photo.height
// ? await cropToOvalRegion(photo.uri, photo.width, photo.height, facing === "front")
// : photo.uri;
// newPhotos.push(croppedUri);
// Vibration.vibrate(30);
// }
// // Jeda singkat antara 2 foto burst
// if (i < CAPTURES_PER_POSE - 1) {
// await new Promise(resolve => setTimeout(resolve, 400));
// }
// }
// // Update state
// setCapturedImages(prev => [...prev, ...newPhotos]);
// // Pindah ke pose berikutnya atau selesai
// if (currentPoseIndex < POSES.length - 1) {
// setCurrentPoseIndex(prev => prev + 1);
// }
// // Jika ini pose terakhir, allCaptured akan jadi true di render berikutnya
// } catch (error) {
// Alert.alert("Error", "Gagal mengambil foto. Silakan coba lagi.");
// } finally {
// setIsCapturing(false);
// }
// };
// const handleRetake = () => {
// resetCapture();
// };
// // ── Handle Delete ─────────────────────────────────────────────────────────
// const handleDelete = async () => {
// if (!user?.nim || !token) {
// Alert.alert("Error", "Silakan login terlebih dahulu.");
// return;
// }
// Alert.alert(
// "Hapus Registrasi?",
// `Apakah Anda yakin ingin menghapus data wajah untuk NIM ${user.nim}? Anda perlu registrasi ulang di kemudian hari.`,
// [
// { text: "Batal", style: "cancel" },
// {
// text: "Hapus",
// style: "destructive",
// onPress: async () => {
// try {
// const response = await fetch(`${API_BASE}/arcface/delete/${user.nim}`, {
// method: "DELETE",
// headers: { "Authorization": `Bearer ${token}` },
// });
// const result = await response.json();
// if (result.success) {
// setIsAlreadyRegistered(false);
// Alert.alert("Berhasil", "Data wajah berhasil dihapus.");
// } else {
// Alert.alert("Gagal", result.message || "Gagal menghapus data wajah.");
// }
// } catch (error) {
// Alert.alert("Error", "Terjadi kesalahan koneksi.");
// }
// },
// },
// ]
// );
// };
// // ── Handle Confirm / Submit ───────────────────────────────────────────────
// const handleConfirm = async () => {
// if (capturedImages.length === 0) {
// Alert.alert("Error", "Tidak ada foto yang akan dikirim.");
// return;
// }
// if (!user?.nim) {
// Alert.alert("Error", "Silakan login terlebih dahulu untuk melakukan registrasi wajah.");
// return;
// }
// if (isAlreadyRegistered) {
// Alert.alert(
// "Perbarui Foto?",
// `Wajah Anda sudah terdaftar. Foto lama akan ditimpa dengan foto baru.\n\nApakah ingin melanjutkan?`,
// [
// { text: "Batal", style: "cancel" },
// { text: "Lanjutkan", onPress: () => performEnrollment(true) },
// ]
// );
// } else {
// performEnrollment(false);
// }
// };
// const performEnrollment = async (forceUpdate: boolean) => {
// setIsSubmitting(true);
// try {
// const photosBase64: string[] = [];
// for (const photoUri of capturedImages) {
// const base64Response = await fetch(photoUri);
// const blob = await base64Response.blob();
// const reader = new FileReader();
// const base64 = await new Promise((resolve, reject) => {
// reader.onloadend = () => resolve(reader.result as string);
// reader.onerror = () => reject(new Error("Failed to read image"));
// reader.readAsDataURL(blob);
// });
// photosBase64.push(base64);
// }
// const response = await fetch(`${API_BASE}/arcface/enroll`, {
// method: "POST",
// headers: {
// "Content-Type": "application/json",
// "Authorization": `Bearer ${token}`,
// },
// body: JSON.stringify({
// nim: user!.nim,
// foto_base64: photosBase64,
// force_update: forceUpdate,
// }),
// });
// const result = await response.json();
// if (result.success) {
// setIsAlreadyRegistered(true);
// Alert.alert(
// forceUpdate ? "Berhasil Diperbarui! 🎉" : "Berhasil! 🎉",
// forceUpdate
// ? `Wajah ${user!.nama_mahasiswa || user!.name} (${user!.nim}) berhasil diperbarui!\n\nData lama telah ditimpa.`
// : `Wajah ${user!.nama_mahasiswa || user!.name} (${user!.nim}) berhasil terdaftar!\n\nEmbedding tersimpan di: face_mahasiswa/${user!.nim}/`,
// [{ text: "OK", onPress: handleRetake }]
// );
// } else {
// if (result.message?.includes("sudah ter-enroll")) {
// if (forceUpdate) {
// setIsAlreadyRegistered(true);
// Alert.alert(
// "Berhasil Diperbarui! 🎉",
// `Wajah ${user!.nama_mahasiswa || user!.name} (${user!.nim}) berhasil diperbarui!\n\nData lama telah ditimpa.`,
// [{ text: "OK", onPress: handleRetake }]
// );
// } else {
// Alert.alert(
// "Info",
// "Wajah Anda sudah terdaftar sebelumnya.\n\nJika ingin memperbarui foto, silakan ambil foto baru dan pilih 'Perbarui'.",
// [{ text: "OK", onPress: handleRetake }]
// );
// }
// } else if (result.message?.includes("ArcFace model")) {
// Alert.alert(
// "Model Error",
// "Sistem Face Recognition belum siap. Hubungi admin untuk mengaktifkan model ArcFace.",
// [{ text: "OK" }]
// );
// } else {
// Alert.alert("Gagal", result.message || "Gagal menyimpan foto wajah.");
// }
// }
// } catch (error) {
// console.error("Enrollment error:", error);
// Alert.alert("Error", "Gagal mengirim foto. Periksa koneksi internet dan coba lagi.");
// } finally {
// setIsSubmitting(false);
// }
// };
// // ─── Render: Permission Loading ───────────────────────────────────────────
// if (!permission) {
// return (
//
//
//
//
// Memuat kamera...
//
//
// );
// }
// if (!permission.granted) {
// return (
//
//
//
//
//
//
//
// Izinkan Akses Kamera
//
// Kami membutuhkan akses kamera untuk mengambil foto wajah Anda untuk verifikasi kehadiran.
//
//
//
//
// Izinkan Kamera
//
//
//
//
//
// );
// }
// // ─── Render: Main Screen ─────────────────────────────────────────────────
// return (
//
//
// {/* Header */}
//
//
//
//
//
//
//
// {isAlreadyRegistered ? "Update Wajah" : "Register Wajah"}
//
//
// {isCheckingStatus
// ? "Memuat..."
// : isAlreadyRegistered
// ? "✓ Sudah terdaftar"
// : "Ambil foto untuk verifikasi"}
//
//
//
//
// {/* Gallery Button - Select one by one with crop editor */}
// {
// if (allCaptured || showPreview) return;
// const remaining = totalRequired - capturedImages.length;
// if (remaining <= 0) return;
// try {
// const result = await ImagePicker.launchImageLibraryAsync({
// mediaTypes: ["images"],
// allowsMultipleSelection: false,
// quality: 1,
// });
// if (!result.canceled && result.assets.length > 0) {
// const selectedPhoto = result.assets[0];
// setSelectedImageUri(selectedPhoto.uri);
// setShowCropEditor(true);
// }
// } catch (err) {
// Alert.alert("Error", "Gagal memilih gambar dari galeri");
// }
// }}
// >
//
//
// {/* Delete Button */}
// {isAlreadyRegistered && (
//
//
//
// )}
//
//
// {/* Scrollable body */}
//
// {/* Pose guide — DI ATAS oval, tidak menghalangi wajah */}
// {!showPreview && !allCaptured && (
//
//
// {currentPose.icon}
// {currentPose.label}
// {currentPose.description}
//
//
// )}
// {/* Camera wrapper */}
//
//
// {(showPreview || allCaptured) && capturedImages.length > 0 ? (
// /* Tampilkan foto depan (pertama) sebagai preview utama di oval */
//
// ) : (
// <>
//
// {/* Flash overlay */}
//
// >
// )}
//
// {!showPreview && !allCaptured && }
// {(showPreview || allCaptured) && (
//
//
//
// {allCaptured ? `${capturedImages.length} foto selesai` : "Foto Tertangkap"}
//
//
// )}
//
// {/* Thumbnails — di bawah oval, tampilkan semua foto kecil-kecil */}
// {capturedImages.length > 0 && (showPreview || allCaptured) && (
//
// {capturedImages.map((uri, idx) => (
//
//
//
// {idx < 2 ? 'Depan' : idx < 4 ? 'Kanan' : 'Kiri'} {(idx % 2) + 1}
//
//
// ))}
//
// )}
// {/* Pose progress stepper */}
//
// {POSES.map((pose, idx) => {
// const isDone = idx < currentPoseIndex || (idx === currentPoseIndex && allCaptured);
// const isCurrent = idx === currentPoseIndex && !allCaptured;
// return (
//
//
// {isDone && }
//
// {pose.label.replace('Hadap ', '')}
// {idx < POSES.length - 1 && (
//
// )}
//
// );
// })}
//
// {/* Status badge */}
//
//
//
//
// {showPreview
// ? `${capturedImages.length} foto siap dikirim`
// : allCaptured
// ? "Semua pose selesai! Ketuk untuk simpan"
// : isCapturing
// ? "📸 Mengambil 2 foto..."
// : `${currentPose.label} — Ketuk tombol untuk ambil foto`}
//
//
//
// {/* Instructions */}
//
//
//
//
// {showPreview || allCaptured
// ? "6 foto siap dikirim ke server"
// : "Tekan tombol kamera atau galeri untuk ambil foto"}
//
//
//
//
//
// Tombol galeri: pilih foto satu per satu, di-crop otomatis sesuai oval
//
//
//
//
//
// {allCaptured ? 'Semua 3 pose sudah diambil' : `Langkah ${currentPoseIndex + 1}/3: ${currentPose.description}`}
//
//
//
//
// Lepas kacamata, topi, atau masker
//
//
//
// {/* Bottom Action — fixed */}
//
// {showPreview ? (
//
//
//
// Ulangi
//
//
//
//
// {isSubmitting ? (
// <>
//
// Mengirim...
// >
// ) : (
// <>
//
// Simpan {capturedImages.length} Foto
// >
// )}
//
//
//
//
// ) : (
//
//
// setShowPreview(true) : handleBurstCapture}
// disabled={isCapturing}
// activeOpacity={0.85}
// >
//
// {isCapturing ? (
//
// ) : allCaptured ? (
//
// ) : (
//
// )}
//
//
//
//
//
// {isCapturing
// ? "Mengambil 2 foto..."
// : allCaptured
// ? `${capturedImages.length} foto — Ketuk untuk preview`
// : `Ketuk untuk ambil foto ${currentPose.label}`}
//
//
// )}
//
// {/* Crop Editor Modal */}
// {
// setShowCropEditor(false);
// setCapturedImages(prev => [...prev, croppedUri]);
// const newCount = capturedImages.length + 1;
// const nextPose = Math.floor(newCount / CAPTURES_PER_POSE);
// // Update pose index
// if (newCount % CAPTURES_PER_POSE === 0 && nextPose < POSES.length) {
// setCurrentPoseIndex(Math.min(nextPose, POSES.length - 1));
// }
// // If all 6 photos, go to preview
// if (newCount >= totalRequired) {
// setShowPreview(true);
// setCurrentPoseIndex(POSES.length - 1);
// }
// }}
// onCancel={() => {
// setShowCropEditor(false);
// setSelectedImageUri('');
// }}
// />
//
// );
// }
// // ─── Styles ─────────────────────────────────────────────────────────────────
// const styles = StyleSheet.create({
// container: {
// flex: 1,
// backgroundColor: THEME.bg,
// },
// // ─── Header ───────────────────────────────────────────────────────────────
// header: {
// flexDirection: "row",
// alignItems: "center",
// justifyContent: "space-between",
// paddingTop: Platform.OS === "android" ? (StatusBar.currentHeight ?? 0) + 10 : 55,
// paddingHorizontal: 20,
// paddingBottom: 14,
// backgroundColor: THEME.bg,
// borderBottomWidth: 1,
// borderBottomColor: THEME.border,
// },
// headerLeft: {
// flexDirection: "row",
// alignItems: "center",
// gap: 12,
// },
// headerIconContainer: {
// width: 42,
// height: 42,
// borderRadius: 12,
// backgroundColor: THEME.primaryBg,
// alignItems: "center",
// justifyContent: "center",
// },
// headerTitle: {
// fontSize: 18,
// fontWeight: "800",
// color: THEME.textPrimary,
// letterSpacing: 0.2,
// },
// headerSubtitle: {
// fontSize: 12,
// color: THEME.primary,
// marginTop: 1,
// },
// headerRightButtons: {
// flexDirection: "row",
// alignItems: "center",
// gap: 8,
// },
// iconButton: {
// width: 42,
// height: 42,
// borderRadius: 12,
// backgroundColor: THEME.bgInput,
// alignItems: "center",
// justifyContent: "center",
// borderWidth: 1,
// borderColor: THEME.border,
// },
// deleteButton: {
// borderColor: `${THEME.danger}40`,
// backgroundColor: `${THEME.danger}10`,
// },
// // ─── Scroll body ──────────────────────────────────────────────────────────
// scrollView: {
// flex: 1,
// },
// scrollContent: {
// alignItems: "center",
// paddingTop: 16,
// paddingBottom: 160,
// },
// // ─── Camera area ──────────────────────────────────────────────────────────
// cameraWrapper: {
// alignSelf: "center",
// width: CAMERA_SIZE,
// height: CAMERA_SIZE,
// position: "relative",
// borderRadius: 24,
// backgroundColor: "#FFFFFF",
// },
// ovalClip: {
// position: "absolute",
// top: OVAL_TOP,
// left: OVAL_LEFT,
// width: OVAL_WIDTH,
// height: OVAL_HEIGHT,
// borderRadius: OVAL_WIDTH / 2,
// overflow: "hidden",
// backgroundColor: THEME.bgSecondary,
// },
// cameraInner: {
// position: "absolute",
// top: -OVAL_TOP,
// left: -OVAL_LEFT,
// width: CAMERA_SIZE,
// height: CAMERA_SIZE,
// },
// previewImage: {
// width: "100%",
// height: "100%",
// },
// ovalRing: {
// position: "absolute",
// top: OVAL_TOP,
// left: OVAL_LEFT,
// width: OVAL_WIDTH,
// height: OVAL_HEIGHT,
// borderRadius: OVAL_WIDTH / 2,
// borderWidth: 3,
// borderColor: "#FFFFFF",
// zIndex: 10,
// },
// flashOverlay: {
// ...StyleSheet.absoluteFillObject,
// backgroundColor: "#FFFFFF",
// zIndex: 12,
// },
// // ─── Pose guide (above oval) ────────────────────────────────────────────────
// poseGuideAbove: {
// alignItems: "center",
// marginBottom: 12,
// },
// poseGuideBox: {
// backgroundColor: "rgba(0,0,0,0.82)",
// paddingHorizontal: 20,
// paddingVertical: 12,
// borderRadius: 16,
// alignItems: "center",
// borderWidth: 2,
// gap: 3,
// },
// poseIcon: {
// fontSize: 26,
// },
// poseLabel: {
// fontSize: 16,
// fontWeight: "700",
// color: "#FFFFFF",
// },
// poseDesc: {
// fontSize: 12,
// color: "rgba(255,255,255,0.75)",
// },
// // ─── Preview badge ────────────────────────────────────────────────────────
// previewBadge: {
// position: "absolute",
// top: 12,
// right: 12,
// flexDirection: "row",
// alignItems: "center",
// gap: 6,
// backgroundColor: "rgba(255,255,255,0.95)",
// paddingHorizontal: 10,
// paddingVertical: 5,
// borderRadius: 20,
// borderWidth: 1,
// borderColor: THEME.border,
// zIndex: 20,
// },
// previewBadgeText: {
// fontSize: 11,
// fontWeight: "600",
// color: THEME.success,
// },
// // ─── Thumbnail row (below oval) ───────────────────────────────────────────
// thumbnailRow: {
// flexDirection: "row",
// justifyContent: "center",
// flexWrap: "wrap",
// gap: 10,
// marginTop: 14,
// paddingHorizontal: 20,
// },
// thumbnailWrapper: {
// alignItems: "center",
// gap: 4,
// },
// thumbnailBelow: {
// width: 52,
// height: 64,
// borderRadius: 10,
// borderWidth: 2,
// borderColor: THEME.border,
// },
// thumbnailLabel: {
// fontSize: 9,
// fontWeight: "600",
// color: THEME.textMuted,
// },
// // ─── Stepper ──────────────────────────────────────────────────────────────
// stepperContainer: {
// flexDirection: "row",
// alignItems: "center",
// justifyContent: "center",
// marginTop: 16,
// paddingHorizontal: 16,
// },
// stepperItem: {
// alignItems: "center",
// flexDirection: "row",
// gap: 4,
// },
// stepperDot: {
// width: 22,
// height: 22,
// borderRadius: 11,
// alignItems: "center",
// justifyContent: "center",
// },
// stepperLabel: {
// fontSize: 10,
// color: THEME.textMuted,
// fontWeight: "500",
// marginRight: 2,
// },
// stepperLine: {
// width: 16,
// height: 2,
// backgroundColor: THEME.border,
// marginHorizontal: 2,
// borderRadius: 1,
// },
// // ─── Status badge ─────────────────────────────────────────────────────────
// statusWrapper: {
// alignItems: "center",
// marginTop: 12,
// paddingHorizontal: 24,
// },
// statusBadge: {
// flexDirection: "row",
// alignItems: "center",
// paddingHorizontal: 18,
// paddingVertical: 9,
// borderRadius: 50,
// borderWidth: 1,
// gap: 8,
// },
// statusDot: {
// width: 8,
// height: 8,
// borderRadius: 4,
// },
// statusText: {
// fontSize: 13,
// fontWeight: "600",
// },
// // ─── Info panel ───────────────────────────────────────────────────────────
// infoPanel: {
// marginHorizontal: 20,
// marginTop: 14,
// width: CAMERA_SIZE,
// backgroundColor: THEME.bgCard,
// borderRadius: 16,
// padding: 14,
// borderWidth: 1,
// borderColor: THEME.border,
// gap: 10,
// },
// infoPanelRow: {
// flexDirection: "row",
// alignItems: "center",
// gap: 8,
// },
// infoPanelText: {
// fontSize: 13,
// color: THEME.textSecondary,
// flex: 1,
// lineHeight: 18,
// },
// // ─── Bottom capture section ───────────────────────────────────────────────
// captureSection: {
// position: "absolute",
// bottom: 0,
// left: 0,
// right: 0,
// alignItems: "center",
// paddingTop: 16,
// paddingBottom: Platform.OS === "android" ? 20 : 32,
// backgroundColor: THEME.bg,
// borderTopWidth: 1,
// borderTopColor: THEME.border,
// },
// captureContainer: {
// alignItems: "center",
// gap: 10,
// },
// captureButtonOuter: {
// alignItems: "center",
// justifyContent: "center",
// },
// captureButton: {
// width: 72,
// height: 72,
// borderRadius: 36,
// alignItems: "center",
// justifyContent: "center",
// shadowColor: THEME.primary,
// shadowOffset: { width: 0, height: 6 },
// shadowOpacity: 0.4,
// shadowRadius: 16,
// elevation: 8,
// },
// captureRing: {
// position: "absolute",
// width: 86,
// height: 86,
// borderRadius: 43,
// borderWidth: 2.5,
// opacity: 0.25,
// },
// captureHintBottom: {
// fontSize: 13,
// color: THEME.textSecondary,
// fontWeight: "500",
// },
// // ─── Preview actions ──────────────────────────────────────────────────────
// previewActions: {
// flexDirection: "row",
// alignItems: "center",
// gap: 16,
// },
// retakeButton: {
// alignItems: "center",
// justifyContent: "center",
// gap: 4,
// width: 68,
// height: 68,
// borderRadius: 34,
// backgroundColor: THEME.bgInput,
// borderWidth: 2,
// borderColor: THEME.border,
// },
// retakeButtonText: {
// fontSize: 11,
// fontWeight: "600",
// color: THEME.primary,
// },
// confirmButton: {
// borderRadius: 40,
// overflow: "hidden",
// shadowColor: THEME.success,
// shadowOffset: { width: 0, height: 6 },
// shadowOpacity: 0.35,
// shadowRadius: 16,
// elevation: 8,
// },
// confirmButtonGradient: {
// flexDirection: "row",
// alignItems: "center",
// justifyContent: "center",
// gap: 8,
// paddingHorizontal: 28,
// paddingVertical: 18,
// borderRadius: 40,
// },
// confirmButtonText: {
// fontSize: 15,
// fontWeight: "700",
// color: "#fff",
// },
// // ─── Permission screen ────────────────────────────────────────────────────
// permissionContainer: {
// flex: 1,
// alignItems: "center",
// justifyContent: "center",
// padding: 24,
// },
// permissionCard: {
// alignItems: "center",
// backgroundColor: THEME.bgCard,
// borderRadius: 24,
// padding: 32,
// borderWidth: 1,
// borderColor: THEME.border,
// width: "100%",
// maxWidth: 340,
// shadowColor: THEME.shadowDark,
// shadowOffset: { width: 0, height: 4 },
// shadowOpacity: 1,
// shadowRadius: 16,
// elevation: 4,
// },
// permissionIconBg: {
// width: 96,
// height: 96,
// borderRadius: 48,
// backgroundColor: THEME.primaryBg,
// alignItems: "center",
// justifyContent: "center",
// marginBottom: 20,
// },
// permissionTitle: {
// fontSize: 20,
// fontWeight: "700",
// color: THEME.textPrimary,
// marginBottom: 12,
// textAlign: "center",
// },
// permissionDesc: {
// fontSize: 14,
// color: THEME.textSecondary,
// textAlign: "center",
// lineHeight: 22,
// marginBottom: 28,
// paddingHorizontal: 8,
// },
// permissionButton: {
// borderRadius: 14,
// overflow: "hidden",
// width: "100%",
// },
// permissionButtonGradient: {
// flexDirection: "row",
// alignItems: "center",
// justifyContent: "center",
// gap: 10,
// paddingVertical: 16,
// borderRadius: 14,
// },
// permissionButtonText: {
// color: "#fff",
// fontSize: 16,
// fontWeight: "700",
// },
// // ─── Loading ───────────────────────────────────────────────────────────────
// loadingContainer: {
// flex: 1,
// alignItems: "center",
// justifyContent: "center",
// gap: 16,
// },
// loadingText: {
// fontSize: 14,
// color: THEME.textSecondary,
// },
// });
import React, { useState, useRef, useEffect, useCallback } from "react";
import {
View,
Text,
StyleSheet,
TouchableOpacity,
Alert,
Dimensions,
Platform,
StatusBar,
Animated,
Image,
ScrollView,
ActivityIndicator,
Vibration,
Modal,
PanResponder,
} from "react-native";
import { CameraView, CameraType, useCameraPermissions } from "expo-camera";
import { Ionicons } from "@expo/vector-icons";
import { LinearGradient } from "expo-linear-gradient";
import * as ImageManipulator from "expo-image-manipulator";
import * as ImagePicker from "expo-image-picker";
import { useAuth } from "@/contexts/AuthContext";
import { API_BASE } from "@/constants/config";
// ─── Pose Types ─────────────────────────────────────────────────────────────
type PoseType = 'center' | 'right' | 'left';
interface PoseConfig {
id: PoseType;
label: string;
icon: string;
description: string;
color: string;
}
const POSES: PoseConfig[] = [
{ id: 'center', label: 'Hadap Depan', icon: '👤', description: 'Lihat kamera secara langsung', color: '#2563EB' },
{ id: 'right', label: 'Hadap Kanan', icon: '👉', description: 'Putar kepala ke kanan ~30°', color: '#8B5CF6' },
{ id: 'left', label: 'Hadap Kiri', icon: '👈', description: 'Putar kepala ke kiri ~30°', color: '#EC4899' },
];
const CAPTURES_PER_POSE = 2; // burst: 2 foto per klik
// ─── Constants ──────────────────────────────────────────────────────────────
const { width: SCREEN_WIDTH, height: SCREEN_HEIGHT } = Dimensions.get("window");
const CAMERA_SIZE = SCREEN_WIDTH * 0.85;
const OVAL_WIDTH = CAMERA_SIZE * 0.55;
const OVAL_HEIGHT = CAMERA_SIZE * 0.75;
const OVAL_TOP = (CAMERA_SIZE - OVAL_HEIGHT) / 2;
const OVAL_LEFT = (CAMERA_SIZE - OVAL_WIDTH) / 2;
// Crop Editor constants
const EDITOR_SIZE = SCREEN_WIDTH * 0.9;
const EDITOR_OVAL_WIDTH = EDITOR_SIZE * 0.6;
const EDITOR_OVAL_HEIGHT = EDITOR_SIZE * 0.8;
// ─── Theme Colors ───────────────────────────────────────────────────────────
const THEME = {
bg: "#FFFFFF",
bgSecondary: "#F8FAFC",
bgCard: "#FFFFFF",
bgInput: "#F1F5F9",
border: "#E2E8F0",
borderLight: "#F1F5F9",
textPrimary: "#1E293B",
textSecondary: "#64748B",
textMuted: "#94A3B8",
primary: "#2563EB",
primaryLight: "#3B82F6",
primaryDark: "#1D4ED8",
primaryBg: "#EFF6FF",
primaryBgDark: "#DBEAFE",
success: "#10B981",
successBg: "#ECFDF5",
warning: "#F59E0B",
warningBg: "#FFFBEB",
danger: "#EF4444",
dangerBg: "#FEF2F2",
shadow: "rgba(0, 0, 0, 0.06)",
shadowDark: "rgba(0, 0, 0, 0.08)",
};
// ─── Oval Mask Overlay ──────────────────────────────────────────────────────
function OvalMaskOverlay() {
return ;
}
// ─── Crop Editor Modal Component ───────────────────────────────────────────
// Approach: fixed oval mask, user drags/zooms the IMAGE behind it.
// This ensures what-you-see-is-what-you-get when confirming the crop.
interface CropEditorProps {
visible: boolean;
imageUri: string;
onConfirm: (croppedUri: string) => void;
onCancel: () => void;
}
function CropEditor({ visible, imageUri, onConfirm, onCancel }: CropEditorProps) {
const [isProcessing, setIsProcessing] = useState(false);
// Original image pixel dimensions
const [originalWidth, setOriginalWidth] = useState(0);
const [originalHeight, setOriginalHeight] = useState(0);
// Display dimensions. The image uses COVER behavior so the oval is always filled.
const [displayWidth, setDisplayWidth] = useState(EDITOR_SIZE);
const [displayHeight, setDisplayHeight] = useState(EDITOR_SIZE);
// Image offset from the centered position
const [imageOffsetX, setImageOffsetX] = useState(0);
const [imageOffsetY, setImageOffsetY] = useState(0);
const [scale, setScale] = useState(1);
// IMPORTANT: refs are the single source of truth for gesture/crop values.
// Using React state directly during handleConfirm can read one render behind
// when the user presses the check button immediately after dragging.
const offsetXRef = useRef(0);
const offsetYRef = useRef(0);
const scaleRef = useRef(1);
const displayWRef = useRef(EDITOR_SIZE);
const displayHRef = useRef(EDITOR_SIZE);
const gestureStartOffset = useRef({ x: 0, y: 0 });
// Load the original image dimensions and calculate the initial COVER size.
useEffect(() => {
if (!imageUri) return;
Image.getSize(
imageUri,
(w, h) => {
setOriginalWidth(w);
setOriginalHeight(h);
const fitScale = Math.max(
EDITOR_SIZE / w,
EDITOR_SIZE / h
);
const dw = w * fitScale;
const dh = h * fitScale;
setDisplayWidth(dw);
setDisplayHeight(dh);
displayWRef.current = dw;
displayHRef.current = dh;
},
(error) => console.warn("Failed to get image size:", error)
);
}, [imageUri]);
// Keep the oval completely inside the displayed image.
const clampOffset = useCallback(
(ox: number, oy: number, currentScale: number) => {
const scaledW = displayWRef.current * currentScale;
const scaledH = displayHRef.current * currentScale;
const maxOffsetX = Math.max(
0,
(scaledW - EDITOR_OVAL_WIDTH) / 2
);
const maxOffsetY = Math.max(
0,
(scaledH - EDITOR_OVAL_HEIGHT) / 2
);
return {
x: Math.max(-maxOffsetX, Math.min(maxOffsetX, ox)),
y: Math.max(-maxOffsetY, Math.min(maxOffsetY, oy)),
};
},
[]
);
// PanResponder moves the image behind the fixed oval.
const panResponder = useRef(
PanResponder.create({
onStartShouldSetPanResponder: () => true,
onMoveShouldSetPanResponder: (_, gs) =>
Math.abs(gs.dx) > 2 || Math.abs(gs.dy) > 2,
onPanResponderGrant: () => {
gestureStartOffset.current = {
x: offsetXRef.current,
y: offsetYRef.current,
};
},
onPanResponderMove: (_, gestureState) => {
const newX =
gestureStartOffset.current.x + gestureState.dx;
const newY =
gestureStartOffset.current.y + gestureState.dy;
const clamped = clampOffset(
newX,
newY,
scaleRef.current
);
// Update refs FIRST. These values are used by handleConfirm.
offsetXRef.current = clamped.x;
offsetYRef.current = clamped.y;
// State is only for rendering.
setImageOffsetX(clamped.x);
setImageOffsetY(clamped.y);
},
onPanResponderRelease: () => {},
onPanResponderTerminate: () => {},
})
).current;
const applyZoom = useCallback(
(newScale: number) => {
const safeScale = Math.max(1, Math.min(3, newScale));
const clamped = clampOffset(
offsetXRef.current,
offsetYRef.current,
safeScale
);
offsetXRef.current = clamped.x;
offsetYRef.current = clamped.y;
scaleRef.current = safeScale;
setImageOffsetX(clamped.x);
setImageOffsetY(clamped.y);
setScale(safeScale);
},
[clampOffset]
);
const zoomIn = () => {
applyZoom(scaleRef.current + 0.2);
};
const zoomOut = () => {
applyZoom(scaleRef.current - 0.2);
};
const resetPosition = () => {
offsetXRef.current = 0;
offsetYRef.current = 0;
scaleRef.current = 1;
setImageOffsetX(0);
setImageOffsetY(0);
setScale(1);
};
const handleConfirm = async () => {
if (originalWidth <= 0 || originalHeight <= 0) {
Alert.alert("Error", "Ukuran foto belum siap. Silakan coba lagi.");
return;
}
setIsProcessing(true);
try {
// ================================================================
// IMPORTANT:
// Read ALL current gesture values from refs, not React state.
// This prevents the crop from using an older position when the
// user presses ✓ immediately after dragging/zooming.
// ================================================================
const currentOffsetX = offsetXRef.current;
const currentOffsetY = offsetYRef.current;
const currentScale = scaleRef.current;
const currentDisplayWidth = displayWRef.current;
const currentDisplayHeight = displayHRef.current;
const scaledWidth = currentDisplayWidth * currentScale;
const scaledHeight = currentDisplayHeight * currentScale;
// Fixed oval position inside the editor.
const ovalLeft =
(EDITOR_SIZE - EDITOR_OVAL_WIDTH) / 2;
const ovalTop =
(EDITOR_SIZE - EDITOR_OVAL_HEIGHT) / 2;
// Current image top-left inside the editor.
const imageLeft =
(EDITOR_SIZE - scaledWidth) / 2 + currentOffsetX;
const imageTop =
(EDITOR_SIZE - scaledHeight) / 2 + currentOffsetY;
// Oval position relative to the image.
const cropXOnDisplay = ovalLeft - imageLeft;
const cropYOnDisplay = ovalTop - imageTop;
// Convert displayed pixels back to ORIGINAL image pixels.
const originalPerDisplayX =
originalWidth / scaledWidth;
const originalPerDisplayY =
originalHeight / scaledHeight;
let cropOriginX = Math.round(
cropXOnDisplay * originalPerDisplayX
);
let cropOriginY = Math.round(
cropYOnDisplay * originalPerDisplayY
);
let cropWidth = Math.round(
EDITOR_OVAL_WIDTH * originalPerDisplayX
);
let cropHeight = Math.round(
EDITOR_OVAL_HEIGHT * originalPerDisplayY
);
// Clamp to the original image bounds.
cropOriginX = Math.max(
0,
Math.min(cropOriginX, originalWidth - 1)
);
cropOriginY = Math.max(
0,
Math.min(cropOriginY, originalHeight - 1)
);
cropWidth = Math.max(
10,
Math.min(cropWidth, originalWidth - cropOriginX)
);
cropHeight = Math.max(
10,
Math.min(cropHeight, originalHeight - cropOriginY)
);
const cropRegion = {
originX: cropOriginX,
originY: cropOriginY,
width: cropWidth,
height: cropHeight,
};
console.log("=== CROP DEBUG ===");
console.log({
originalWidth,
originalHeight,
currentDisplayWidth,
currentDisplayHeight,
currentScale,
currentOffsetX,
currentOffsetY,
scaledWidth,
scaledHeight,
imageLeft,
imageTop,
ovalLeft,
ovalTop,
cropXOnDisplay,
cropYOnDisplay,
cropRegion,
});
const result = await ImageManipulator.manipulateAsync(
imageUri,
[
{ crop: cropRegion },
{ resize: { width: 800 } },
],
{
compress: 0.9,
format: ImageManipulator.SaveFormat.JPEG,
}
);
onConfirm(result.uri);
} catch (error) {
console.error("Crop error:", error);
Alert.alert(
"Error",
"Gagal memproses foto. Silakan coba lagi."
);
} finally {
setIsProcessing(false);
}
};
// Reset state whenever a new image/modal is opened.
useEffect(() => {
if (!visible) return;
offsetXRef.current = 0;
offsetYRef.current = 0;
scaleRef.current = 1;
setImageOffsetX(0);
setImageOffsetY(0);
setScale(1);
}, [visible, imageUri]);
return (
Atur Posisi Wajah
{isProcessing ? (
) : (
)}
Geser foto untuk menyesuaikan posisi wajah di dalam oval
{/*
Image is rendered with the exact same dimensions used by
handleConfirm() to calculate the crop coordinates.
This creates a 1:1 relationship between the visible oval
and the final cropped image.
*/}
{/* Dark overlay with transparent oval cutout */}
{Math.round(scale * 100)}%
Reset
);
}
const OVAL_TOP_OFFSET = (EDITOR_SIZE - EDITOR_OVAL_HEIGHT) / 2;
const OVAL_LEFT_OFFSET = (EDITOR_SIZE - EDITOR_OVAL_WIDTH) / 2;
const cropStyles = StyleSheet.create({
modalContainer: {
flex: 1,
backgroundColor: '#000',
},
header: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingTop: Platform.OS === 'android' ? (StatusBar.currentHeight ?? 0) + 10 : 50,
paddingHorizontal: 16,
paddingBottom: 12,
backgroundColor: 'rgba(0,0,0,0.9)',
},
headerButton: {
width: 44,
height: 44,
alignItems: 'center',
justifyContent: 'center',
},
headerTitle: {
fontSize: 18,
fontWeight: '700',
color: '#fff',
},
instructionContainer: {
paddingHorizontal: 20,
paddingVertical: 12,
backgroundColor: 'rgba(0,0,0,0.7)',
},
instructionText: {
fontSize: 14,
color: 'rgba(255,255,255,0.8)',
textAlign: 'center',
},
editorContainer: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
padding: 10,
},
imageWrapper: {
width: EDITOR_SIZE,
height: EDITOR_SIZE,
position: 'relative',
alignItems: 'center',
justifyContent: 'center',
overflow: 'hidden',
borderRadius: 12,
},
fullImage: {
position: 'absolute',
},
// Dark overlay pieces around the oval cutout
overlayContainer: {
...StyleSheet.absoluteFillObject,
},
darkBandHorizontal: {
width: '100%',
height: OVAL_TOP_OFFSET,
backgroundColor: 'rgba(0,0,0,0.55)',
},
middleRow: {
flexDirection: 'row',
height: EDITOR_OVAL_HEIGHT,
},
darkBandVertical: {
width: OVAL_LEFT_OFFSET,
height: '100%',
backgroundColor: 'rgba(0,0,0,0.55)',
},
ovalHole: {
width: EDITOR_OVAL_WIDTH,
height: EDITOR_OVAL_HEIGHT,
borderRadius: EDITOR_OVAL_WIDTH / 2,
overflow: 'hidden',
// transparent — lets the image show through
},
ovalBorder: {
...StyleSheet.absoluteFillObject,
borderRadius: EDITOR_OVAL_WIDTH / 2,
borderWidth: 3,
borderColor: '#fff',
},
controlsContainer: {
paddingHorizontal: 20,
paddingVertical: 20,
backgroundColor: 'rgba(0,0,0,0.9)',
borderTopWidth: 1,
borderTopColor: 'rgba(255,255,255,0.1)',
},
zoomControls: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
gap: 24,
marginBottom: 16,
},
zoomButton: {
width: 50,
height: 50,
borderRadius: 25,
backgroundColor: 'rgba(255,255,255,0.15)',
alignItems: 'center',
justifyContent: 'center',
},
zoomLabel: {
fontSize: 16,
fontWeight: '600',
color: '#fff',
minWidth: 60,
textAlign: 'center',
},
resetButton: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
gap: 8,
paddingVertical: 12,
paddingHorizontal: 24,
backgroundColor: 'rgba(255,255,255,0.1)',
borderRadius: 12,
},
resetText: {
fontSize: 14,
color: THEME.textSecondary,
fontWeight: '500',
},
});
// ─── Main Component ────────────────────────────────────────────────────────────
export default function RegisterFotoScreen() {
const { user, token } = useAuth();
// ── State ─────────────────────────────────────────────────────────────────
const [permission, requestPermission] = useCameraPermissions();
const [facing] = useState("front");
const [isCapturing, setIsCapturing] = useState(false);
const [isSubmitting, setIsSubmitting] = useState(false);
const [capturedImages, setCapturedImages] = useState([]);
const [showPreview, setShowPreview] = useState(false);
const [captureAnim] = useState(new Animated.Value(1));
const [flashAnim] = useState(new Animated.Value(0));
const [isAlreadyRegistered, setIsAlreadyRegistered] = useState(false);
const [isCheckingStatus, setIsCheckingStatus] = useState(true);
const cameraRef = useRef(null);
// ── Crop Editor State ─────────────────────────────────────────────────────
const [showCropEditor, setShowCropEditor] = useState(false);
const [selectedImageUri, setSelectedImageUri] = useState('');
// ── Guided Pose State ─────────────────────────────────────────────────────
const [currentPoseIndex, setCurrentPoseIndex] = useState(0);
const currentPose = POSES[currentPoseIndex];
const totalRequired = POSES.length * CAPTURES_PER_POSE;
const allCaptured = capturedImages.length >= totalRequired;
// ── Reset ─────────────────────────────────────────────────────────────────
const resetCapture = useCallback(() => {
setCapturedImages([]);
setCurrentPoseIndex(0);
setShowPreview(false);
}, []);
// ── Check registration status ─────────────────────────────────────────────
useEffect(() => {
const checkRegistrationStatus = async () => {
if (!token || !user?.nim) {
setIsCheckingStatus(false);
return;
}
try {
const response = await fetch(`${API_BASE}/arcface/status/${user.nim}`, {
method: "GET",
headers: { "Authorization": `Bearer ${token}` },
});
if (response.ok || response.status === 200) {
const result = await response.json();
setIsAlreadyRegistered(result.is_registered === true);
} else {
setIsAlreadyRegistered(false);
}
} catch (error) {
console.warn("Gagal cek status registrasi:", error);
setIsAlreadyRegistered(false);
} finally {
setIsCheckingStatus(false);
}
};
checkRegistrationStatus();
}, [token, user?.nim]);
// ── Crop to oval region ───────────────────────────────────────────────────
const cropToOvalRegion = async (
photoUri: string,
photoWidth: number,
photoHeight: number,
isFront: boolean
): Promise => {
try {
const containerSize = CAMERA_SIZE;
const scale = Math.max(containerSize / photoWidth, containerSize / photoHeight);
const displayedWidth = photoWidth * scale;
const displayedHeight = photoHeight * scale;
const offsetX = (displayedWidth - containerSize) / 2;
const offsetY = (displayedHeight - containerSize) / 2;
let originX = (OVAL_LEFT + offsetX) / scale;
let originY = (OVAL_TOP + offsetY) / scale;
let width = OVAL_WIDTH / scale;
let height = OVAL_HEIGHT / scale;
originX = Math.max(0, Math.min(originX, photoWidth - 1));
originY = Math.max(0, Math.min(originY, photoHeight - 1));
width = Math.max(1, Math.min(width, photoWidth - originX));
height = Math.max(1, Math.min(height, photoHeight - originY));
const cropRegion = {
originX: Math.round(originX),
originY: Math.round(originY),
width: Math.round(width),
height: Math.round(height),
};
const actions: ImageManipulator.Action[] = [];
if (isFront) {
actions.push({ flip: ImageManipulator.FlipType.Horizontal });
}
actions.push({ crop: cropRegion });
actions.push({ resize: { width: 800 } });
const manipResult = await ImageManipulator.manipulateAsync(
photoUri,
actions,
{ compress: 0.9, format: ImageManipulator.SaveFormat.JPEG }
);
return manipResult.uri;
} catch (error) {
console.warn("Gagal melakukan crop oval:", error);
return photoUri;
}
};
// ── Burst capture: 1 klik = 2 foto ───────────────────────────────────────
const handleBurstCapture = async () => {
if (!cameraRef.current || isCapturing || allCaptured) return;
// Capture animation
Animated.sequence([
Animated.timing(captureAnim, { toValue: 0.92, duration: 80, useNativeDriver: true }),
Animated.spring(captureAnim, { toValue: 1, friction: 4, useNativeDriver: true }),
]).start();
setIsCapturing(true);
try {
const newPhotos: string[] = [];
for (let i = 0; i < CAPTURES_PER_POSE; i++) {
// Flash effect
Animated.sequence([
Animated.timing(flashAnim, { toValue: 1, duration: 60, useNativeDriver: true }),
Animated.timing(flashAnim, { toValue: 0, duration: 150, useNativeDriver: true }),
]).start();
const photo = await cameraRef.current.takePictureAsync({
quality: 0.85,
shutterSound: false,
});
if (photo?.uri) {
const croppedUri = photo.width && photo.height
? await cropToOvalRegion(photo.uri, photo.width, photo.height, facing === "front")
: photo.uri;
newPhotos.push(croppedUri);
Vibration.vibrate(30);
}
// Jeda singkat antara 2 foto burst
if (i < CAPTURES_PER_POSE - 1) {
await new Promise(resolve => setTimeout(resolve, 400));
}
}
// Update state
setCapturedImages(prev => [...prev, ...newPhotos]);
// Pindah ke pose berikutnya atau selesai
if (currentPoseIndex < POSES.length - 1) {
setCurrentPoseIndex(prev => prev + 1);
}
// Jika ini pose terakhir, allCaptured akan jadi true di render berikutnya
} catch (error) {
Alert.alert("Error", "Gagal mengambil foto. Silakan coba lagi.");
} finally {
setIsCapturing(false);
}
};
const handleRetake = () => {
resetCapture();
};
// ── Handle Delete ─────────────────────────────────────────────────────────
const handleDelete = async () => {
if (!user?.nim || !token) {
Alert.alert("Error", "Silakan login terlebih dahulu.");
return;
}
Alert.alert(
"Hapus Registrasi?",
`Apakah Anda yakin ingin menghapus data wajah untuk NIM ${user.nim}? Anda perlu registrasi ulang di kemudian hari.`,
[
{ text: "Batal", style: "cancel" },
{
text: "Hapus",
style: "destructive",
onPress: async () => {
try {
const response = await fetch(`${API_BASE}/arcface/delete/${user.nim}`, {
method: "DELETE",
headers: { "Authorization": `Bearer ${token}` },
});
const result = await response.json();
if (result.success) {
setIsAlreadyRegistered(false);
Alert.alert("Berhasil", "Data wajah berhasil dihapus.");
} else {
Alert.alert("Gagal", result.message || "Gagal menghapus data wajah.");
}
} catch (error) {
Alert.alert("Error", "Terjadi kesalahan koneksi.");
}
},
},
]
);
};
// ── Handle Confirm / Submit ───────────────────────────────────────────────
const handleConfirm = async () => {
if (capturedImages.length === 0) {
Alert.alert("Error", "Tidak ada foto yang akan dikirim.");
return;
}
if (!user?.nim) {
Alert.alert("Error", "Silakan login terlebih dahulu untuk melakukan registrasi wajah.");
return;
}
if (isAlreadyRegistered) {
Alert.alert(
"Perbarui Foto?",
`Wajah Anda sudah terdaftar. Foto lama akan ditimpa dengan foto baru.\n\nApakah ingin melanjutkan?`,
[
{ text: "Batal", style: "cancel" },
{ text: "Lanjutkan", onPress: () => performEnrollment(true) },
]
);
} else {
performEnrollment(false);
}
};
const performEnrollment = async (forceUpdate: boolean) => {
setIsSubmitting(true);
try {
const photosBase64: string[] = [];
for (const photoUri of capturedImages) {
const base64Response = await fetch(photoUri);
const blob = await base64Response.blob();
const reader = new FileReader();
const base64 = await new Promise((resolve, reject) => {
reader.onloadend = () => resolve(reader.result as string);
reader.onerror = () => reject(new Error("Failed to read image"));
reader.readAsDataURL(blob);
});
photosBase64.push(base64);
}
const response = await fetch(`${API_BASE}/arcface/enroll`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${token}`,
},
body: JSON.stringify({
nim: user!.nim,
foto_base64: photosBase64,
force_update: forceUpdate,
}),
});
const result = await response.json();
if (result.success) {
setIsAlreadyRegistered(true);
Alert.alert(
forceUpdate ? "Berhasil Diperbarui! 🎉" : "Berhasil! 🎉",
forceUpdate
? `Wajah ${user!.nama_mahasiswa || user!.name} (${user!.nim}) berhasil diperbarui!\n\nData lama telah ditimpa.`
: `Wajah ${user!.nama_mahasiswa || user!.name} (${user!.nim}) berhasil terdaftar!\n\nEmbedding tersimpan di: face_mahasiswa/${user!.nim}/`,
[{ text: "OK", onPress: handleRetake }]
);
} else {
if (result.message?.includes("sudah ter-enroll")) {
if (forceUpdate) {
setIsAlreadyRegistered(true);
Alert.alert(
"Berhasil Diperbarui! 🎉",
`Wajah ${user!.nama_mahasiswa || user!.name} (${user!.nim}) berhasil diperbarui!\n\nData lama telah ditimpa.`,
[{ text: "OK", onPress: handleRetake }]
);
} else {
Alert.alert(
"Info",
"Wajah Anda sudah terdaftar sebelumnya.\n\nJika ingin memperbarui foto, silakan ambil foto baru dan pilih 'Perbarui'.",
[{ text: "OK", onPress: handleRetake }]
);
}
} else if (result.message?.includes("ArcFace model")) {
Alert.alert(
"Model Error",
"Sistem Face Recognition belum siap. Hubungi admin untuk mengaktifkan model ArcFace.",
[{ text: "OK" }]
);
} else {
Alert.alert("Gagal", result.message || "Gagal menyimpan foto wajah.");
}
}
} catch (error) {
console.error("Enrollment error:", error);
Alert.alert("Error", "Gagal mengirim foto. Periksa koneksi internet dan coba lagi.");
} finally {
setIsSubmitting(false);
}
};
// ─── Render: Permission Loading ───────────────────────────────────────────
if (!permission) {
return (
Memuat kamera...
);
}
if (!permission.granted) {
return (
Izinkan Akses Kamera
Kami membutuhkan akses kamera untuk mengambil foto wajah Anda untuk verifikasi kehadiran.
Izinkan Kamera
);
}
// ─── Render: Main Screen ─────────────────────────────────────────────────
return (
{/* Header */}
{isAlreadyRegistered ? "Update Wajah" : "Register Wajah"}
{isCheckingStatus
? "Memuat..."
: isAlreadyRegistered
? "✓ Sudah terdaftar"
: "Ambil foto untuk verifikasi"}
{/* Gallery Button - Select one by one with crop editor */}
{
if (allCaptured || showPreview) return;
const remaining = totalRequired - capturedImages.length;
if (remaining <= 0) return;
try {
const result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ["images"],
allowsMultipleSelection: false,
quality: 1,
});
if (!result.canceled && result.assets.length > 0) {
const selectedPhoto = result.assets[0];
setSelectedImageUri(selectedPhoto.uri);
setShowCropEditor(true);
}
} catch (err) {
Alert.alert("Error", "Gagal memilih gambar dari galeri");
}
}}
>
{/* Delete Button */}
{isAlreadyRegistered && (
)}
{/* Scrollable body */}
{/* Pose guide — DI ATAS oval, tidak menghalangi wajah */}
{!showPreview && !allCaptured && (
{currentPose.icon}
{currentPose.label}
{currentPose.description}
)}
{/* Camera wrapper */}
{(showPreview || allCaptured) && capturedImages.length > 0 ? (
/* Tampilkan foto depan (pertama) sebagai preview utama di oval */
) : (
<>
{/* Flash overlay */}
>
)}
{!showPreview && !allCaptured && }
{(showPreview || allCaptured) && (
{allCaptured ? `${capturedImages.length} foto selesai` : "Foto Tertangkap"}
)}
{/* Thumbnails — di bawah oval, tampilkan semua foto kecil-kecil */}
{capturedImages.length > 0 && (showPreview || allCaptured) && (
{capturedImages.map((uri, idx) => (
{idx < 2 ? 'Depan' : idx < 4 ? 'Kanan' : 'Kiri'} {(idx % 2) + 1}
))}
)}
{/* Pose progress stepper */}
{POSES.map((pose, idx) => {
const isDone = idx < currentPoseIndex || (idx === currentPoseIndex && allCaptured);
const isCurrent = idx === currentPoseIndex && !allCaptured;
return (
{isDone && }
{pose.label.replace('Hadap ', '')}
{idx < POSES.length - 1 && (
)}
);
})}
{/* Status badge */}
{showPreview
? `${capturedImages.length} foto siap dikirim`
: allCaptured
? "Semua pose selesai! Ketuk untuk simpan"
: isCapturing
? "📸 Mengambil 2 foto..."
: `${currentPose.label} — Ketuk tombol untuk ambil foto`}
{/* Instructions */}
{showPreview || allCaptured
? "6 foto siap dikirim ke server"
: "Tekan tombol kamera atau galeri untuk ambil foto"}
Tombol galeri: pilih foto satu per satu, di-crop otomatis sesuai oval
{allCaptured ? 'Semua 3 pose sudah diambil' : `Langkah ${currentPoseIndex + 1}/3: ${currentPose.description}`}
Lepas kacamata, topi, atau masker
{/* Bottom Action — fixed */}
{showPreview ? (
Ulangi
{isSubmitting ? (
<>
Mengirim...
>
) : (
<>
Simpan {capturedImages.length} Foto
>
)}
) : (
setShowPreview(true) : handleBurstCapture}
disabled={isCapturing}
activeOpacity={0.85}
>
{isCapturing ? (
) : allCaptured ? (
) : (
)}
{isCapturing
? "Mengambil 2 foto..."
: allCaptured
? `${capturedImages.length} foto — Ketuk untuk preview`
: `Ketuk untuk ambil foto ${currentPose.label}`}
)}
{/* Crop Editor Modal */}
{
setShowCropEditor(false);
setCapturedImages(prev => [...prev, croppedUri]);
const newCount = capturedImages.length + 1;
const nextPose = Math.floor(newCount / CAPTURES_PER_POSE);
// Update pose index
if (newCount % CAPTURES_PER_POSE === 0 && nextPose < POSES.length) {
setCurrentPoseIndex(Math.min(nextPose, POSES.length - 1));
}
// If all 6 photos, go to preview
if (newCount >= totalRequired) {
setShowPreview(true);
setCurrentPoseIndex(POSES.length - 1);
}
}}
onCancel={() => {
setShowCropEditor(false);
setSelectedImageUri('');
}}
/>
);
}
// ─── Styles ─────────────────────────────────────────────────────────────────
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: THEME.bg,
},
// ─── Header ───────────────────────────────────────────────────────────────
header: {
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between",
paddingTop: Platform.OS === "android" ? (StatusBar.currentHeight ?? 0) + 10 : 55,
paddingHorizontal: 20,
paddingBottom: 14,
backgroundColor: THEME.bg,
borderBottomWidth: 1,
borderBottomColor: THEME.border,
},
headerLeft: {
flexDirection: "row",
alignItems: "center",
gap: 12,
},
headerIconContainer: {
width: 42,
height: 42,
borderRadius: 12,
backgroundColor: THEME.primaryBg,
alignItems: "center",
justifyContent: "center",
},
headerTitle: {
fontSize: 18,
fontWeight: "800",
color: THEME.textPrimary,
letterSpacing: 0.2,
},
headerSubtitle: {
fontSize: 12,
color: THEME.primary,
marginTop: 1,
},
headerRightButtons: {
flexDirection: "row",
alignItems: "center",
gap: 8,
},
iconButton: {
width: 42,
height: 42,
borderRadius: 12,
backgroundColor: THEME.bgInput,
alignItems: "center",
justifyContent: "center",
borderWidth: 1,
borderColor: THEME.border,
},
deleteButton: {
borderColor: `${THEME.danger}40`,
backgroundColor: `${THEME.danger}10`,
},
// ─── Scroll body ──────────────────────────────────────────────────────────
scrollView: {
flex: 1,
},
scrollContent: {
alignItems: "center",
paddingTop: 16,
paddingBottom: 160,
},
// ─── Camera area ──────────────────────────────────────────────────────────
cameraWrapper: {
alignSelf: "center",
width: CAMERA_SIZE,
height: CAMERA_SIZE,
position: "relative",
borderRadius: 24,
backgroundColor: "#FFFFFF",
},
ovalClip: {
position: "absolute",
top: OVAL_TOP,
left: OVAL_LEFT,
width: OVAL_WIDTH,
height: OVAL_HEIGHT,
borderRadius: OVAL_WIDTH / 2,
overflow: "hidden",
backgroundColor: THEME.bgSecondary,
},
cameraInner: {
position: "absolute",
top: -OVAL_TOP,
left: -OVAL_LEFT,
width: CAMERA_SIZE,
height: CAMERA_SIZE,
},
previewImage: {
width: "100%",
height: "100%",
},
ovalRing: {
position: "absolute",
top: OVAL_TOP,
left: OVAL_LEFT,
width: OVAL_WIDTH,
height: OVAL_HEIGHT,
borderRadius: OVAL_WIDTH / 2,
borderWidth: 3,
borderColor: "#FFFFFF",
zIndex: 10,
},
flashOverlay: {
...StyleSheet.absoluteFillObject,
backgroundColor: "#FFFFFF",
zIndex: 12,
},
// ─── Pose guide (above oval) ────────────────────────────────────────────────
poseGuideAbove: {
alignItems: "center",
marginBottom: 12,
},
poseGuideBox: {
backgroundColor: "rgba(0,0,0,0.82)",
paddingHorizontal: 20,
paddingVertical: 12,
borderRadius: 16,
alignItems: "center",
borderWidth: 2,
gap: 3,
},
poseIcon: {
fontSize: 26,
},
poseLabel: {
fontSize: 16,
fontWeight: "700",
color: "#FFFFFF",
},
poseDesc: {
fontSize: 12,
color: "rgba(255,255,255,0.75)",
},
// ─── Preview badge ────────────────────────────────────────────────────────
previewBadge: {
position: "absolute",
top: 12,
right: 12,
flexDirection: "row",
alignItems: "center",
gap: 6,
backgroundColor: "rgba(255,255,255,0.95)",
paddingHorizontal: 10,
paddingVertical: 5,
borderRadius: 20,
borderWidth: 1,
borderColor: THEME.border,
zIndex: 20,
},
previewBadgeText: {
fontSize: 11,
fontWeight: "600",
color: THEME.success,
},
// ─── Thumbnail row (below oval) ───────────────────────────────────────────
thumbnailRow: {
flexDirection: "row",
justifyContent: "center",
flexWrap: "wrap",
gap: 10,
marginTop: 14,
paddingHorizontal: 20,
},
thumbnailWrapper: {
alignItems: "center",
gap: 4,
},
thumbnailBelow: {
width: 52,
height: 64,
borderRadius: 10,
borderWidth: 2,
borderColor: THEME.border,
},
thumbnailLabel: {
fontSize: 9,
fontWeight: "600",
color: THEME.textMuted,
},
// ─── Stepper ──────────────────────────────────────────────────────────────
stepperContainer: {
flexDirection: "row",
alignItems: "center",
justifyContent: "center",
marginTop: 16,
paddingHorizontal: 16,
},
stepperItem: {
alignItems: "center",
flexDirection: "row",
gap: 4,
},
stepperDot: {
width: 22,
height: 22,
borderRadius: 11,
alignItems: "center",
justifyContent: "center",
},
stepperLabel: {
fontSize: 10,
color: THEME.textMuted,
fontWeight: "500",
marginRight: 2,
},
stepperLine: {
width: 16,
height: 2,
backgroundColor: THEME.border,
marginHorizontal: 2,
borderRadius: 1,
},
// ─── Status badge ─────────────────────────────────────────────────────────
statusWrapper: {
alignItems: "center",
marginTop: 12,
paddingHorizontal: 24,
},
statusBadge: {
flexDirection: "row",
alignItems: "center",
paddingHorizontal: 18,
paddingVertical: 9,
borderRadius: 50,
borderWidth: 1,
gap: 8,
},
statusDot: {
width: 8,
height: 8,
borderRadius: 4,
},
statusText: {
fontSize: 13,
fontWeight: "600",
},
// ─── Info panel ───────────────────────────────────────────────────────────
infoPanel: {
marginHorizontal: 20,
marginTop: 14,
width: CAMERA_SIZE,
backgroundColor: THEME.bgCard,
borderRadius: 16,
padding: 14,
borderWidth: 1,
borderColor: THEME.border,
gap: 10,
},
infoPanelRow: {
flexDirection: "row",
alignItems: "center",
gap: 8,
},
infoPanelText: {
fontSize: 13,
color: THEME.textSecondary,
flex: 1,
lineHeight: 18,
},
// ─── Bottom capture section ───────────────────────────────────────────────
captureSection: {
position: "absolute",
bottom: 0,
left: 0,
right: 0,
alignItems: "center",
paddingTop: 16,
paddingBottom: Platform.OS === "android" ? 20 : 32,
backgroundColor: THEME.bg,
borderTopWidth: 1,
borderTopColor: THEME.border,
},
captureContainer: {
alignItems: "center",
gap: 10,
},
captureButtonOuter: {
alignItems: "center",
justifyContent: "center",
},
captureButton: {
width: 72,
height: 72,
borderRadius: 36,
alignItems: "center",
justifyContent: "center",
shadowColor: THEME.primary,
shadowOffset: { width: 0, height: 6 },
shadowOpacity: 0.4,
shadowRadius: 16,
elevation: 8,
},
captureRing: {
position: "absolute",
width: 86,
height: 86,
borderRadius: 43,
borderWidth: 2.5,
opacity: 0.25,
},
captureHintBottom: {
fontSize: 13,
color: THEME.textSecondary,
fontWeight: "500",
},
// ─── Preview actions ──────────────────────────────────────────────────────
previewActions: {
flexDirection: "row",
alignItems: "center",
gap: 16,
},
retakeButton: {
alignItems: "center",
justifyContent: "center",
gap: 4,
width: 68,
height: 68,
borderRadius: 34,
backgroundColor: THEME.bgInput,
borderWidth: 2,
borderColor: THEME.border,
},
retakeButtonText: {
fontSize: 11,
fontWeight: "600",
color: THEME.primary,
},
confirmButton: {
borderRadius: 40,
overflow: "hidden",
shadowColor: THEME.success,
shadowOffset: { width: 0, height: 6 },
shadowOpacity: 0.35,
shadowRadius: 16,
elevation: 8,
},
confirmButtonGradient: {
flexDirection: "row",
alignItems: "center",
justifyContent: "center",
gap: 8,
paddingHorizontal: 28,
paddingVertical: 18,
borderRadius: 40,
},
confirmButtonText: {
fontSize: 15,
fontWeight: "700",
color: "#fff",
},
// ─── Permission screen ────────────────────────────────────────────────────
permissionContainer: {
flex: 1,
alignItems: "center",
justifyContent: "center",
padding: 24,
},
permissionCard: {
alignItems: "center",
backgroundColor: THEME.bgCard,
borderRadius: 24,
padding: 32,
borderWidth: 1,
borderColor: THEME.border,
width: "100%",
maxWidth: 340,
shadowColor: THEME.shadowDark,
shadowOffset: { width: 0, height: 4 },
shadowOpacity: 1,
shadowRadius: 16,
elevation: 4,
},
permissionIconBg: {
width: 96,
height: 96,
borderRadius: 48,
backgroundColor: THEME.primaryBg,
alignItems: "center",
justifyContent: "center",
marginBottom: 20,
},
permissionTitle: {
fontSize: 20,
fontWeight: "700",
color: THEME.textPrimary,
marginBottom: 12,
textAlign: "center",
},
permissionDesc: {
fontSize: 14,
color: THEME.textSecondary,
textAlign: "center",
lineHeight: 22,
marginBottom: 28,
paddingHorizontal: 8,
},
permissionButton: {
borderRadius: 14,
overflow: "hidden",
width: "100%",
},
permissionButtonGradient: {
flexDirection: "row",
alignItems: "center",
justifyContent: "center",
gap: 10,
paddingVertical: 16,
borderRadius: 14,
},
permissionButtonText: {
color: "#fff",
fontSize: 16,
fontWeight: "700",
},
// ─── Loading ───────────────────────────────────────────────────────────────
loadingContainer: {
flex: 1,
alignItems: "center",
justifyContent: "center",
gap: 16,
},
loadingText: {
fontSize: 14,
color: THEME.textSecondary,
},
});