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

Issue 130 Untitled and Location #135

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
14 changes: 12 additions & 2 deletions __tests__/AddNoteRichEditor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ describe("AddNoteScreen", () => {
beforeEach(() => {
setNoteContentMock = jest.fn();
React.useState = jest.fn(() => ['', setNoteContentMock]);
wrapper = shallow(<AddNoteScreen />);
const routeMock = {
params: {
untitledNumber: 1 // or any other value that makes sense for your tests
}
};
wrapper = shallow(<AddNoteScreen route={routeMock} />);
});

afterEach(() => {
Expand All @@ -68,10 +73,15 @@ describe("AddNoteScreen", () => {
it('calls setNoteContent when the Rich Text Editor content changes', () => {
// Set up the mock function
const setNoteContentMock = jest.fn();
const routeMock = {
params: {
untitledNumber: 1 // or any other value that makes sense for your tests
}
};

// Shallow render the AddNoteScreen component and pass the mock function as a prop
// Ensure that this matches how your actual component receives the setNoteContent prop
const wrapper = shallow(<AddNoteScreen setNoteContent={setNoteContentMock} />);
const wrapper = shallow(<AddNoteScreen route = {routeMock} setNoteContent={setNoteContentMock} />);

// Simulate the content change on the Rich Text Editor component
// The selector needs to match the test ID or the component name/class
Expand Down
14 changes: 12 additions & 2 deletions __tests__/AddNoteScreen.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ describe("AddNoteScreen", () => {
beforeEach(() => {
setNoteContentMock = jest.fn();
React.useState = jest.fn(() => ['', setNoteContentMock]);
wrapper = shallow(<AddNoteScreen />);
const routeMock = {
params: {
untitledNumber: 1 // or any other value that makes sense for your tests
}
};
wrapper = shallow(<AddNoteScreen route={routeMock} />);
});

afterEach(() => {
Expand All @@ -58,10 +63,15 @@ describe("AddNoteScreen", () => {
it('calls setNoteContent when the Rich Text Editor content changes', () => {
// Set up the mock function
const setNoteContentMock = jest.fn();
const routeMock = {
params: {
untitledNumber: 1 // or any other value that makes sense for your tests
}
};

// Shallow render the AddNoteScreen component and pass the mock function as a prop
// Ensure that this matches how your actual component receives the setNoteContent prop
const wrapper = shallow(<AddNoteScreen setNoteContent={setNoteContentMock} />);
const wrapper = shallow(<AddNoteScreen route={routeMock} setNoteContent={setNoteContentMock} />);

// Simulate the content change on the Rich Text Editor component
// The selector needs to match the test ID or the component name/class
Expand Down
7 changes: 6 additions & 1 deletion __tests__/DifferentPlatformTimes.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ jest.mock('react-native/Libraries/Utilities/Platform', () => ({

describe("AddNoteScreen", () => {
it("renders without crashing", () => {
const wrapper = shallow(<AddNoteScreen />);
const routeMock = {
params: {
untitledNumber: 1 // or any other value that makes sense for your tests
}
};
const wrapper = shallow(<AddNoteScreen route={routeMock}/>);
expect(wrapper).toMatchSnapshot();
});
});
Expand Down
7 changes: 6 additions & 1 deletion __tests__/InsertImageNoteScreen.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ jest.mock('../lib/components/ThemeProvider', () => ({

describe("AddNoteScreen", () => {
it("adds image to editor", () => {
const wrapper = shallow(<AddNoteScreen />);
const routeMock = {
params: {
untitledNumber: 1 // or any other value that makes sense for your tests
}
};
const wrapper = shallow(<AddNoteScreen route={routeMock}/>);

// Mock richTextRef
const richTextRef = { current: { insertImage: jest.fn() } };
Expand Down
7 changes: 6 additions & 1 deletion __tests__/SetTime.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ beforeAll(() => {

describe("AddNoteScreen", () => {
it("renders without crashing", () => {
const wrapper = shallow(<AddNoteScreen />);
const routeMock = {
params: {
untitledNumber: 1 // or any other value that makes sense for your tests
}
};
const wrapper = shallow(<AddNoteScreen route ={routeMock} />);
expect(wrapper).toMatchSnapshot();
});
});
Expand Down
1 change: 0 additions & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
},
"updates": {
"url": "https://u.expo.dev/801029ef-db83-4668-a97a-5adcc4c333e2"

},
"extra": {

Expand Down
2 changes: 1 addition & 1 deletion lib/components/loadingImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,4 @@ const styles = StyleSheet.create({
position: "absolute",
alignSelf: "center",
},
});
});
2 changes: 1 addition & 1 deletion lib/components/photoScroller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const PhotoScroller = forwardRef(
});
setNewMedia([...newMedia, newMediaItem]);
if (insertImageToEditor) {
insertImageToEditor(uploadedUrl, 'Captured Image');
insertImageToEditor(uploadedUrl);
}
} else if (
uri.endsWith(".MOV") ||
Expand Down
131 changes: 100 additions & 31 deletions lib/screens/AddNoteScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const user = User.getInstance();

const AddNoteScreen: React.FC<AddNoteScreenProps> = ({ navigation, route }) => {
const [titleText, setTitleText] = useState("");
const [isSaveButtonEnabled, setIsSaveButtonEnabled] = useState(true);
const [untitledNumber, setUntitledNumber] = useState("0");
const [bodyText, setBodyText] = useState("");
const [newMedia, setNewMedia] = useState<Media[]>([]);
const [newAudio, setNewAudio] = useState<AudioType[]>([]);
Expand All @@ -56,13 +58,14 @@ const AddNoteScreen: React.FC<AddNoteScreenProps> = ({ navigation, route }) => {
const [initialLoad, setInitialLoad] = useState(true);
const [promptedMissingTitle, setPromptedMissingTitle] = useState(false);
const [isUpdating, setIsUpdating] = useState(false);
const [location, setLocation] = useState<{
const [isLocationIconPressed, setIsLocationIconPressed] = useState(false);
let [location, setLocation] = useState<{
latitude: number;
longitude: number;
} | null>(null);

const { theme } = useTheme();

useEffect(() => {
const keyboardDidShowListener = Keyboard.addListener(
"keyboardDidShow",
Expand Down Expand Up @@ -94,6 +97,15 @@ const AddNoteScreen: React.FC<AddNoteScreenProps> = ({ navigation, route }) => {
checkLocationPermission();
}, []);

useEffect(() => {
const { untitledNumber } = route.params;
if (untitledNumber) {
setUntitledNumber(untitledNumber.toString());
}
}, [route.params]);

const [isLocationShown, setIsLocationShown] = useState(true);

const grabLocation = async () => {
try {
const userLocation = await Location.getCurrentPositionAsync({});
Expand Down Expand Up @@ -194,6 +206,60 @@ const AddNoteScreen: React.FC<AddNoteScreenProps> = ({ navigation, route }) => {
});
};

const [latitude, setLatitude] = useState(
location?.latitude?.toString() || ""
);
const [longitude, setLongitude] = useState(
location?.longitude?.toString() || ""
);

async function getLocation() {
try {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== "granted") {
console.log("Permission to access location was denied");
return null;
}
return await Location.getCurrentPositionAsync({});
} catch (error) {
console.error("Error getting location:", error);
return null;
}
}

const toggleLocationVisibility = async () => {
if (isLocationShown) {
// Hide Location
setLocation({
latitude: 0,
longitude: 0,
});
setLatitude("0");
setLongitude("0");
} else {
// Show Location
try {
let userLocation = await getLocation();

if (userLocation?.coords?.latitude !== undefined && userLocation?.coords?.longitude !== undefined) {
setLocation({
latitude: userLocation.coords.latitude,
longitude: userLocation.coords.longitude,
});

setLatitude(userLocation.coords.latitude.toString());
setLongitude(userLocation.coords.longitude.toString());
} else {
console.log("Location data is not available.");
}
} catch (error) {
console.error("Error setting location:", error);
}
}
setIsLocationShown((prev) => !prev);
setIsLocationIconPressed((prev) => !prev);
};

const checkLocationPermission = async () => {
try {
let { status } = await Location.getForegroundPermissionsAsync();
Expand Down Expand Up @@ -234,18 +300,23 @@ const AddNoteScreen: React.FC<AddNoteScreenProps> = ({ navigation, route }) => {

const saveNote = async () => {
const locationPermissionGranted = await checkLocationPermission();
if (titleText === "") {
if (!promptedMissingTitle) {
setPromptedMissingTitle(true);
Alert.alert(
"Title is empty",
"Please enter a title to save the note, or press back again to confirm not saving the note.",
);
return;
} else {
navigation.goBack();
return;
}
setIsSaveButtonEnabled(false);
let finalTitle = titleText.trim();
if (finalTitle === "") {
finalTitle = `Untitled ${untitledNumber}`;
}

if (finalTitle.trim() === "") {
Alert.alert(
"Empty Title",
"Please enter a title to save the note or delete the note.",
[
{ text: "Delete Note", onPress: () => navigation.goBack() },
{ text: "OK", onPress: () => console.log("OK Pressed") }
],
{ cancelable: false }
);
return;
}
if (!locationPermissionGranted) {
return;
Expand All @@ -254,26 +325,21 @@ const AddNoteScreen: React.FC<AddNoteScreenProps> = ({ navigation, route }) => {
setIsUpdating(true);

try {
const userLocation = await Location.getCurrentPositionAsync({});
const userID = await user.getId();
let latitude, longitude;

if (Platform.OS === 'ios') {
latitude = location?.latitude.toString();
longitude = location?.longitude.toString();
} else if (Platform.OS === 'android') {
latitude = userLocation.coords.latitude.toString();
longitude = userLocation.coords.longitude.toString();
}

const userLocation = await Location.getCurrentPositionAsync({});
const latitudeToSave = location ? location.latitude.toString() : userLocation.coords.latitude.toString();
const longitudeToSave = location ? location.longitude.toString() : userLocation.coords.longitude.toString();

setTime(new Date()); // force a fresh time date grab on note save

const newNote = {
title: titleText,
title: finalTitle,
text: bodyText,
media: newMedia,
audio: newAudio,
creator: userID,
latitude: latitude,
longitude: longitude,
latitude: latitudeToSave,
longitude: longitudeToSave,
published: isPublished,
tags: tags,
time: time,
Expand All @@ -291,14 +357,15 @@ const AddNoteScreen: React.FC<AddNoteScreenProps> = ({ navigation, route }) => {
setIsUpdating(false);
}
}
setIsSaveButtonEnabled(true);
};

return (
<SafeAreaView style={{ flex: 1 }}>
<View style={NotePageStyles().topContainer}>

<View style={NotePageStyles().topButtonsContainer}>
<TouchableOpacity style={NotePageStyles().topButtons} onPress={saveNote} testID="checklocationpermission">
<TouchableOpacity style={NotePageStyles().topButtons} disabled={!isSaveButtonEnabled} onPress={saveNote} testID="checklocationpermission">
<Ionicons name="arrow-back-outline" size={30} color={NotePageStyles().saveText.color} />
</TouchableOpacity>
<TextInput
Expand Down Expand Up @@ -353,11 +420,13 @@ const AddNoteScreen: React.FC<AddNoteScreenProps> = ({ navigation, route }) => {
setViewMedia(false);
setViewAudio(false);
setIsTagging(false);
setIsLocation(!isLocation);
//setIsLocation(!isLocation);
toggleLocationVisibility();
setIsTime(false);

}}
>
<Ionicons name="location-outline" size={30} color={NotePageStyles().saveText.color} />
<Ionicons name="location-outline" size={30} color={isLocationIconPressed ? "red" : NotePageStyles().saveText.color} />
</TouchableOpacity>
<TouchableOpacity
onPress={() => {
Expand Down
Loading
Loading