├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── dimens.xml
│ │ │ │ ├── colors.xml
│ │ │ │ ├── styles.xml
│ │ │ │ └── strings.xml
│ │ │ ├── drawable
│ │ │ │ ├── wow_such_empty.jpg
│ │ │ │ ├── ic_add.xml
│ │ │ │ ├── ic_delete.xml
│ │ │ │ └── ic_launcher_background.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── menu
│ │ │ │ └── menu_main.xml
│ │ │ ├── layout
│ │ │ │ ├── activity_to_do_list.xml
│ │ │ │ ├── content_to_do_list.xml
│ │ │ │ ├── activity_add.xml
│ │ │ │ └── todo_item.xml
│ │ │ └── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── project
│ │ │ │ └── segunfrancis
│ │ │ │ └── yetanothertodoapp
│ │ │ │ ├── ToDoApplication.kt
│ │ │ │ ├── data
│ │ │ │ ├── ToDoRoomDatabase.kt
│ │ │ │ ├── ToDoRepository.kt
│ │ │ │ ├── ToDoDao.kt
│ │ │ │ ├── ToDoRepositoryImpl.kt
│ │ │ │ └── ToDo.kt
│ │ │ │ ├── ui
│ │ │ │ ├── list
│ │ │ │ │ ├── OnItemClickListener.kt
│ │ │ │ │ ├── ListUtils.kt
│ │ │ │ │ ├── ToDoListViewModel.kt
│ │ │ │ │ ├── ToDoListActivity.kt
│ │ │ │ │ └── ToDoAdapter.kt
│ │ │ │ └── add
│ │ │ │ │ ├── AddViewModel.kt
│ │ │ │ │ └── AddActivity.kt
│ │ │ │ ├── ExtensionUtil.kt
│ │ │ │ └── di
│ │ │ │ ├── RepoModule.kt
│ │ │ │ └── DatabaseModule.kt
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── project
│ │ │ └── segunfrancis
│ │ │ └── yetanothertodoapp
│ │ │ ├── TestExt.kt
│ │ │ ├── MainCoroutineRule.kt
│ │ │ ├── ListUtilsTest.kt
│ │ │ ├── AddViewModelTest.kt
│ │ │ └── ListViewModelTest.kt
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── project
│ │ └── segunfrancis
│ │ └── yetanothertodoapp
│ │ ├── AddActivityTest.kt
│ │ ├── ToDoListActivityTest.kt
│ │ └── ToDoDaoTest.kt
├── proguard-rules.pro
└── build.gradle
├── .idea
├── .name
├── codeStyles
│ ├── codeStyleConfig.xml
│ └── Project.xml
├── dictionaries
│ └── SegunFrancis.xml
├── vcs.xml
├── kotlinc.xml
├── misc.xml
├── runConfigurations.xml
├── gradle.xml
└── jarRepositories.xml
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── screenshots
├── Screenshot_20200811-190551_Yet Another Todo App.jpg
├── Screenshot_20200811-190610_Yet Another Todo App.jpg
├── Screenshot_20200811-190626_Yet Another Todo App.jpg
└── Screenshot_20200813-192229_Yet Another Todo App.jpg
├── .gitignore
├── gradle.properties
├── gradlew.bat
├── README.md
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | Yet Another Todo App
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 | rootProject.name = "Yet Another Todo App"
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 | 16dp
3 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/segunfrancis/Yet-Another-Todo-App/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/drawable/wow_such_empty.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/segunfrancis/Yet-Another-Todo-App/HEAD/app/src/main/res/drawable/wow_such_empty.jpg
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/segunfrancis/Yet-Another-Todo-App/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/segunfrancis/Yet-Another-Todo-App/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/segunfrancis/Yet-Another-Todo-App/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/segunfrancis/Yet-Another-Todo-App/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/segunfrancis/Yet-Another-Todo-App/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/segunfrancis/Yet-Another-Todo-App/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/segunfrancis/Yet-Another-Todo-App/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/segunfrancis/Yet-Another-Todo-App/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/segunfrancis/Yet-Another-Todo-App/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/segunfrancis/Yet-Another-Todo-App/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/.idea/codeStyles/codeStyleConfig.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/screenshots/Screenshot_20200811-190551_Yet Another Todo App.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/segunfrancis/Yet-Another-Todo-App/HEAD/screenshots/Screenshot_20200811-190551_Yet Another Todo App.jpg
--------------------------------------------------------------------------------
/screenshots/Screenshot_20200811-190610_Yet Another Todo App.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/segunfrancis/Yet-Another-Todo-App/HEAD/screenshots/Screenshot_20200811-190610_Yet Another Todo App.jpg
--------------------------------------------------------------------------------
/screenshots/Screenshot_20200811-190626_Yet Another Todo App.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/segunfrancis/Yet-Another-Todo-App/HEAD/screenshots/Screenshot_20200811-190626_Yet Another Todo App.jpg
--------------------------------------------------------------------------------
/screenshots/Screenshot_20200813-192229_Yet Another Todo App.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/segunfrancis/Yet-Another-Todo-App/HEAD/screenshots/Screenshot_20200813-192229_Yet Another Todo App.jpg
--------------------------------------------------------------------------------
/.idea/dictionaries/SegunFrancis.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | segun
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/kotlinc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 | .cxx
15 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Aug 05 09:37:13 WAT 2020
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/java/com/project/segunfrancis/yetanothertodoapp/ToDoApplication.kt:
--------------------------------------------------------------------------------
1 | package com.project.segunfrancis.yetanothertodoapp
2 |
3 | import android.app.Application
4 | import dagger.hilt.android.HiltAndroidApp
5 |
6 | /**
7 | * Created by SegunFrancis
8 | */
9 |
10 | @HiltAndroidApp
11 | class ToDoApplication : Application()
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_add.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/java/com/project/segunfrancis/yetanothertodoapp/data/ToDoRoomDatabase.kt:
--------------------------------------------------------------------------------
1 | package com.project.segunfrancis.yetanothertodoapp.data
2 |
3 | import androidx.room.Database
4 | import androidx.room.RoomDatabase
5 |
6 | /**
7 | * Created by SegunFrancis
8 | */
9 |
10 | @Database(entities = [ToDo::class], version = 1, exportSchema = false)
11 | abstract class ToDoRoomDatabase : RoomDatabase() {
12 | abstract fun todoDao(): ToDoDao
13 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/project/segunfrancis/yetanothertodoapp/ui/list/OnItemClickListener.kt:
--------------------------------------------------------------------------------
1 | package com.project.segunfrancis.yetanothertodoapp.ui.list
2 |
3 | import com.project.segunfrancis.yetanothertodoapp.data.ToDo
4 |
5 | /**
6 | * Created by SegunFrancis
7 | *
8 | * Interface to handle adapter item clicks
9 | */
10 |
11 | interface OnItemClickListener {
12 | fun onCheckboxChecked(id: String)
13 | fun onDeleteIconClicked(toDo: ToDo)
14 | }
--------------------------------------------------------------------------------
/app/src/test/java/com/project/segunfrancis/yetanothertodoapp/TestExt.kt:
--------------------------------------------------------------------------------
1 | package com.project.segunfrancis.yetanothertodoapp
2 |
3 | import kotlinx.coroutines.ExperimentalCoroutinesApi
4 | import kotlinx.coroutines.test.runBlockingTest
5 |
6 | /**
7 | * Created by SegunFrancis
8 | */
9 |
10 | @ExperimentalCoroutinesApi
11 | fun MainCoroutineRule.runBlockingTest(block: suspend () -> Unit) =
12 | this.testDispatcher.runBlockingTest {
13 | block()
14 | }
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/app/src/main/java/com/project/segunfrancis/yetanothertodoapp/data/ToDoRepository.kt:
--------------------------------------------------------------------------------
1 | package com.project.segunfrancis.yetanothertodoapp.data
2 |
3 | import kotlinx.coroutines.flow.Flow
4 |
5 | /**
6 | * Created by SegunFrancis
7 | */
8 |
9 | interface ToDoRepository {
10 |
11 | fun getAllToDos(): Flow>
12 |
13 | suspend fun insert(toDo: ToDo)
14 |
15 | suspend fun toggleTodo(id: String)
16 |
17 | fun getUpcomingToDosCount(): Flow
18 |
19 | suspend fun delete(toDo: ToDo)
20 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/project/segunfrancis/yetanothertodoapp/ExtensionUtil.kt:
--------------------------------------------------------------------------------
1 | package com.project.segunfrancis.yetanothertodoapp
2 |
3 | import android.view.View
4 | import androidx.lifecycle.LiveData
5 | import androidx.lifecycle.MutableLiveData
6 |
7 | /**
8 | * Created by SegunFrancis
9 | */
10 |
11 | fun View.show() {
12 | visibility = View.VISIBLE
13 | }
14 |
15 | fun View.hide() {
16 | visibility = View.INVISIBLE
17 | }
18 |
19 | fun MutableLiveData.asLiveData(): LiveData {
20 | return this
21 | }
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #003CEF
4 | #00279A
5 | #03DAC5
6 | #fff3e0
7 | #ffb74d
8 | #fb8c00
9 | #ef6c00
10 | #e65100
11 | #64b5f6
12 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_delete.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
15 |
16 |
--------------------------------------------------------------------------------
/app/src/main/java/com/project/segunfrancis/yetanothertodoapp/di/RepoModule.kt:
--------------------------------------------------------------------------------
1 | package com.project.segunfrancis.yetanothertodoapp.di
2 |
3 | import com.project.segunfrancis.yetanothertodoapp.data.ToDoRepository
4 | import com.project.segunfrancis.yetanothertodoapp.data.ToDoRepositoryImpl
5 | import dagger.Binds
6 | import dagger.Module
7 | import dagger.hilt.InstallIn
8 | import dagger.hilt.android.components.ActivityRetainedComponent
9 |
10 | /**
11 | * Created by SegunFrancis
12 | */
13 |
14 | @Module
15 | @InstallIn(ActivityRetainedComponent::class)
16 | abstract class RepoModule {
17 |
18 | @Binds
19 | abstract fun bindRepository(repositoryImpl: ToDoRepositoryImpl): ToDoRepository
20 | }
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
9 |
10 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/project/segunfrancis/yetanothertodoapp/AddActivityTest.kt:
--------------------------------------------------------------------------------
1 | package com.project.segunfrancis.yetanothertodoapp
2 |
3 | import androidx.test.espresso.Espresso.onView
4 | import androidx.test.espresso.assertion.ViewAssertions.matches
5 | import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
6 | import androidx.test.espresso.matcher.ViewMatchers.withId
7 | import androidx.test.rule.ActivityTestRule
8 | import com.project.segunfrancis.yetanothertodoapp.ui.add.AddActivity
9 | import org.junit.Rule
10 | import org.junit.Test
11 |
12 | /**
13 | * Created by SegunFrancis
14 | */
15 | class AddActivityTest {
16 |
17 | @get:Rule
18 | val activityTestRule = ActivityTestRule(AddActivity::class.java)
19 |
20 | @Test
21 | fun test_isEditTextVisible() {
22 | onView(withId(R.id.tilTitle)).check(matches(isDisplayed()))
23 | }
24 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/project/segunfrancis/yetanothertodoapp/data/ToDoDao.kt:
--------------------------------------------------------------------------------
1 | package com.project.segunfrancis.yetanothertodoapp.data
2 |
3 | import androidx.room.*
4 | import kotlinx.coroutines.flow.Flow
5 |
6 | /**
7 | * Created by SegunFrancis
8 | */
9 |
10 | @Dao
11 | interface ToDoDao {
12 |
13 | @Query("SELECT * FROM todo ORDER BY created DESC")
14 | fun getAllToDos(): Flow>
15 |
16 | @Query("SELECT count(*) FROM todo WHERE completed = 0 AND dueDate >= :date")
17 | fun getDateCount(date: Long): Flow
18 |
19 | @Query("SELECT * FROM todo WHERE id = :id")
20 | fun getToDo(id: String): ToDo
21 |
22 | @Insert(onConflict = OnConflictStrategy.REPLACE)
23 | suspend fun insert(toDo: ToDo)
24 |
25 | @Query("UPDATE todo set completed = ~completed WHERE id = :id")
26 | suspend fun toggleTodo(id: String): Int
27 |
28 | @Delete
29 | suspend fun delete(toDo: ToDo)
30 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/project/segunfrancis/yetanothertodoapp/ui/list/ListUtils.kt:
--------------------------------------------------------------------------------
1 | package com.project.segunfrancis.yetanothertodoapp.ui.list
2 |
3 | import androidx.annotation.VisibleForTesting
4 | import com.project.segunfrancis.yetanothertodoapp.R
5 |
6 | /**
7 | * Created by SegunFrancis
8 | */
9 |
10 | @VisibleForTesting
11 | fun determineCardColor(dueDate: Long?, done: Boolean): Int {
12 | var color = R.color.todoNotDue
13 |
14 | if (done) return R.color.todoDone
15 |
16 | if (dueDate != null) {
17 | val now = System.currentTimeMillis()
18 | val day = 1000 * 60 * 60 * 24
19 | val daysUntilDue = (dueDate.minus(now)) / day
20 |
21 | color = when {
22 | daysUntilDue <= 0 -> R.color.todoOverDue
23 | daysUntilDue <= 7 -> R.color.todoDueStrong
24 | daysUntilDue <= 14 -> R.color.todoDueMedium
25 | else -> R.color.todoDueLight
26 | }
27 | }
28 | return color
29 | }
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/app/src/test/java/com/project/segunfrancis/yetanothertodoapp/MainCoroutineRule.kt:
--------------------------------------------------------------------------------
1 | package com.project.segunfrancis.yetanothertodoapp
2 |
3 | import kotlinx.coroutines.Dispatchers
4 | import kotlinx.coroutines.ExperimentalCoroutinesApi
5 | import kotlinx.coroutines.test.TestCoroutineDispatcher
6 | import kotlinx.coroutines.test.resetMain
7 | import kotlinx.coroutines.test.setMain
8 | import org.junit.rules.TestWatcher
9 | import org.junit.runner.Description
10 |
11 | /**
12 | * Created by SegunFrancis
13 | */
14 |
15 | @ExperimentalCoroutinesApi
16 | class MainCoroutineRule(val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()) :
17 | TestWatcher() {
18 | override fun starting(description: Description?) {
19 | super.starting(description)
20 | Dispatchers.setMain(testDispatcher)
21 | }
22 |
23 | override fun finished(description: Description?) {
24 | super.finished(description)
25 | Dispatchers.resetMain()
26 | testDispatcher.cleanupTestCoroutines()
27 | }
28 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
13 |
14 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Yet Another Todo App
3 | Settings
4 |
5 | First Fragment
6 | Second Fragment
7 | Next
8 | Previous
9 |
10 | Hello first fragment
11 | Hello second fragment. Arg: %1$s
12 | Due Soon:
13 | Start
14 | Due
15 | Delete icon
16 | Save
17 | Clear Due Date
18 | Set Due Date
19 | Title
20 | Empty todo list image
21 |
--------------------------------------------------------------------------------
/.idea/jarRepositories.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/app/src/main/java/com/project/segunfrancis/yetanothertodoapp/data/ToDoRepositoryImpl.kt:
--------------------------------------------------------------------------------
1 | package com.project.segunfrancis.yetanothertodoapp.data
2 |
3 | import kotlinx.coroutines.flow.Flow
4 | import java.lang.IllegalArgumentException
5 | import javax.inject.Inject
6 |
7 | /**
8 | * Created by SegunFrancis
9 | */
10 |
11 | class ToDoRepositoryImpl @Inject constructor(private val toDoDao: ToDoDao) : ToDoRepository {
12 |
13 | override fun getAllToDos(): Flow> {
14 | return toDoDao.getAllToDos()
15 | }
16 |
17 | override suspend fun insert(toDo: ToDo) {
18 | require(toDo.title != "") {
19 | "Title must not be empty"
20 | }
21 | toDoDao.insert(toDo)
22 | }
23 |
24 | override suspend fun toggleTodo(id: String) {
25 | require(toDoDao.toggleTodo(id) == 1) {
26 | throw IllegalArgumentException("Todo not found")
27 | }
28 | }
29 |
30 | override fun getUpcomingToDosCount(): Flow {
31 | return toDoDao.getDateCount(System.currentTimeMillis())
32 | }
33 |
34 | override suspend fun delete(toDo: ToDo) {
35 | toDoDao.delete(toDo)
36 | }
37 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/project/segunfrancis/yetanothertodoapp/ui/add/AddViewModel.kt:
--------------------------------------------------------------------------------
1 | package com.project.segunfrancis.yetanothertodoapp.ui.add
2 |
3 | import androidx.hilt.lifecycle.ViewModelInject
4 | import androidx.lifecycle.ViewModel
5 | import androidx.lifecycle.viewModelScope
6 | import com.project.segunfrancis.yetanothertodoapp.data.ToDo
7 | import com.project.segunfrancis.yetanothertodoapp.data.ToDoRepository
8 | import kotlinx.coroutines.CoroutineDispatcher
9 | import kotlinx.coroutines.launch
10 | import java.util.*
11 |
12 | /**
13 | * Created by SegunFrancis
14 | */
15 |
16 | class AddViewModel @ViewModelInject constructor(
17 | private val toDoRepository: ToDoRepository,
18 | private val dispatcher: CoroutineDispatcher
19 | ) :
20 | ViewModel() {
21 | val toDo = ToDo(
22 | UUID.randomUUID().toString(),
23 | "",
24 | null,
25 | false,
26 | 0
27 | )
28 |
29 | fun save(): String? {
30 | if (toDo.title == "") return "Title is required"
31 |
32 | toDo.created = System.currentTimeMillis()
33 | viewModelScope.launch(dispatcher) { toDoRepository.insert(toDo) }
34 | return null
35 | }
36 | }
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx2048m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app"s APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 | # Kotlin code style for this project: "official" or "obsolete":
21 | kotlin.code.style=official
--------------------------------------------------------------------------------
/app/src/main/java/com/project/segunfrancis/yetanothertodoapp/data/ToDo.kt:
--------------------------------------------------------------------------------
1 | package com.project.segunfrancis.yetanothertodoapp.data
2 |
3 | import androidx.room.Entity
4 | import androidx.room.PrimaryKey
5 |
6 | /**
7 | * Created by SegunFrancis
8 | */
9 |
10 | @Entity
11 | data class ToDo(
12 | @PrimaryKey var id: String,
13 | var title: String,
14 | var dueDate: Long?,
15 | var completed: Boolean,
16 | var created: Long
17 | ) {
18 | override fun equals(other: Any?): Boolean {
19 | if (this === other) return true
20 | if (javaClass != other?.javaClass) return false
21 |
22 | other as ToDo
23 |
24 | if (id != other.id) return false
25 | if (title != other.title) return false
26 | if (dueDate != other.dueDate) return false
27 | if (completed != other.completed) return false
28 | if (created != other.created) return false
29 |
30 | return true
31 | }
32 |
33 | override fun hashCode(): Int {
34 | var result = id.hashCode()
35 | result = 31 * result + title.hashCode()
36 | result = 31 * result + (dueDate?.hashCode() ?: 0)
37 | result = 31 * result + completed.hashCode()
38 | result = 31 * result + created.hashCode()
39 | return result
40 | }
41 | }
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/project/segunfrancis/yetanothertodoapp/ToDoListActivityTest.kt:
--------------------------------------------------------------------------------
1 | package com.project.segunfrancis.yetanothertodoapp
2 |
3 | import androidx.test.espresso.Espresso.onView
4 | import androidx.test.espresso.action.ViewActions.click
5 | import androidx.test.espresso.assertion.ViewAssertions.matches
6 | import androidx.test.espresso.intent.Intents.intended
7 | import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent
8 | import androidx.test.espresso.intent.rule.IntentsTestRule
9 | import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
10 | import androidx.test.espresso.matcher.ViewMatchers.withId
11 | import com.project.segunfrancis.yetanothertodoapp.ui.add.AddActivity
12 | import com.project.segunfrancis.yetanothertodoapp.ui.list.ToDoListActivity
13 | import org.junit.Rule
14 | import org.junit.Test
15 |
16 | /**
17 | * Created by SegunFrancis
18 | */
19 |
20 | class ToDoListActivityTest {
21 |
22 | @get:Rule
23 | val intentsTestRule = IntentsTestRule(ToDoListActivity::class.java)
24 |
25 | @Test
26 | fun test_checkFloatingActionButtonVisibility() {
27 | onView(withId(R.id.fab)).check(matches(isDisplayed()))
28 | }
29 |
30 | @Test
31 | fun test_checkToolbarVisibility() {
32 | onView(withId(R.id.toolbar)).check(matches(isDisplayed()))
33 | }
34 |
35 | @Test
36 | fun test_navigateToAddActivity() {
37 | onView(withId(R.id.fab)).perform(click())
38 | intended(hasComponent(AddActivity::class.java.name))
39 | }
40 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/project/segunfrancis/yetanothertodoapp/di/DatabaseModule.kt:
--------------------------------------------------------------------------------
1 | package com.project.segunfrancis.yetanothertodoapp.di
2 |
3 | import android.content.Context
4 | import androidx.room.Room
5 | import com.project.segunfrancis.yetanothertodoapp.data.ToDoDao
6 | import com.project.segunfrancis.yetanothertodoapp.data.ToDoRoomDatabase
7 | import dagger.Module
8 | import dagger.Provides
9 | import dagger.hilt.InstallIn
10 | import dagger.hilt.android.components.ApplicationComponent
11 | import dagger.hilt.android.qualifiers.ApplicationContext
12 | import kotlinx.coroutines.CoroutineDispatcher
13 | import kotlinx.coroutines.Dispatchers
14 | import javax.inject.Singleton
15 |
16 | /**
17 | * Created by SegunFrancis
18 | */
19 |
20 | @Module
21 | @InstallIn(ApplicationComponent::class)
22 | object DatabaseModule {
23 |
24 | @Provides
25 | @Singleton
26 | fun provideDatabase(@ApplicationContext context: Context): ToDoRoomDatabase {
27 | return synchronized(ToDoRoomDatabase::class.java) {
28 | Room.databaseBuilder(
29 | context.applicationContext,
30 | ToDoRoomDatabase::class.java,
31 | "todo_database"
32 | )
33 | .build()
34 | }
35 | }
36 |
37 | @Provides
38 | fun provideToDoDao(database: ToDoRoomDatabase): ToDoDao {
39 | return database.todoDao()
40 | }
41 |
42 | @Provides
43 | fun provideCoroutineDispatcher(): CoroutineDispatcher {
44 | return Dispatchers.IO
45 | }
46 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_to_do_list.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
20 |
21 |
22 |
23 |
30 |
31 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/test/java/com/project/segunfrancis/yetanothertodoapp/ListUtilsTest.kt:
--------------------------------------------------------------------------------
1 | package com.project.segunfrancis.yetanothertodoapp
2 |
3 | import com.project.segunfrancis.yetanothertodoapp.ui.list.determineCardColor
4 | import org.junit.Assert.assertEquals
5 | import org.junit.Test
6 | import org.junit.runner.RunWith
7 | import org.junit.runners.Parameterized
8 |
9 | /**
10 | * Created by SegunFrancis
11 | */
12 |
13 | @RunWith(Parameterized::class)
14 | class ListUtilsTest(
15 | private val expected: Int,
16 | private val dueDate: Long?,
17 | private val done: Boolean,
18 | private val scenario: String
19 | ) {
20 |
21 | companion object {
22 | private val now = System.currentTimeMillis()
23 | private const val day = 1000 * 60 * 60 * 24
24 |
25 | @JvmStatic
26 | @Parameterized.Parameters(name = "determineCardColor: {3}")
27 | fun toDos() = listOf(
28 | arrayOf(R.color.todoDone, null, true, "Done, no date"),
29 | arrayOf(R.color.todoNotDue, null, false, "Not done, no date"),
30 | arrayOf(R.color.todoOverDue, now - day, false, "Not done, due yesterday"),
31 | arrayOf(R.color.todoDueStrong, now + (day * 7), false, "Not done, due in 7 days"),
32 | arrayOf(R.color.todoDueMedium, now + (day * 14), false, "Not done, due in 14 days"),
33 | arrayOf(R.color.todoDueLight, now + (day * 20), false, "Not done, due in 16 days and above")
34 | )
35 | }
36 |
37 | @Test
38 | fun test_determineCardColor() {
39 |
40 | val actual = determineCardColor(dueDate, done)
41 | assertEquals(expected, actual)
42 | }
43 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/app/src/main/java/com/project/segunfrancis/yetanothertodoapp/ui/list/ToDoListViewModel.kt:
--------------------------------------------------------------------------------
1 | package com.project.segunfrancis.yetanothertodoapp.ui.list
2 |
3 | import androidx.hilt.lifecycle.ViewModelInject
4 | import androidx.lifecycle.*
5 | import com.project.segunfrancis.yetanothertodoapp.asLiveData
6 | import com.project.segunfrancis.yetanothertodoapp.data.ToDo
7 | import com.project.segunfrancis.yetanothertodoapp.data.ToDoRepository
8 | import kotlinx.coroutines.CoroutineDispatcher
9 | import kotlinx.coroutines.flow.collect
10 | import kotlinx.coroutines.launch
11 |
12 | /**
13 | * Created by SegunFrancis
14 | */
15 |
16 | class ToDoListViewModel @ViewModelInject constructor(
17 | private val toDoRepository: ToDoRepository,
18 | private val coroutineDispatcher: CoroutineDispatcher
19 | ) :
20 | ViewModel() {
21 |
22 | private var _toDoList = MutableLiveData>()
23 | val toDoList: LiveData> = _toDoList.asLiveData()
24 |
25 | private var _count = MutableLiveData()
26 | val count: LiveData = _count.asLiveData()
27 |
28 | val allToDos = toDoRepository.getAllToDos()
29 | val upcomingToDosCount = toDoRepository.getUpcomingToDosCount()
30 |
31 | init {
32 | getToDos()
33 | getUpcomingToDosCount()
34 | }
35 |
36 | private fun getToDos() {
37 | viewModelScope.launch(coroutineDispatcher) {
38 | allToDos.collect {
39 | _toDoList.postValue(it)
40 | }
41 | }
42 | }
43 |
44 | private fun getUpcomingToDosCount() {
45 | viewModelScope.launch(coroutineDispatcher) {
46 | upcomingToDosCount.collect {
47 | _count.postValue(it)
48 | }
49 | }
50 | }
51 |
52 | fun toggleToDo(id: String) {
53 | viewModelScope.launch(coroutineDispatcher) {
54 | toDoRepository.toggleTodo(id)
55 | }
56 | }
57 |
58 | fun deleteToDo(toDo: ToDo) {
59 | viewModelScope.launch(coroutineDispatcher) {
60 | toDoRepository.delete(toDo)
61 | }
62 | }
63 | }
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/project/segunfrancis/yetanothertodoapp/ToDoDaoTest.kt:
--------------------------------------------------------------------------------
1 | package com.project.segunfrancis.yetanothertodoapp
2 |
3 | import androidx.room.Room
4 | import androidx.test.ext.junit.runners.AndroidJUnit4
5 | import androidx.test.platform.app.InstrumentationRegistry
6 | import com.project.segunfrancis.yetanothertodoapp.data.ToDo
7 | import com.project.segunfrancis.yetanothertodoapp.data.ToDoDao
8 | import com.project.segunfrancis.yetanothertodoapp.data.ToDoRoomDatabase
9 | import kotlinx.coroutines.GlobalScope
10 | import kotlinx.coroutines.flow.collect
11 | import kotlinx.coroutines.launch
12 | import org.junit.After
13 | import org.junit.Assert.assertEquals
14 | import org.junit.Before
15 | import org.junit.Test
16 | import org.junit.runner.RunWith
17 |
18 | /**
19 | * Created by SegunFrancis
20 | */
21 |
22 | @RunWith(AndroidJUnit4::class)
23 | class ToDoDaoTest {
24 | private lateinit var db: ToDoRoomDatabase
25 | private lateinit var dao: ToDoDao
26 | private val now = System.currentTimeMillis()
27 | private val day = 1000 * 60 * 60 * 24
28 |
29 | @Before
30 | fun setup() {
31 | val context = InstrumentationRegistry.getInstrumentation().context
32 | db = Room.inMemoryDatabaseBuilder(context, ToDoRoomDatabase::class.java)
33 | .allowMainThreadQueries().build()
34 | dao = db.todoDao()
35 | }
36 |
37 | @Test
38 | fun test_insertToDoWithDueDate() {
39 | val expected = 1
40 | GlobalScope.launch {
41 | dao.insert(ToDo("1", "First Todo", now + day, false, now))
42 | dao.getDateCount(now).collect { actual ->
43 | assertEquals(expected, actual)
44 | }
45 | }
46 | }
47 |
48 | @Test
49 | fun test_insertToDoWithoutDueDate() {
50 | val expected = 0
51 | GlobalScope.launch {
52 | dao.insert(ToDo("1", "First Todo", null, false, now))
53 | dao.getDateCount(now).collect { actual ->
54 | assertEquals(expected, actual)
55 | }
56 | }
57 | }
58 |
59 | @After
60 | fun cleanup() {
61 | db.close()
62 | }
63 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/project/segunfrancis/yetanothertodoapp/ui/add/AddActivity.kt:
--------------------------------------------------------------------------------
1 | package com.project.segunfrancis.yetanothertodoapp.ui.add
2 |
3 | import androidx.appcompat.app.AppCompatActivity
4 | import android.os.Bundle
5 | import androidx.activity.viewModels
6 | import com.google.android.material.snackbar.Snackbar
7 | import com.project.segunfrancis.yetanothertodoapp.databinding.ActivityAddBinding
8 | import com.project.segunfrancis.yetanothertodoapp.hide
9 | import com.project.segunfrancis.yetanothertodoapp.show
10 | import dagger.hilt.android.AndroidEntryPoint
11 | import java.util.*
12 |
13 | @AndroidEntryPoint
14 | class AddActivity : AppCompatActivity() {
15 |
16 | private val binding: ActivityAddBinding by lazy {
17 | ActivityAddBinding.inflate(layoutInflater)
18 | }
19 |
20 | private val addViewModel: AddViewModel by viewModels()
21 |
22 | override fun onCreate(savedInstanceState: Bundle?) {
23 | super.onCreate(savedInstanceState)
24 | setContentView(binding.root)
25 |
26 | binding.due.date = System.currentTimeMillis()
27 | binding.due.setOnDateChangeListener { _, year, month, day ->
28 | val calendar = Calendar.getInstance()
29 | calendar.set(year, month, day)
30 | addViewModel.toDo.dueDate = calendar.timeInMillis
31 | binding.clearDue.show()
32 | }
33 |
34 | binding.save.setOnClickListener { view ->
35 | addViewModel.toDo.title = binding.txtTitle.text.toString()
36 |
37 | val message = addViewModel.save()
38 | if (message != null) {
39 | Snackbar.make(view, message, Snackbar.LENGTH_LONG).show()
40 | } else {
41 | finish()
42 | }
43 | }
44 |
45 | binding.clearDue.setOnClickListener {
46 | addViewModel.toDo.dueDate = null
47 | binding.clearDue.hide()
48 | binding.due.hide()
49 | binding.setDue.show()
50 | }
51 |
52 | binding.setDue.setOnClickListener {
53 | binding.clearDue.show()
54 | binding.due.show()
55 | binding.setDue.hide()
56 | }
57 | }
58 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/project/segunfrancis/yetanothertodoapp/ui/list/ToDoListActivity.kt:
--------------------------------------------------------------------------------
1 | package com.project.segunfrancis.yetanothertodoapp.ui.list
2 |
3 | import android.content.Intent
4 | import android.os.Bundle
5 | import androidx.activity.viewModels
6 | import androidx.appcompat.app.AppCompatActivity
7 | import androidx.core.view.isVisible
8 | import androidx.recyclerview.widget.LinearLayoutManager
9 | import com.project.segunfrancis.yetanothertodoapp.data.ToDo
10 | import com.project.segunfrancis.yetanothertodoapp.databinding.ActivityToDoListBinding
11 | import com.project.segunfrancis.yetanothertodoapp.ui.add.AddActivity
12 | import dagger.hilt.android.AndroidEntryPoint
13 |
14 | @AndroidEntryPoint
15 | class ToDoListActivity : AppCompatActivity(), OnItemClickListener {
16 |
17 | private val binding: ActivityToDoListBinding by lazy {
18 | ActivityToDoListBinding.inflate(layoutInflater)
19 | }
20 |
21 | private val toDoListViewModel: ToDoListViewModel by viewModels()
22 |
23 | private val toDoAdapter: ToDoAdapter by lazy {
24 | ToDoAdapter(this)
25 | }
26 |
27 | override fun onCreate(savedInstanceState: Bundle?) {
28 | super.onCreate(savedInstanceState)
29 | setContentView(binding.root)
30 | setSupportActionBar(binding.toolbar)
31 |
32 | binding.fab.setOnClickListener {
33 | startActivity(Intent(this@ToDoListActivity, AddActivity::class.java))
34 | }
35 |
36 | binding.include.listTodos.apply {
37 | layoutManager = LinearLayoutManager(this@ToDoListActivity)
38 | adapter = toDoAdapter
39 | }
40 |
41 | toDoListViewModel.toDoList.observe(this, { toDos ->
42 | binding.include.emptyImage.isVisible = toDos.isEmpty()
43 | toDoAdapter.submitList(toDos)
44 | })
45 |
46 | toDoListViewModel.count.observe(this, { count ->
47 | binding.include.soonValue.text = count.toString()
48 | })
49 | }
50 |
51 | override fun onCheckboxChecked(id: String) {
52 | toDoListViewModel.toggleToDo(id)
53 | }
54 |
55 | override fun onDeleteIconClicked(toDo: ToDo) {
56 | toDoListViewModel.deleteToDo(toDo)
57 | }
58 | }
--------------------------------------------------------------------------------
/app/src/test/java/com/project/segunfrancis/yetanothertodoapp/AddViewModelTest.kt:
--------------------------------------------------------------------------------
1 | package com.project.segunfrancis.yetanothertodoapp
2 |
3 | import com.nhaarman.mockitokotlin2.*
4 | import com.project.segunfrancis.yetanothertodoapp.data.ToDoRepository
5 | import com.project.segunfrancis.yetanothertodoapp.ui.add.AddViewModel
6 | import kotlinx.coroutines.ExperimentalCoroutinesApi
7 | import org.junit.Assert.*
8 | import org.junit.Rule
9 | import org.junit.Test
10 |
11 | /**
12 | * Created by SegunFrancis
13 | */
14 |
15 | @ExperimentalCoroutinesApi
16 | class AddViewModelTest {
17 |
18 | @get:Rule
19 | val coroutineRule = MainCoroutineRule()
20 |
21 | @Test
22 | fun test_saveToDoWithoutDate() = coroutineRule.runBlockingTest {
23 | val repository: ToDoRepository = mock()
24 | val model = AddViewModel(repository, coroutineRule.testDispatcher)
25 | val actualTitle = "Test ToDo"
26 | model.toDo.title = actualTitle
27 |
28 | val actual = model.save()
29 |
30 | assertNull(actual)
31 | verify(repository).insert(
32 | argThat {
33 | title == actualTitle && dueDate == null
34 | }
35 | )
36 | }
37 |
38 | @Test
39 | fun test_saveToDoWithDate() = coroutineRule.runBlockingTest {
40 | val repository: ToDoRepository = mock()
41 | val model = AddViewModel(repository, coroutineRule.testDispatcher)
42 | val actualTitle = "Test ToDo"
43 | val actualDate = System.currentTimeMillis()
44 | model.toDo.title = actualTitle
45 | model.toDo.dueDate = actualDate
46 |
47 | val actual = model.save()
48 |
49 | assertNull(actual)
50 | verify(repository).insert(
51 | argThat {
52 | title == actualTitle && dueDate == actualDate
53 | }
54 | )
55 | }
56 |
57 | @Test
58 | fun test_saveToDoNoTitle() = coroutineRule.runBlockingTest {
59 | val repository: ToDoRepository = mock()
60 | val model = AddViewModel(repository, coroutineRule.testDispatcher)
61 | val expected = "Title is required"
62 |
63 | val actual = model.save()
64 |
65 | assertNotNull(actual)
66 | assertEquals(expected, actual)
67 | verify(
68 | repository,
69 | never()
70 | ).insert(any()) // never() prevents the verify() method from being called when a toDo does'nt have a title
71 | }
72 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/content_to_do_list.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
19 |
20 |
29 |
30 |
40 |
41 |
52 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/app/src/main/java/com/project/segunfrancis/yetanothertodoapp/ui/list/ToDoAdapter.kt:
--------------------------------------------------------------------------------
1 | package com.project.segunfrancis.yetanothertodoapp.ui.list
2 |
3 | import android.os.Build
4 | import android.text.format.DateFormat
5 | import androidx.recyclerview.widget.RecyclerView
6 | import android.view.LayoutInflater
7 | import android.view.ViewGroup
8 | import androidx.recyclerview.widget.DiffUtil
9 | import androidx.recyclerview.widget.ListAdapter
10 | import com.project.segunfrancis.yetanothertodoapp.data.ToDo
11 | import com.project.segunfrancis.yetanothertodoapp.databinding.TodoItemBinding
12 | import com.project.segunfrancis.yetanothertodoapp.hide
13 | import java.util.*
14 |
15 | /**
16 | * Created by SegunFrancis
17 | */
18 |
19 | class ToDoAdapter(private val onClickListener: OnItemClickListener) :
20 | ListAdapter(MyDiffUtil()) {
21 |
22 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ToDoViewHolder {
23 | return ToDoViewHolder(
24 | TodoItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
25 | )
26 | }
27 |
28 | override fun onBindViewHolder(holder: ToDoViewHolder, position: Int) =
29 | holder.bind(getItem(position), onClickListener)
30 |
31 | class ToDoViewHolder(private val binding: TodoItemBinding) :
32 | RecyclerView.ViewHolder(binding.root) {
33 | fun bind(toDo: ToDo, clickListener: OnItemClickListener) {
34 | binding.completed.isChecked = toDo.completed
35 | binding.completed.setOnClickListener { clickListener.onCheckboxChecked(toDo.id) }
36 | binding.delete.setOnClickListener { clickListener.onDeleteIconClicked(toDo) }
37 |
38 | binding.title.text = toDo.title
39 | val calender = Calendar.getInstance()
40 | val dateFormat = DateFormat.getDateFormat(binding.root.context)
41 |
42 | calender.timeInMillis = toDo.created
43 | binding.start.text = dateFormat.format(calender.time)
44 |
45 | if (toDo.dueDate != null) {
46 | calender.timeInMillis = toDo.dueDate!!
47 | binding.due.text = dateFormat.format(calender.time)
48 | } else {
49 | binding.due.hide()
50 | binding.dueLabel.hide()
51 | }
52 |
53 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
54 | binding.card.setCardBackgroundColor(
55 | binding.root.context.getColor(
56 | determineCardColor(
57 | toDo.dueDate,
58 | toDo.completed
59 | )
60 | )
61 | )
62 | }
63 | }
64 | }
65 |
66 | class MyDiffUtil : DiffUtil.ItemCallback() {
67 | override fun areItemsTheSame(oldItem: ToDo, newItem: ToDo): Boolean {
68 | return oldItem.id == newItem.id
69 | }
70 |
71 |
72 | override fun areContentsTheSame(oldItem: ToDo, newItem: ToDo): Boolean {
73 | return oldItem == newItem
74 | }
75 | }
76 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | # Yet-Another-Todo-App
5 | A simple android Todo app built using MVVM architecture, repository pattern and Jetpack library that allows users to add a todo and optionally specify a due date. This project contains unit tests, integrated tests and user interface tests.
6 |
7 | ## SDK Requirements
8 | - Minimum SDK Requirement - android API 21 (Lollipop)
9 | - Target SDK - android API 29 (Android 10)
10 |
11 | ## Installation
12 | To run this code, clone this repository and import into android studio
13 |
14 | `git clone https://github.com/segunfrancis/Yet-Another_todo-App.git`
15 |
16 | ## Screenshots
17 |
23 |
24 | ## Built With
25 |
26 | * [Android Jetpack](https://developer.android.com/jetpack) - Suite of libraries, tools, and guidance to help developers write high-quality apps easier.
27 | * [Android KTX](https://developer.android.com/kotlin/ktx)
28 | * [ViewBinding](https://developer.android.com/topic/libraries/view-binding)
29 | * [LiveData](https://developer.android.com/topic/libraries/architecture/livedata)
30 | * [ViewModel](https://developer.android.com/topic/libraries/architecture/viewmodel)
31 | * [Room](https://developer.android.com/topic/libraries/architecture/room)
32 | * [Kotlin Coroutines](https://developer.android.com/kotlin/coroutines) - Concurrency design pattern used on Android to simplify code that executes asynchronously.
33 | * [Flow](https://kotlinlang.org/docs/reference/coroutines/flow.html) - Kotlin API that can return multiple asynchronously computed values.
34 | * [Hilt](https://developer.android.com/training/dependency-injection/hilt-android) - A dependency injection library for Android that reduces the boilerplate of doing manual dependency injection in your project.
35 | * [Mockito](https://site.mockito.org/) - Mockito is a mocking framework that lets you write beautiful tests with a clean & simple API.
36 | * [Espresso](https://developer.android.com/training/testing/espresso) - User Interface testing library for android.
37 | * [Mockito-Kotlin](https://github.com/nhaarman/mockito-kotlin) - A library that provides helper functions to work with Mockito in kotlin.
38 |
39 |
40 |
41 | ## Author
42 |
43 | * **Segun Francis**
44 | - [Twitter](https://twitter.com/segun__francis)
45 | - [LinkedIn](https://www.linkedin.com/in/segun-francis-302361a1)
46 |
47 |
48 | ## License
49 |
50 | Copyright 2020 Segun Francis
51 |
52 | Licensed under the Apache License, Version 2.0 (the "License");
53 | you may not use this file except in compliance with the License.
54 | You may obtain a copy of the License at
55 |
56 | http://www.apache.org/licenses/LICENSE-2.0
57 |
58 | Unless required by applicable law or agreed to in writing, software
59 | distributed under the License is distributed on an "AS IS" BASIS,
60 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
61 | See the License for the specific language governing permissions and
62 | limitations under the License.
63 |
64 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 | apply plugin: 'kotlin-android'
3 | apply plugin: 'kotlin-android-extensions'
4 | apply plugin: 'kotlin-kapt'
5 | apply plugin: 'dagger.hilt.android.plugin'
6 |
7 | android {
8 | compileSdkVersion 30
9 | buildToolsVersion "30.0.1"
10 |
11 | defaultConfig {
12 | applicationId "com.project.segunfrancis.yetanothertodoapp"
13 | minSdkVersion 21
14 | targetSdkVersion 30
15 | versionCode 1
16 | versionName "1.0"
17 | multiDexEnabled true
18 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
19 | }
20 | buildFeatures {
21 | viewBinding true
22 | }
23 | buildTypes {
24 | release {
25 | minifyEnabled false
26 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
27 | }
28 | }
29 | compileOptions {
30 | sourceCompatibility JavaVersion.VERSION_1_8
31 | targetCompatibility JavaVersion.VERSION_1_8
32 | }
33 | kotlinOptions {
34 | jvmTarget = '1.8'
35 | freeCompilerArgs += [
36 | "-Xuse-experimental=kotlinx.coroutines.ExperimentalCoroutinesApi",
37 | "-Xuse-experimental=kotlinx.coroutines.ObsoleteCoroutinesApi"]
38 | }
39 | testOptions {
40 | unitTests {
41 | includeAndroidResources = true
42 | }
43 | }
44 | }
45 |
46 | dependencies {
47 | implementation fileTree(dir: "libs", include: ["*.jar"])
48 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
49 | implementation "androidx.core:core-ktx:$core_version"
50 | implementation "androidx.appcompat:appcompat:$appcompat_version"
51 | implementation "com.google.android.material:material:$material_version"
52 | implementation "androidx.constraintlayout:constraintlayout:$constraintLayout_version"
53 | implementation "androidx.recyclerview:recyclerview:$recyclerview_version"
54 | implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
55 | implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
56 | implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
57 | kapt "androidx.room:room-compiler:$room_version"
58 | implementation "androidx.room:room-runtime:$room_version"
59 | implementation "androidx.room:room-ktx:$room_version"
60 | implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutine_version"
61 | implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutine_version"
62 | implementation "com.google.dagger:hilt-android:$hilt_version"
63 | kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
64 | kapt "androidx.hilt:hilt-compiler:$hilt_viewmodel_version"
65 | implementation "androidx.hilt:hilt-lifecycle-viewmodel:$hilt_viewmodel_version"
66 | implementation "androidx.activity:activity-ktx:1.1.0"
67 | implementation 'androidx.multidex:multidex:2.0.1'
68 |
69 | // Testing
70 | testImplementation "junit:junit:$jUnit_version"
71 | testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.9"
72 | testImplementation "org.mockito:mockito-inline:$mockito_version"
73 | testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:$nhaarman_mockito_version"
74 | testImplementation "androidx.arch.core:core-testing:$androidx_core_test_version"
75 | testImplementation "androidx.test:core-ktx:$androidx_test_version"
76 | testImplementation "androidx.test.ext:junit:$androidx_junit_version"
77 | androidTestImplementation "androidx.test:runner:$androidx_test_version"
78 | androidTestImplementation "androidx.test.ext:junit:$androidx_junit_version"
79 | androidTestImplementation "androidx.test.espresso:espresso-core:$espresso_version"
80 | androidTestImplementation "androidx.test.espresso:espresso-intents:$espresso_version"
81 | androidTestImplementation "androidx.test.espresso:espresso-contrib:$espresso_version"
82 | androidTestImplementation "androidx.test.espresso:espresso-idling-resource:$espresso_version"
83 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_add.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
23 |
24 |
29 |
30 |
31 |
41 |
42 |
52 |
53 |
60 |
61 |
70 |
71 |
85 |
86 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/todo_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
18 |
19 |
22 |
23 |
32 |
33 |
43 |
44 |
54 |
55 |
63 |
64 |
74 |
75 |
83 |
84 |
92 |
93 |
94 |
95 |
96 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 | xmlns:android
33 |
34 | ^$
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 | xmlns:.*
44 |
45 | ^$
46 |
47 |
48 | BY_NAME
49 |
50 |
51 |
52 |
53 |
54 |
55 | .*:id
56 |
57 | http://schemas.android.com/apk/res/android
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 | .*:name
67 |
68 | http://schemas.android.com/apk/res/android
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 | name
78 |
79 | ^$
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 | style
89 |
90 | ^$
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 | .*
100 |
101 | ^$
102 |
103 |
104 | BY_NAME
105 |
106 |
107 |
108 |
109 |
110 |
111 | .*
112 |
113 | http://schemas.android.com/apk/res/android
114 |
115 |
116 | ANDROID_ATTRIBUTE_ORDER
117 |
118 |
119 |
120 |
121 |
122 |
123 | .*
124 |
125 | .*
126 |
127 |
128 | BY_NAME
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/test/java/com/project/segunfrancis/yetanothertodoapp/ListViewModelTest.kt:
--------------------------------------------------------------------------------
1 | package com.project.segunfrancis.yetanothertodoapp
2 |
3 | import com.nhaarman.mockitokotlin2.mock
4 | import com.nhaarman.mockitokotlin2.verify
5 | import com.nhaarman.mockitokotlin2.whenever
6 | import com.project.segunfrancis.yetanothertodoapp.data.ToDo
7 | import com.project.segunfrancis.yetanothertodoapp.data.ToDoRepository
8 | import com.project.segunfrancis.yetanothertodoapp.ui.add.AddViewModel
9 | import com.project.segunfrancis.yetanothertodoapp.ui.list.ToDoListViewModel
10 | import kotlinx.coroutines.ExperimentalCoroutinesApi
11 | import kotlinx.coroutines.flow.collect
12 | import kotlinx.coroutines.flow.flowOf
13 | import org.junit.Assert.assertEquals
14 | import org.junit.Assert.assertNotNull
15 | import org.junit.Rule
16 | import org.junit.Test
17 | import org.junit.rules.ExpectedException
18 | import org.junit.runner.RunWith
19 | import org.junit.runners.BlockJUnit4ClassRunner
20 | import java.lang.IllegalArgumentException
21 |
22 | /**
23 | * Created by SegunFrancis
24 | */
25 |
26 | @ExperimentalCoroutinesApi
27 | @RunWith(BlockJUnit4ClassRunner::class)
28 | class ListViewModelTest {
29 |
30 | private val now = System.currentTimeMillis()
31 | private val day = 1000 * 60 * 60 * 24
32 |
33 | @get:Rule
34 | val exceptionRule = ExpectedException.none()
35 |
36 | @get:Rule
37 | val coroutineRule: MainCoroutineRule = MainCoroutineRule()
38 |
39 | @Test
40 | fun test_allToDosEmpty() = coroutineRule.runBlockingTest {
41 | val expected = 0
42 | val repository: ToDoRepository = mock()
43 | whenever(repository.getAllToDos())
44 | .thenReturn(flowOf(arrayListOf()))
45 | val model = ToDoListViewModel(repository, coroutineRule.testDispatcher)
46 | val toDos = model.allToDos
47 |
48 | assertNotNull(toDos)
49 | toDos.collect {
50 | assertEquals(expected, it.size)
51 | }
52 | }
53 |
54 | @Test
55 | fun test_allToDosSingle() = coroutineRule.runBlockingTest {
56 | val expected = 1
57 | val repository: ToDoRepository = mock()
58 | whenever(repository.getAllToDos())
59 | .thenReturn(
60 | flowOf(
61 | arrayListOf(
62 | ToDo("5", "Todo 5", now + day, false, now)
63 | )
64 | )
65 | )
66 | val model = ToDoListViewModel(repository, coroutineRule.testDispatcher)
67 | val toDos = model.allToDos
68 |
69 | assertNotNull(toDos)
70 | toDos.collect {
71 | assertEquals(expected, it.size)
72 | }
73 | }
74 |
75 | @Test
76 | fun test_allToDosMultiple() = coroutineRule.runBlockingTest {
77 | val expected = 3
78 | val repository: ToDoRepository = mock()
79 | whenever(repository.getAllToDos())
80 | .thenReturn(
81 | flowOf(
82 | arrayListOf(
83 | ToDo("5", "Todo 5", now + day, false, now),
84 | ToDo("3", "Todo 3", now + day, false, now),
85 | ToDo("2", "Todo 2", now + day, false, now)
86 | )
87 | )
88 | )
89 | val model = ToDoListViewModel(repository, coroutineRule.testDispatcher)
90 | val toDos = model.allToDos
91 |
92 | assertNotNull(toDos)
93 | toDos.collect {
94 | assertEquals(expected, it.size)
95 | }
96 | }
97 |
98 | @Test
99 | fun test_upcomingToDosCountEmpty() = coroutineRule.runBlockingTest {
100 | val expected = 0
101 | val repository: ToDoRepository = mock()
102 | whenever(repository.getUpcomingToDosCount())
103 | .thenReturn(flowOf(expected))
104 | val model = ToDoListViewModel(repository, coroutineRule.testDispatcher)
105 | val count = model.upcomingToDosCount
106 |
107 | assertNotNull(count)
108 | count.collect {
109 | assertEquals(expected, it)
110 | }
111 | }
112 |
113 | @Test
114 | fun test_upcomingToDosCountSingle() = coroutineRule.runBlockingTest {
115 | val expected = 1
116 | val repository: ToDoRepository = mock()
117 | whenever(repository.getUpcomingToDosCount())
118 | .thenReturn(flowOf(expected))
119 | val model = ToDoListViewModel(repository, coroutineRule.testDispatcher)
120 | val count = model.upcomingToDosCount
121 |
122 | assertNotNull(count)
123 | count.collect {
124 | assertEquals(expected, it)
125 | }
126 | }
127 |
128 | @Test
129 | fun test_upcomingToDosCountMultiple() = coroutineRule.runBlockingTest {
130 | val expected = 5
131 | val repository: ToDoRepository = mock()
132 | whenever(repository.getUpcomingToDosCount())
133 | .thenReturn(flowOf(expected))
134 | val model = ToDoListViewModel(repository, coroutineRule.testDispatcher)
135 | val count = model.upcomingToDosCount
136 |
137 | assertNotNull(count)
138 | count.collect {
139 | assertEquals(expected, it)
140 | }
141 | }
142 |
143 | @Test
144 | fun test_toggleToDo() = coroutineRule.runBlockingTest {
145 | val id = "fake"
146 | val repository: ToDoRepository = mock()
147 | val model = ToDoListViewModel(repository, coroutineRule.testDispatcher)
148 |
149 | model.toggleToDo(id)
150 |
151 | verify(repository)
152 | .toggleTodo(id)
153 | }
154 |
155 | @Test
156 | fun test_toggleToDoNotFound() = coroutineRule.runBlockingTest {
157 | val repository: ToDoRepository = mock()
158 | val id = "fake"
159 | val exceptionMessage = "Todo not found"
160 | whenever(repository.toggleTodo(id))
161 | .thenThrow(IllegalArgumentException(exceptionMessage))
162 | val model = ToDoListViewModel(repository, coroutineRule.testDispatcher)
163 | exceptionRule.expect(IllegalArgumentException::class.java)
164 | exceptionRule.expectMessage(exceptionMessage)
165 | model.toggleToDo(id)
166 | verify(repository)
167 | .toggleTodo(id)
168 | }
169 |
170 | @Test
171 | fun test_deleteToDo() = coroutineRule.runBlockingTest {
172 | val repository: ToDoRepository = mock()
173 | val model = AddViewModel(repository, coroutineRule.testDispatcher)
174 | val listModel = ToDoListViewModel(repository, coroutineRule.testDispatcher)
175 | val actualTitle = "Test ToDo"
176 | val actualDate = System.currentTimeMillis()
177 | val toDo = ToDo("1", actualTitle, now + day, true, actualDate)
178 | model.toDo.title = toDo.title
179 | model.toDo.dueDate = toDo.dueDate
180 | model.save()
181 |
182 | listModel.deleteToDo(toDo)
183 | verify(repository).delete(toDo)
184 | }
185 | }
--------------------------------------------------------------------------------