Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec4ec49
commit c347765
Showing
6 changed files
with
161 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
app/src/main/java/cz/josefadamcik/activityjournal/model/ActivityRecordsRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package cz.josefadamcik.activityjournal.model | ||
|
||
class ActivityRecordsRepository { | ||
private val activityRecordsList = mutableListOf<ActivityRecord>() | ||
|
||
fun add(item: ActivityRecord) { | ||
activityRecordsList.add(item) | ||
|
||
|
||
activityRecordsList.sortWith(Comparator<ActivityRecord> { record1: ActivityRecord, record2: ActivityRecord -> | ||
//a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the | ||
val parsedDateTime1 = record1.parsedDateTime | ||
val parsedDateTime2 = record2.parsedDateTime | ||
if (record1.duration == ActivityRecordDuration.Undergoing | ||
&& record2.duration != ActivityRecordDuration.Undergoing) { | ||
return@Comparator -1 | ||
} else if (record1.duration != ActivityRecordDuration.Undergoing | ||
&& record2.duration == ActivityRecordDuration.Undergoing) { | ||
return@Comparator 1 | ||
} else { | ||
return@Comparator parsedDateTime2.compareTo(parsedDateTime1) | ||
} | ||
}) | ||
} | ||
|
||
fun getActivityRecords(): List<ActivityRecord> = activityRecordsList | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
app/src/test/java/cz/josefadamcik/activityjournal/model/ActivityRecordsRepositoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package cz.josefadamcik.activityjournal.model | ||
|
||
import org.junit.Assert.* | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
|
||
/** | ||
* Timeline should look like this: | ||
* | ||
* 10:00 Undergoing | ||
* 8:00 Undergoing | ||
* 12:00 Finished | ||
* 10:00 Finished | ||
* | ||
*/ | ||
class ActivityRecordsRepositoryTest { | ||
|
||
|
||
private val activityRecord = ActivityRecord( | ||
title = "first activity", | ||
time = "10:00", | ||
date = "12.10.2018", | ||
duration = ActivityRecordDuration.Done(60) | ||
) | ||
|
||
private val repository = ActivityRecordsRepository().apply { | ||
add(activityRecord) | ||
} | ||
|
||
|
||
@Test | ||
fun `when new item's time is before first it should be returned as second`() { | ||
//arrange | ||
val record = activityRecord.copy(time = "8:00") | ||
|
||
//act | ||
repository.add(record) | ||
|
||
//assert | ||
assertEquals(record, repository.getActivityRecords()[1]) | ||
} | ||
|
||
|
||
@Test | ||
fun `when new item's time is after first it should be returned as first`() { | ||
//arrange | ||
val record = activityRecord.copy(time = "12:00") | ||
|
||
//act | ||
repository.add(record) | ||
|
||
//assert | ||
assertEquals(record, repository.getActivityRecords()[0]) | ||
} | ||
|
||
@Test | ||
fun `when new item is before first but undergoing it should be returned as first`() { | ||
//arrange | ||
val record = activityRecord.copy(time = "8:00", duration = ActivityRecordDuration.Undergoing) | ||
|
||
//act | ||
repository.add(record) | ||
|
||
//assert | ||
assertEquals(record, repository.getActivityRecords()[0]) | ||
} | ||
|
||
@Test | ||
fun `when two undergoing items are added they should be ordered by time`() { | ||
//arrange | ||
val record = activityRecord.copy(time = "8:00", duration = ActivityRecordDuration.Undergoing) | ||
val record2 = activityRecord.copy(time = "9:00", duration = ActivityRecordDuration.Undergoing) | ||
|
||
//act | ||
repository.add(record) | ||
repository.add(record2) | ||
|
||
//assert | ||
assertEquals(record2, repository.getActivityRecords()[0]) | ||
assertEquals(record, repository.getActivityRecords()[1]) | ||
} | ||
|
||
|
||
@Test | ||
fun `when new item's date is after first it should be returned as first`() { | ||
//arrange | ||
val record = activityRecord.copy(date = "13.10.2018") | ||
|
||
//act | ||
repository.add(record) | ||
|
||
//assert | ||
assertEquals(record, repository.getActivityRecords()[0]) | ||
} | ||
|
||
|
||
|
||
} |