Skip to content
This repository was archived by the owner on Nov 16, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions base/notepad/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
49 changes: 49 additions & 0 deletions base/notepad/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
id 'kotlin-kapt'
id 'kotlin-android-extensions'
}

apply from: rootProject.file('dependencies.gradle')
apply from: rootProject.file('versions.gradle')

android {
compileSdkVersion versions.compileSdk

defaultConfig {
minSdkVersion versions.minSdk
targetSdkVersion versions.targetSdk
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
// Because of error More than one file was found with OS independent path 'META-INF/notepad_debug.kotlin_module'.
freeCompilerArgs = ["-module-name", project.path.substring(1).replace(':', '-')]
jvmTarget = '1.8'
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
test.java.srcDirs += 'src/test/kotlin'
androidTest.java.srcDirs += 'src/androidTest/kotlin'
}
androidExtensions {
experimental = true
}
}

dependencies {
implementation project(":core")

implementation(
deps.kotlin.stdlib,
deps.dagger.dagger,
deps.material.material,
deps.constraint.constraint
)
kapt(
deps.dagger.processor
)
}
1 change: 1 addition & 0 deletions base/notepad/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions base/notepad/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="com.dvinc.base.notepad"/>
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
* All rights reserved.
*/

package com.dvinc.notepad.data.mapper.note
package com.dvinc.base.notepad.data.mapper.note

import com.dvinc.core.database.entity.note.NoteEntity
import com.dvinc.notepad.domain.model.note.Note
import com.dvinc.base.notepad.domain.model.Note
import javax.inject.Inject

class NoteDataMapper @Inject constructor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* All rights reserved.
*/

package com.dvinc.notepad.domain.model.note
package com.dvinc.base.notepad.domain.model

