Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: codinginflow/MVVMTodo
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: codinginflow/MVVMTodo
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: Part-14_Restoring-the-Fragment-SearchView
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 13 commits
  • 26 files changed
  • 1 contributor

Commits on Nov 11, 2020

  1. End of part 2

    codinginflow committed Nov 11, 2020
    Copy the full SHA
    a1b4618 View commit details

Commits on Nov 12, 2020

  1. End of part 4

    codinginflow committed Nov 12, 2020
    Copy the full SHA
    9853a82 View commit details

Commits on Nov 13, 2020

  1. End of part 5

    codinginflow committed Nov 13, 2020
    Copy the full SHA
    79644cf View commit details

Commits on Nov 14, 2020

  1. End of part 6

    codinginflow committed Nov 14, 2020
    Copy the full SHA
    8dcc06a View commit details
  2. End of part 7

    codinginflow committed Nov 14, 2020
    Copy the full SHA
    9c21a1e View commit details
  3. End of part 8

    codinginflow committed Nov 14, 2020
    Copy the full SHA
    77d8d8f View commit details
  4. End of part 9

    codinginflow committed Nov 14, 2020
    Copy the full SHA
    aa3ece0 View commit details
  5. End of part 10

    codinginflow committed Nov 14, 2020
    Copy the full SHA
    477da74 View commit details

Commits on Nov 16, 2020

  1. End of part 11

    codinginflow committed Nov 16, 2020
    Copy the full SHA
    047a1c8 View commit details
  2. End of part 12

    codinginflow committed Nov 16, 2020
    Copy the full SHA
    6bec02e View commit details
  3. End of part 13

    codinginflow committed Nov 16, 2020
    Copy the full SHA
    cbc1eef View commit details
  4. End of part 14

    codinginflow committed Nov 16, 2020
    Copy the full SHA
    4517c20 View commit details
  5. Formatting code + small corrections after finishing the tutorial

    codinginflow authored and Florian Walther committed Nov 16, 2020
    Copy the full SHA
    bc2b89c View commit details
Showing with 1,113 additions and 21 deletions.
  1. +139 −0 .idea/codeStyles/Project.xml
  2. +5 −0 .idea/codeStyles/codeStyleConfig.xml
  3. +4 −1 app/src/main/AndroidManifest.xml
  4. +0 −11 app/src/main/java/com/codinginflow/mvvmtodo/MainActivity.kt
  5. +8 −0 app/src/main/java/com/codinginflow/mvvmtodo/ToDoApplication.kt
  6. +60 −0 app/src/main/java/com/codinginflow/mvvmtodo/data/PreferencesManager.kt
  7. +20 −0 app/src/main/java/com/codinginflow/mvvmtodo/data/Task.kt
  8. +32 −0 app/src/main/java/com/codinginflow/mvvmtodo/data/TaskDao.kt
  9. +39 −0 app/src/main/java/com/codinginflow/mvvmtodo/data/TaskDatabase.kt
  10. +40 −0 app/src/main/java/com/codinginflow/mvvmtodo/di/AppModule.kt
  11. +34 −0 app/src/main/java/com/codinginflow/mvvmtodo/ui/MainActivity.kt
  12. +68 −0 app/src/main/java/com/codinginflow/mvvmtodo/ui/addedittask/AddEditTaskFragment.kt
  13. +71 −0 app/src/main/java/com/codinginflow/mvvmtodo/ui/addedittask/AddEditTaskViewModel.kt
  14. +24 −0 ...src/main/java/com/codinginflow/mvvmtodo/ui/deleteallcompleted/DeleteAllCompletedDialogFragment.kt
  15. +18 −0 app/src/main/java/com/codinginflow/mvvmtodo/ui/deleteallcompleted/DeleteAllCompletedViewModel.kt
  16. +69 −0 app/src/main/java/com/codinginflow/mvvmtodo/ui/tasks/TasksAdapter.kt
  17. +179 −0 app/src/main/java/com/codinginflow/mvvmtodo/ui/tasks/TasksFragment.kt
  18. +93 −0 app/src/main/java/com/codinginflow/mvvmtodo/ui/tasks/TasksViewModel.kt
  19. +4 −0 app/src/main/java/com/codinginflow/mvvmtodo/util/Utils.kt
  20. +16 −0 app/src/main/java/com/codinginflow/mvvmtodo/util/ViewExt.kt
  21. +8 −9 app/src/main/res/layout/activity_main.xml
  22. +43 −0 app/src/main/res/layout/fragment_add_edit_task.xml
  23. +21 −0 app/src/main/res/layout/fragment_tasks.xml
  24. +38 −0 app/src/main/res/layout/item_task.xml
  25. +42 −0 app/src/main/res/menu/menu_fragment_tasks.xml
  26. +38 −0 app/src/main/res/navigation/nav_graph.xml
139 changes: 139 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -3,13 +3,16 @@
package="com.codinginflow.mvvmtodo">

<application
android:name=".ToDoApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MVVMTodo">
<activity android:name="com.codinginflow.mvvmtodo.MainActivity">
<activity
android:name="com.codinginflow.mvvmtodo.ui.MainActivity"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

11 changes: 0 additions & 11 deletions app/src/main/java/com/codinginflow/mvvmtodo/MainActivity.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.codinginflow.mvvmtodo

import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class ToDoApplication : Application() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.codinginflow.mvvmtodo.data

import android.content.Context
import android.util.Log
import androidx.datastore.preferences.createDataStore
import androidx.datastore.preferences.edit
import androidx.datastore.preferences.emptyPreferences
import androidx.datastore.preferences.preferencesKey
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.map
import java.io.IOException
import javax.inject.Inject
import javax.inject.Singleton

