Skip to content

Commit

Permalink
Merge pull request #32 from niwla23/development
Browse files Browse the repository at this point in the history
v2.0.1
  • Loading branch information
niwla23 authored Apr 20, 2022
2 parents 21cb9f7 + 1c7e8ad commit 01eea00
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 16 deletions.
66 changes: 66 additions & 0 deletions __tests__/smartTimetableDay-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* @format
*/

import 'react-native';
import smartTimetableDay from '../src/helpers/smartTimetableDay';

it('monday, 11:00', () => {
let result = smartTimetableDay(new Date('2022-04-18T11:00:00Z'));
expect(result).toBe(1); // monday
});

it('monday, 20:00', () => {
let result = smartTimetableDay(new Date('2022-04-18T20:00:00Z'));
expect(result).toBe(2); // tuesday
});

it('tuesday, 11:00', () => {
let result = smartTimetableDay(new Date('2022-04-19T11:00:00Z'));
expect(result).toBe(2); // tuesday
});

it('tuesday, 20:00', () => {
let result = smartTimetableDay(new Date('2022-04-19T20:00:00Z'));
expect(result).toBe(3); // wednesday
});

it('wednesday, 5:00', () => {
let result = smartTimetableDay(new Date('2022-04-20T05:00:00Z'));
expect(result).toBe(3); // wednesday
});

it('wednesday, 23:59', () => {
let result = smartTimetableDay(new Date('2022-04-20T23:59:00Z'));
expect(result).toBe(4); // thursday
});

it('thursday, 10:00', () => {
let result = smartTimetableDay(new Date('2022-04-21T10:00:00Z'));
expect(result).toBe(4); // thursday
});

it('thursday, 22:00', () => {
let result = smartTimetableDay(new Date('2022-04-21T22:00:00Z'));
expect(result).toBe(5); // friday
});

it('friday, 10:00', () => {
let result = smartTimetableDay(new Date('2022-04-22T10:00:00Z'));
expect(result).toBe(5); // friday
});

it('friday, 20:00', () => {
let result = smartTimetableDay(new Date('2022-04-22T20:00:00Z'));
expect(result).toBe(1); // monday
});

it('saturday, 14:00', () => {
let result = smartTimetableDay(new Date('2022-04-23T14:00:00Z'));
expect(result).toBe(1); // monday
});

it('sunday, 1:00', () => {
let result = smartTimetableDay(new Date('2022-04-24T01:00:00Z'));
expect(result).toBe(1); // monday
});
4 changes: 2 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ android {
applicationId "com.schulhack"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 15
versionName "2.0.0"
versionCode 16
versionName "2.0.1"
multiDexEnabled true
}

Expand Down
27 changes: 14 additions & 13 deletions src/helpers/serializeTimetable.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import AsyncStorage from '@react-native-async-storage/async-storage';
import { Lesson } from '../types/timetable';
import {Lesson} from '../types/timetable';

export default async (data_to_serialize: Lesson[][]) => {
data_to_serialize.map(async (day, dayIndex) => {
day.map(async (lesson, lessonIndex) => {
await AsyncStorage.setItem(`timetable.${dayIndex}.${lessonIndex}`, JSON.stringify(
{
"teacher": lesson.teacher,
"subject": lesson.subject,
"room": lesson.room,
"day": dayIndex,
"hour": lessonIndex
}
))
})
})
}
await AsyncStorage.setItem(
`timetable.${dayIndex}.${lessonIndex}`,
JSON.stringify({
teacher: lesson.teacher,
subject: lesson.subject,
room: lesson.room,
day: dayIndex,
hour: lessonIndex,
}),
);
});
});
};
24 changes: 24 additions & 0 deletions src/helpers/smartTimetableDay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const smartTimetableDay = (currentDate: Date): number => {
let shouldUseNextDay = false;
if (currentDate.getUTCHours() >= 18) {
shouldUseNextDay = true;
}
if (currentDate.getUTCDay() >= 6 || currentDate.getUTCDay() === 0) {
// who thought it is a good idea to make sunday day 0?
// if we are on a weekend always show table for monday
return 1;
}
if (shouldUseNextDay) {
// time is after 18:00, showing next day
if (currentDate.getUTCDay() === 5) {
// friday evening, also show monday
return 1;
} else {
// mo - fr, after 18:00 show next day
return currentDate.getUTCDay() + 1;
}
}
return currentDate.getUTCDay();
};

export default smartTimetableDay;
4 changes: 3 additions & 1 deletion src/screens/timetable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import deserializeTimetable from '../helpers/deserializeTimetable';
import {TextInputModal} from '../components/textInputModal';
import serializeTimetable from '../helpers/serializeTimetable';
import Button from '../components/button';
import smartTimetableDay from '../helpers/smartTimetableDay';

export const TimetableItem = ({item, isWeekView}) => {
const {colors, isDark} = useTheme();
Expand Down Expand Up @@ -115,8 +116,9 @@ export default function TimetableScreen({navigation}) {

const [timetable, setTimetable] = useState([]);
const [currentDay, setCurrentDay] = useState(
new Date().getDay() < 5 ? new Date().getDay() : 0,
smartTimetableDay(new Date()) - 1,
);

const [timetableExists, setTimetableExists] = useState(true);
const [weekView, setWeekView] = useState(false);

Expand Down

0 comments on commit 01eea00

Please sign in to comment.