Lab #3: Time Calculator App
In Android Studio, you will build an Android application centered around a Duration abstraction.
-
Using pure Kotlin, you will implement your own immutable
Durationtype representing days, hours, and minutes. In particular, you will overload the plus operator to allow twoDurationinstances to be added together. -
You will create a screen that acts as a time calculator, with buttons for digits, the unit selectors
d,h, andm(days, hours, minutes), as well as+and=to enter operations and display the result. -
You will implement two screens to create and display
Todos. EachTodois represented as aTimed<String>, combining aDurationwith a textual description. On the list screen,Todos can be selected and completed using a Finish button. When the selectedTodos are finished, the application will use thecombinefunction to display a message showing the total accumulated time together with the list of their descriptions. TheTimed<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}