data class Note(
val id: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* All rights reserved.
*/

package com.dvinc.notepad.presentation.adapter.notepad
package com.dvinc.base.notepad.presentation.adapter.notepad

import android.view.View
import com.dvinc.core.ui.BaseViewHolder
import com.dvinc.notepad.presentation.model.NoteUi
import com.dvinc.base.notepad.presentation.model.NoteUi
import kotlinx.android.synthetic.main.item_note.item_note_content as content
import kotlinx.android.synthetic.main.item_note.item_note_name as name
import kotlinx.android.synthetic.main.item_note.item_note_updating_time as updateTime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
* All rights reserved.
*/

package com.dvinc.notepad.presentation.adapter.notepad
package com.dvinc.base.notepad.presentation.adapter.notepad

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.dvinc.notepad.R
import com.dvinc.notepad.presentation.model.NoteUi
import com.dvinc.base.notepad.R
import com.dvinc.base.notepad.presentation.model.NoteUi

class NotepadAdapter : RecyclerView.Adapter<NoteViewHolder>() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
* All rights reserved.
*/

package com.dvinc.notepad.presentation.adapter.notepad
package com.dvinc.base.notepad.presentation.adapter.notepad

import androidx.recyclerview.widget.DiffUtil
import com.dvinc.notepad.presentation.model.NoteUi
import com.dvinc.base.notepad.presentation.model.NoteUi

class NotepadDiffUtilsCallback(
private val oldNotesList: List<NoteUi>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
* All rights reserved.
*/

package com.dvinc.notepad.presentation.adapter.notepad
package com.dvinc.base.notepad.presentation.adapter.notepad

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.dvinc.notepad.R
import com.dvinc.notepad.presentation.model.NoteUi
import com.dvinc.base.notepad.R
import com.dvinc.base.notepad.presentation.model.NoteUi

class NotepadSwipeToDeleteCallback(
class NotepadSwipeCallback(
private val notesAdapter: NotepadAdapter,
private val onItemSwipedListener: (note: NoteUi) -> Unit
private val onItemSwipedListener: (note: NoteUi, swipeDirection: NotepadSwipeDirection) -> Unit
) : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {

companion object {
Expand All @@ -24,6 +24,8 @@ class NotepadSwipeToDeleteCallback(

private val deletedItemBackground = ColorDrawable(Color.RED)

private val archiveItemBackground = ColorDrawable(Color.GREEN)

override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
Expand All @@ -35,7 +37,12 @@ class NotepadSwipeToDeleteCallback(
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
val position = viewHolder.adapterPosition
val swipedNote = notesAdapter.getItem(position)
onItemSwipedListener.invoke(swipedNote)
// (!) Be careful about directions
if (direction == ItemTouchHelper.RIGHT) {
onItemSwipedListener.invoke(swipedNote, NotepadSwipeDirection.LEFT)
} else if (direction == ItemTouchHelper.LEFT) {
onItemSwipedListener.invoke(swipedNote, NotepadSwipeDirection.RIGHT)
}
}

override fun onChildDraw(
Expand All @@ -56,7 +63,7 @@ class NotepadSwipeToDeleteCallback(

when {
dX > 0 -> {
deletedItemBackground.setBounds(
archiveItemBackground.setBounds(
itemView.left,
itemView.top,
itemView.left + (dX.toInt() + BACKGROUND_CORNER_OFFSET),
Expand All @@ -80,9 +87,11 @@ class NotepadSwipeToDeleteCallback(
}
else -> {
deletedItemBackground.setBounds(0, 0, 0, 0)
archiveItemBackground.setBounds(0, 0, 0, 0)
}
}
deletedItemBackground.draw(c)
archiveItemBackground.draw(c)
deleteIcon.draw(c)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.dvinc.base.notepad.presentation.adapter.notepad

enum class NotepadSwipeDirection {

LEFT,

RIGHT
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
* All rights reserved.
*/

package com.dvinc.notepad.presentation.mapper
package com.dvinc.base.notepad.presentation.mapper

import com.dvinc.notepad.domain.model.note.Note
import com.dvinc.notepad.presentation.model.NoteUi
import com.dvinc.base.notepad.domain.model.Note
import com.dvinc.base.notepad.presentation.model.NoteUi
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* All rights reserved.
*/

package com.dvinc.notepad.presentation.model
package com.dvinc.base.notepad.presentation.model

data class NoteUi(
val id: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<item>
<shape>
<solid android:color="@color/noteItemBackgroundColor" />
<corners android:radius="10.0dp" />
<corners android:radius="10dp" />
</shape>
</item>
</ripple>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="noteItemBackgroundColor">@color/darkItemBackgroundColor</color>
<color name="noteItemBackgroundColor">@color/darkItemBackground</color>
<color name="noteTitleTextColor">@color/darkTextColor</color>
<color name="noteContentTextColor">@color/darkTextColor</color>
</resources>
</resources>
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="darkItemBackgroundColor">#1F1A24</color>

<color name="noteItemBackgroundColor">@color/white</color>
<color name="noteTitleTextColor">@color/black</color>
<color name="noteContentTextColor">@color/gray</color>
Expand Down
8 changes: 8 additions & 0 deletions base/notepad/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="error_while_load_data_from_db">An error has occurred while loading from database</string>
<string name="error_while_deleting_note">An error has occurred while deleting note</string>
<string name="error_while_loading_note">Cannot load note from database</string>
<string name="error_while_adding_note">Cannot add note</string>
<string name="error_while_archiving_note">An error has occurred while archiving note</string>
</resources>
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="NoteHeaderStyle">
<item name="android:textColor">@color/noteTitleTextColor</item>
<item name="android:textSize">20sp</item>
Expand All @@ -10,5 +9,4 @@
<item name="android:textColor">@color/noteContentTextColor</item>
<item name="android:textSize">14sp</item>
</style>

</resources>
10 changes: 9 additions & 1 deletion core/src/main/kotlin/com/dvinc/core/database/NotepadDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ package com.dvinc.core.database

import androidx.room.Database
import androidx.room.RoomDatabase
import com.dvinc.core.database.dao.archive.ArchiveDao
import com.dvinc.core.database.dao.note.NoteDao
import com.dvinc.core.database.entity.note.NoteEntity

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

abstract fun notesDao(): NoteDao

abstract fun archiveDao(): ArchiveDao
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.dvinc.core.database.dao.archive

import androidx.room.Dao
import androidx.room.Query
import com.dvinc.core.database.entity.note.NoteEntity
import kotlinx.coroutines.flow.Flow

@Dao
interface ArchiveDao {

@Query("UPDATE Notes SET is_archived = 1 WHERE id =:noteId")
suspend fun markNoteAsArchived(noteId: Long)

@Query("SELECT * FROM Notes WHERE is_archived = 1 ORDER BY id DESC")
fun getArchive(): Flow<List<NoteEntity>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface NoteDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun addNote(note: NoteEntity)

@Query("SELECT * FROM Notes ORDER BY id DESC")
@Query("SELECT * FROM Notes WHERE is_archived = 0 ORDER BY id DESC")
fun getNotes(): Flow<List<NoteEntity>>

@Query("DELETE FROM Notes WHERE id = :id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ data class NoteEntity(
val content: String,

@ColumnInfo(name = "update_time")
val updateTime: Long
val updateTime: Long,

@ColumnInfo(name = "is_archived")
val isArchived: Boolean = false
)
15 changes: 14 additions & 1 deletion core/src/main/kotlin/com/dvinc/core/ui/BaseViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package com.dvinc.core.ui

import androidx.annotation.StringRes
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.navigation.NavDirections

abstract class BaseViewModel : ViewModel() {
abstract class BaseViewModel<State : ViewState>(initialViewState: State? = null) : ViewModel() {

val viewState = MutableLiveData<State>()

val viewCommands = CommandsLiveData<ViewCommand>()

init {
setInitialViewState(initialViewState)
}

protected fun showMessage(@StringRes messageResId: Int) {
val showMessageCommand = ShowMessage(messageResId)
viewCommands.onNext(showMessageCommand)
Expand All @@ -26,4 +33,10 @@ abstract class BaseViewModel : ViewModel() {
protected fun navigateBack() {
viewCommands.onNext(NavigateUp)
}

private fun setInitialViewState(initialViewState: State?) {
initialViewState?.let {
viewState.value = it
}
}
}
3 changes: 3 additions & 0 deletions core/src/main/kotlin/com/dvinc/core/ui/ViewState.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.dvinc.core.ui

interface ViewState
1 change: 1 addition & 0 deletions core/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<color name="darkScreenBackgroundColor">#121212</color>
<color name="darkTextColor">#888888</color>
<color name="darkTextHintColor">#AAAAAA</color>
<color name="darkItemBackground">#1F1A24</color>

<color name="screenBackgroundColor">@color/white</color>
<color name="editTextColor">@color/black</color>
Expand Down
1 change: 1 addition & 0 deletions features/archive/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
Loading