MGS-Developers / my-mgs

The MyMGS app's mobile-native client
MIT License
1 stars 0 forks source link

Fix catering week calculator #21

Closed palkerecsenyi closed 3 years ago

palkerecsenyi commented 3 years ago

Catering weeks currently operate on a tri-weekly schedule, with 3 weeks labelled Week 1, Week 2, and Week 3. However, this is subject to change, so our code must ensure the amount of weeks in a catering cycle is variable.

Here's the current algorithm for calculating which week it is, all of which takes place in the lib/data/catering.dart file:

  1. Download the list of half terms. Each contains three pieces of data:

    start week, end week, catering week

    Start week and end week refer to the week number in the year. For the two michaelmas half terms, these are the week numbers of 2021 (at the moment), while the remaining 4 half terms are week numbers of 2022.

    Catering week refers to the catering week that half term starts with. E.g. a catering week of 3 means that the first week of this half term is a catering Week 3. This value must be honoured.

  2. Find the current week number. This is pretty simple, here's how we do it right now:

    final currentDate = DateTime.now();
    final currentWeekNumber = Jiffy(currentDate).week;
  3. Find which half term it currently is, based on the week number. If none of the half-terms match, we're currently in a school holiday, so we can tell the user this by returning a null food menu for today. Here's how we do it right now

    final Term? currentTerm = terms.firstWhere((term) {
        return currentWeekNumber >= term.startWeek && currentWeekNumber <= term.endWeek;
    });
  4. Use some kind of loop to figure out which catering week it is. You should be able to work this out based on the difference between the current week number and the start week number of the current half term, the starting catering week of the half term, and the CATERING_CYCLE_LENGTH constant.

Step 4 is what I need help with. It seems really simple, but all my solutions (including the current one) are simply incorrect. You can see the current implementation for step 4 here. All you need to do is change the contents of the _calculateCycle function to make it return a correct catering week number.