private const val TAG = "PreferencesManager"

enum class SortOrder { BY_NAME, BY_DATE }

data class FilterPreferences(val sortOrder: SortOrder, val hideCompleted: Boolean)

@Singleton
class PreferencesManager @Inject constructor(@ApplicationContext context: Context) {

private val dataStore = context.createDataStore("user_preferences")

val preferencesFlow = dataStore.data
.catch { exception ->
if (exception is IOException) {
Log.e(TAG, "Error reading preferences", exception)
emit(emptyPreferences())
} else {
throw exception
}
}
.map { preferences ->
val sortOrder = SortOrder.valueOf(
preferences[PreferencesKeys.SORT_ORDER] ?: SortOrder.BY_DATE.name
)
val hideCompleted = preferences[PreferencesKeys.HIDE_COMPLETED] ?: false
FilterPreferences(sortOrder, hideCompleted)
}

suspend fun updateSortOrder(sortOrder: SortOrder) {
dataStore.edit { preferences ->
preferences[PreferencesKeys.SORT_ORDER] = sortOrder.name
}
}

suspend fun updateHideCompleted(hideCompleted: Boolean) {
dataStore.edit { preferences ->
preferences[PreferencesKeys.HIDE_COMPLETED] = hideCompleted
}
}

private object PreferencesKeys {
val SORT_ORDER = preferencesKey<String>("sort_order")
val HIDE_COMPLETED = preferencesKey<Boolean>("hide_completed")
}
}
20 changes: 20 additions & 0 deletions app/src/main/java/com/codinginflow/mvvmtodo/data/Task.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.codinginflow.mvvmtodo.data

import android.os.Parcelable
import androidx.room.Entity
import androidx.room.PrimaryKey
import kotlinx.android.parcel.Parcelize
import java.text.DateFormat

@Entity(tableName = "task_table")
@Parcelize
data class Task(
val name: String,
val important: Boolean = false,
val completed: Boolean = false,
val created: Long = System.currentTimeMillis(),
@PrimaryKey(autoGenerate = true) val id: Int = 0
) : Parcelable {
val createdDateFormatted: String
get() = DateFormat.getDateTimeInstance().format(created)
}
32 changes: 32 additions & 0 deletions app/src/main/java/com/codinginflow/mvvmtodo/data/TaskDao.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.codinginflow.mvvmtodo.data

import androidx.room.*
import kotlinx.coroutines.flow.Flow

@Dao
interface TaskDao {

fun getTasks(query: String, sortOrder: SortOrder, hideCompleted: Boolean): Flow<List<Task>> =
when (sortOrder) {
SortOrder.BY_DATE -> getTasksSortedByDateCreated(query, hideCompleted)
SortOrder.BY_NAME -> getTasksSortedByName(query, hideCompleted)
}

@Query("SELECT * FROM task_table WHERE (completed != :hideCompleted OR completed = 0) AND name LIKE '%' || :searchQuery || '%' ORDER BY important DESC, name")
fun getTasksSortedByName(searchQuery: String, hideCompleted: Boolean): Flow<List<Task>>

@Query("SELECT * FROM task_table WHERE (completed != :hideCompleted OR completed = 0) AND name LIKE '%' || :searchQuery || '%' ORDER BY important DESC, created")
fun getTasksSortedByDateCreated(searchQuery: String, hideCompleted: Boolean): Flow<List<Task>>

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(task: Task)

@Update
suspend fun update(task: Task)

@Delete
suspend fun delete(task: Task)

@Query("DELETE FROM task_table WHERE completed = 1")
suspend fun deleteCompletedTasks()
}
39 changes: 39 additions & 0 deletions app/src/main/java/com/codinginflow/mvvmtodo/data/TaskDatabase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.codinginflow.mvvmtodo.data

import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase
import com.codinginflow.mvvmtodo.di.ApplicationScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import javax.inject.Inject
import javax.inject.Provider

@Database(entities = [Task::class], version = 1)
abstract class TaskDatabase : RoomDatabase() {

abstract fun taskDao(): TaskDao

class Callback @Inject constructor(
private val database: Provider<TaskDatabase>,
@ApplicationScope private val applicationScope: CoroutineScope
) : RoomDatabase.Callback() {

override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)

val dao = database.get().taskDao()

applicationScope.launch {
dao.insert(Task("Wash the dishes"))
dao.insert(Task("Do the laundry"))
dao.insert(Task("Buy groceries", important = true))
dao.insert(Task("Prepare food", completed = true))
dao.insert(Task("Call mom"))
dao.insert(Task("Visit grandma", completed = true))
dao.insert(Task("Repair my bike"))
dao.insert(Task("Call Elon Musk"))
}
}
}
}
40 changes: 40 additions & 0 deletions app/src/main/java/com/codinginflow/mvvmtodo/di/AppModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.codinginflow.mvvmtodo.di

import android.app.Application
import androidx.room.Room
import com.codinginflow.mvvmtodo.data.TaskDatabase
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ApplicationComponent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import javax.inject.Qualifier
import javax.inject.Singleton

@Module
@InstallIn(ApplicationComponent::class)
object AppModule {

@Provides
@Singleton
fun provideDatabase(
app: Application,
callback: TaskDatabase.Callback
) = Room.databaseBuilder(app, TaskDatabase::class.java, "task_database")
.fallbackToDestructiveMigration()
.addCallback(callback)
.build()

@Provides
fun provideTaskDao(db: TaskDatabase) = db.taskDao()

@ApplicationScope
@Provides
@Singleton
fun provideApplicationScope() = CoroutineScope(SupervisorJob())
}

@Retention(AnnotationRetention.RUNTIME)
@Qualifier
annotation class ApplicationScope
Loading