Permalink
Browse files

WIP activity ordering

  • Loading branch information...
josefadamcik committed Oct 2, 2018
1 parent ec4ec49 commit c347765a8be844790613b3087ea3249af0237129
@@ -68,7 +68,9 @@ dependencies {
implementation "androidx.constraintlayout:constraintlayout:2.0.0-alpha2"
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'

// https://github.com/JakeWharton/ThreeTenABP
// https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html
implementation 'com.jakewharton.threetenabp:threetenabp:1.1.0'

// https://developer.android.com/kotlin/ktx
implementation "androidx.core:core-ktx:$ktx_version"
@@ -3,6 +3,7 @@ package cz.josefadamcik.activityjournal
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import cz.josefadamcik.activityjournal.model.ActivityRecord
import cz.josefadamcik.activityjournal.model.ActivityRecordsRepository
import cz.josefadamcik.activityjournal.screens.addactivity.AddActivityFlowFragment
import cz.josefadamcik.activityjournal.screens.addactivity.AddActivityTimeFragment
import cz.josefadamcik.activityjournal.screens.addactivity.AddActivityTitleFragment
@@ -17,7 +18,7 @@ class MainActivity : AppCompatActivity(),
AddActivityTimeFragment.OnFragmentInteractionListener,
AddActivityTitleFragment.OnFragmentInteractionListener {

private val activityRecordsList = mutableListOf<ActivityRecord>()
private val activityRecordsRepository = ActivityRecordsRepository()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -28,7 +29,7 @@ class MainActivity : AppCompatActivity(),
.add(android.R.id.content, TimelineFragment.newInstance())
.commit()
supportFragmentManager.executePendingTransactions()
findTimelineFragment()?.showRecords(activityRecordsList)
findTimelineFragment()?.showRecords(activityRecordsRepository.getActivityRecords())
}
}

@@ -41,8 +42,8 @@ class MainActivity : AppCompatActivity(),

override fun onAddActivityFlowFinished(activityRecord: ActivityRecord) {
supportFragmentManager.popBackStackImmediate()
activityRecordsList.add(activityRecord)
findTimelineFragment()?.showRecords(activityRecordsList)
activityRecordsRepository.add(activityRecord)
findTimelineFragment()?.showRecords(activityRecordsRepository.getActivityRecords())
}

override fun onBackPressed() {
@@ -1,5 +1,13 @@
package cz.josefadamcik.activityjournal.model

import org.threeten.bp.LocalDate
import org.threeten.bp.LocalDateTime
import org.threeten.bp.LocalTime
import org.threeten.bp.format.DateTimeFormatterBuilder




sealed class ActivityRecordDuration {
object Undergoing : ActivityRecordDuration()
data class Done(val minutes: Int) : ActivityRecordDuration()
@@ -10,4 +18,21 @@ data class ActivityRecord(
val date: String,
val time: String,
val duration: ActivityRecordDuration
)
) {
companion object {
private val timeFormatter = DateTimeFormatterBuilder()
.appendPattern("H:mm")
.toFormatter()

private val dateFormatter = DateTimeFormatterBuilder()
.appendPattern("d.M.yyyy")
.toFormatter()
}

val parsedDateTime : LocalDateTime
get() = LocalDateTime.of(
LocalDate.parse(date, dateFormatter),
LocalTime.parse(time, timeFormatter)
)

}
@@ -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
}
@@ -60,7 +60,7 @@ class TimelineFragment : Fragment() {
listener = null
}

fun showRecords(list: MutableList<ActivityRecord>) {
fun showRecords(list: List<ActivityRecord>) {
listOfRecords = list
adapter?.updateList(list)
}
@@ -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])
}



}

0 comments on commit c347765

Please sign in to comment.