Lab #3

Lab #3: Time Calculator App

In Android Studio, you will build an Android application centered around a Duration abstraction.

  1. Using pure Kotlin, you will implement your own immutable Duration type representing days, hours, and minutes. In particular, you will overload the plus operator to allow two Duration instances to be added together.

  2. You will create a screen that acts as a time calculator, with buttons for digits, the unit selectors d, h, and m (days, hours, minutes), as well as + and = to enter operations and display the result.

  3. You will implement two screens to create and display Todos. Each Todo is represented as a Timed<String>, combining a Duration with a textual description. On the list screen, Todos can be selected and completed using a Finish button. When the selected Todos are finished, the application will use the combine function to display a message showing the total accumulated time together with the list of their descriptions. The Timed<T> type is given below:

 1data class Timed<T>(
 2    val duration: Duration,
 3    val value: T
 4) {
 5
 6    fun <U> map(f: (T) -> U): Timed<U> =
 7        Timed(duration, f(value))
 8
 9    fun <U> flatMap(f: (T) -> Timed<U>): Timed<U> {
10        val next = f(value)
11        return Timed(
12            duration = duration + next.duration,
13            value = next.value
14        )
15    }
16
17    companion object {
18
19        fun <T> pure(value: T): Timed<T> =
20            Timed(Duration.ZERO, value)
21
22        fun <T> combine(items: List<Timed<T>>): Timed<List<T>> =
23            items.fold(pure(emptyList())) { acc, timed ->
24                acc.flatMap { list ->
25                    timed.map { value -> list + value }
26                }
27            }
28    }
29
30}