Skip to content

Commit

Permalink
fix: manual flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0rgal committed Dec 19, 2024
1 parent 83d0d88 commit c2bc0c4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
25 changes: 21 additions & 4 deletions components/library/NewMeal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import { useMealsDatabase } from "../../shared/MealsStorageContext";
import { calculateCalories } from "../../utils/food";
import { v4 as uuidv4 } from "uuid";
import { MealAnalysis, MealTemplate } from "../../types";
import { useSingleImagePicker } from "../../hooks/useSingleImagePicker";
import { useNavigation } from "@react-navigation/native";
import type { BottomTabNavigationProp } from "@react-navigation/bottom-tabs";
import { MainTabParamList } from "../../navigation/types";

interface NewMealFormData {
imageUri?: string;
Expand All @@ -28,14 +32,15 @@ interface NewMealFormData {
}

interface NewMealProps {
onPress: () => Promise<void>;
prefilledMeal?: MealTemplate;
}

const NewMeal: React.FC<NewMealProps> = ({ onPress, prefilledMeal }) => {
const NewMeal: React.FC<NewMealProps> = ({ prefilledMeal }) => {
const [formData, setFormData] = useState<NewMealFormData>({});
const { insertMeal } = useMealsDatabase();
const scheme = useColorScheme();
const { pickImage } = useSingleImagePicker();
const navigation = useNavigation<BottomTabNavigationProp<MainTabParamList>>();

useEffect(() => {
if (prefilledMeal) {
Expand Down Expand Up @@ -111,19 +116,28 @@ const NewMeal: React.FC<NewMealProps> = ({ onPress, prefilledMeal }) => {
};

await insertMeal({
meal_id: analysis.meal_id,
image_uri: formData.imageUri,
status: "complete",
last_analysis: analysis,
created_at: Date.now(),
});

setFormData({});
Alert.alert("Success", "Meal added successfully");
navigation.navigate("Summary");
} catch (error) {
console.error("Error adding meal:", error);
Alert.alert("Error", "Failed to add meal");
}
};

const handleImageSelection = async () => {
const imageUri = await pickImage();
if (imageUri) {
setFormData((prev) => ({ ...prev, imageUri }));
}
};

const isFormValid = Boolean(
formData.imageUri &&
formData.mealName &&
Expand Down Expand Up @@ -286,7 +300,10 @@ const NewMeal: React.FC<NewMealProps> = ({ onPress, prefilledMeal }) => {
</View>

<View style={styles.imageButtonContainer}>
<TouchableOpacity style={styles.selectPhotoButton} onPress={onPress}>
<TouchableOpacity
style={styles.selectPhotoButton}
onPress={handleImageSelection}
>
<Text style={styles.buttonText}>
{formData.imageUri ? "Change Photo" : "Select Photo"}
</Text>
Expand Down
4 changes: 1 addition & 3 deletions components/screens/MealsLibrary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ const MealsLibrary = () => {
{selectedIndex === 1 ? (
<FavoriteLibrary handlePrefillMeal={handlePrefillMeal} />
) : null}
{selectedIndex === 2 ? (
<NewMeal prefilledMeal={prefilledMeal} onPress={handlePhotoSelect} />
) : null}
{selectedIndex === 2 ? <NewMeal prefilledMeal={prefilledMeal} /> : null}
</SafeAreaView>
);
};
Expand Down
37 changes: 37 additions & 0 deletions hooks/useSingleImagePicker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as ImagePicker from "expo-image-picker";
import { Alert } from "react-native";

export const useSingleImagePicker = () => {
const pickImage = async () => {
try {
const { status } =
await ImagePicker.requestMediaLibraryPermissionsAsync();

if (status !== "granted") {
Alert.alert(
"Photo Library Access Required",
"Calorily needs access to your photos. You can enable it in your iOS settings."
);
return null;
}

const result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.Images,
allowsMultipleSelection: false,
quality: 0.075,
});

if (!result.canceled && result.assets) {
return result.assets[0].uri;
}

return null;
} catch (error) {
console.error("Error picking image:", error);
Alert.alert("Error", "Failed to load photo. Please try again.");
return null;
}
};

return { pickImage };
};

0 comments on commit c2bc0c4

Please sign in to comment.