Daily Word (WidgetKit/SwiftUI)
mobile-dev
Daily Word is now on the App Store if you want to add a little language to your homescreen. Its source can be found on GitHub.
WidgetKit has been very exciting for lots of mobile developers, and something I recently explored a bit. The result of this exploration is “Daily Word” - a simple app that, every ~24-hours, shows you a new (Italian|French|Japanese)
word right on your homescreen! It’s simple and helped me nail down the basics of WidgetKit (and SwiftUI, something else i’d been wanting to try).
Specifically the getTimeline()
function, which defines the earliest possible time you’d like the widget to refresh (the OS will decide when actually to do that refresh). You then pass that new timeline to the completion.
func getTimeline(in context: Context, completion: @escaping (Timeline<WordEntry>) -> Void) {
// Time of creation
let now: Date = Date()
// The earliest moment we'd be ok with the widget refreshing
let calendar = Calendar.current
let future = calendar.date(byAdding: .hour, value: 24, to: now)!
let lang = LanguageFactory.Create(lang: language)
let randWord = lang.getRandom()
let entry = WordEntry(date: now, word: randWord, flag: lang.getFlag())
let timeline = Timeline(entries: [entry], policy: .after(future))
completion(timeline)
}
Add a new Language
Want to see your favorite language on your homescreen! Add a word list to my app with a PR!
Wordlists are embedded into the widget and can be added easily.
- Add a unique enum for your newly Supported Language.
- Create a new
.swift
file to /Languages that conforms to theLanguageBase
protocol.
Example .swift
class : LanguageBase {
var words: [Word] = [
Word(native: "war", foreign: "guerra"),
Word(native: "thing", foreign: "cosa"),
Word(native: "street", foreign: "strada")
...
...
]
func getAll() -> [Word] {
return words
}
func getRandom() -> Word {
let number = Int.random(in: 0..<words.count)
return words[number]
}
func getFlag() -> String {
return "🇮🇹"
}
}
- Add your new class here in the LanguageFactory.swift.
Your language will be available in the picker!
Get Daily Word for yourself on the App Store and check out its source on GitHub.
Have a comment? Let me know
This post helpful? Buy me a coffee!