Swift Date/Time Selective Repeats (Every Mon, Wed, and Friday, etc.)

Mazen Kilani
4 min readJul 2, 2021

Selective Repeats will Schedule a Repeat of an Event or Notification According to a User-Specified Days of the Week (Mon to Sun).

AppsGym ToDo Selective Repeat of Days of the Week

Scenario

Our app will schedule a repeating Remind Me date/time notification, based on a user-specified days of the week. The user should be able to select any combination of days (Mon, Tue, Wed, Thu, Fri, Sat, and Sun).

Technique

We shall utilise 7 UIButtons, a button for each day. The user can toggle each button off/on to indicate day selections. Upon save, we check each button status (off/on) and construct the repeat label showing ‘on’ days.

The Repeat View will implement a Delegate Protocol, with a function to return the user specified days of the week values back to the calling view. In our app, the ToDo Note view, which contains the Remind Me date/time field, will be the calling view. Hence, the ToDo Note view will act as the Repeat View Delegate and implement the Repeat View Delegate functions, which allows it to receive the selected days of the week values.

We trigger the logic to actually re-schedule the next reminder date/time when the user flags the current ToDo as done (completed). This will be detailed in a future article.

GIF AppsGym ToDo Showing Selective Repeats of Days of the Week

Audience

The article is for Swift developers who seek complete, proven, code-centric solutions to speed up their development projects.

App Model

We based the article on the AppsGym ToDo model app, published on Apple’s App Store (as 8ToDo), and you can download the complete Xcode project on AppsGym.com, for free.

User Interfaces

The Todo Note view will show a Remind Me Repeat UIButton which will navigate to the Repeat view, a UILabel to show the current repeat setup, and a Cancel UIButton, which will erase repeat setup.

ToDo Note View (Showing Remind Me Date/Time and Repeat Sections)

The Repeat view will show the Selective Days of the Week (Mon, Tue, Wed, Thu, Fri, Sat, and Sun), allow the user to tap each day’s UIButton to toggle the values (On/Off, visually changing the button image), and return the values to the calling view upon save.

ToDo Repeat View (Showing Selective Section)

Logic

NoteViewController.swift will contain a Repeat UIButton and a UITextField. The UIButton navigates to the RepeatViewController.swift, and the UITextField shows the string concatenation of the user-selected days of the week (e.g., ‘Mon Wed Fri’ or ‘Sat Sun’).

The UIButton triggers the “showRepeat” segue in the Storyboard, and passes the current values of repeat days of the week, if any. This allows the RepeatViewController.swift to show the current days selected, if any.

The NoteViewController acts as the RepeatViewControllerDelegate, linking the 2 views.

The RepeatViewController.swift implements the protocol RepeatViewControllerDelegate with its function repeatSelectiveOrPeriodic(..), as the same RepeatViewController also handles the (Cycle and Interval) Periodic logic (detailed in a prior Medium story).

The RepeatViewController.swift initialises 7 Boolean variables (for each day of the week, e.g., alarmOnWed), and shows 7 UIButtons, each acting as a toggle switch (on/off) upon tap, visually changing the button image. The view also contains a Save switch to save the user-selected values, and a function that is triggered upon the ‘Back’ arrow button to pass the user-selected days back to the calling view.

Code

Note: The app RepeatViewController handles both Periodic (every hour, day, …) and Selective (specific days of the week). In this article, the code snippets below have been ‘trimmed’ to show only the Selective part, allowing focus on the article scope. Please refer to our other article on Periodic Repeats.

NoteViewController.swift Showing Repeat Button and Label

NoteViewController.swift: Initialise, Button Action, and Navigation.

RepeatViewController.swift Initialise Variables, Arrays, and UIButtons

Repeat View Controller Selective Repeat Days of the Week Buttons

RepeatViewController.swift viewDidLoad() ToDo Note Title

RepeatViewController.swift viewWillAppear() Show current button values.

RepeatViewController.swift setAllSelectiveDaysToWhite()

RepeatViewController.swift setAllSelectiveDaysImages()

RepeatViewController.swift Selective Days Alarms

RepeatViewController.swift willMove(..) Return to Calling View.

The article covered the complete setup, logic, and code to implement a comprehensive date/time selective days of the week repeat feature. The user can select any combination of days, and we store the selections on the ToDo Note record to trigger the events or notifications on the specified dates and times. Hope you find the article useful for your app. Thanks for reading!

--

--

Mazen Kilani

I published 47 Swift iOS apps/games and 2 Flutter Android apps. I share complete Xcode projects, free (no ads), at AppsGym.com.