├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── ic_launcher_background.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── themes.xml
│ │ │ ├── drawable
│ │ │ │ └── notepad_icon.png
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ ├── ic_launcher_round.png
│ │ │ │ └── ic_launcher_foreground.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ ├── ic_launcher_round.png
│ │ │ │ └── ic_launcher_foreground.png
│ │ │ ├── font
│ │ │ │ └── varela_round_regular.ttf
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ ├── ic_launcher_round.png
│ │ │ │ └── ic_launcher_foreground.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ ├── ic_launcher_round.png
│ │ │ │ └── ic_launcher_foreground.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ ├── ic_launcher_round.png
│ │ │ │ └── ic_launcher_foreground.png
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── values-night
│ │ │ │ └── themes.xml
│ │ │ └── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ ├── ic_launcher-playstore.png
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── akshayashokcode
│ │ │ │ └── notepad
│ │ │ │ ├── NoteApp.kt
│ │ │ │ ├── feature_note
│ │ │ │ ├── domain
│ │ │ │ │ ├── util
│ │ │ │ │ │ ├── OrderType.kt
│ │ │ │ │ │ └── NoteOrder.kt
│ │ │ │ │ ├── use_case
│ │ │ │ │ │ ├── NoteUseCases.kt
│ │ │ │ │ │ ├── DeleteNote.kt
│ │ │ │ │ │ ├── GetNote.kt
│ │ │ │ │ │ ├── AddNote.kt
│ │ │ │ │ │ └── GetNotes.kt
│ │ │ │ │ ├── repository
│ │ │ │ │ │ └── NoteRepository.kt
│ │ │ │ │ └── model
│ │ │ │ │ │ └── Note.kt
│ │ │ │ ├── presentation
│ │ │ │ │ ├── add_edit_note
│ │ │ │ │ │ ├── NoteTextFieldState.kt
│ │ │ │ │ │ ├── AddEditNoteEvent.kt
│ │ │ │ │ │ ├── components
│ │ │ │ │ │ │ ├── AppKeyboardFocusManager.kt
│ │ │ │ │ │ │ └── TransparentHintTextField.kt
│ │ │ │ │ │ ├── AddEditNoteViewModel.kt
│ │ │ │ │ │ └── AddEditNoteScreen.kt
│ │ │ │ │ ├── util
│ │ │ │ │ │ ├── Screen.kt
│ │ │ │ │ │ └── KeyBoardManager.kt
│ │ │ │ │ ├── notes
│ │ │ │ │ │ ├── NotesState.kt
│ │ │ │ │ │ ├── NotesEvent.kt
│ │ │ │ │ │ ├── components
│ │ │ │ │ │ │ ├── DefaultRadioButton.kt
│ │ │ │ │ │ │ ├── EmptyScreenText.kt
│ │ │ │ │ │ │ ├── OrderSection.kt
│ │ │ │ │ │ │ └── NoteItem.kt
│ │ │ │ │ │ ├── NotesViewModel.kt
│ │ │ │ │ │ └── NotesScreen.kt
│ │ │ │ │ └── MainActivity.kt
│ │ │ │ └── data
│ │ │ │ │ ├── data_source
│ │ │ │ │ ├── NoteDataBase.kt
│ │ │ │ │ └── NoteDao.kt
│ │ │ │ │ └── repository
│ │ │ │ │ └── NoteRepositoryImpl.kt
│ │ │ │ ├── core
│ │ │ │ └── util
│ │ │ │ │ └── TestTags.kt
│ │ │ │ ├── ui
│ │ │ │ └── theme
│ │ │ │ │ ├── Shape.kt
│ │ │ │ │ ├── Color.kt
│ │ │ │ │ ├── Type.kt
│ │ │ │ │ └── Theme.kt
│ │ │ │ └── di
│ │ │ │ └── AppModule.kt
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── akshayashokcode
│ │ │ └── notepad
│ │ │ ├── feature_note
│ │ │ ├── domain
│ │ │ │ └── use_case
│ │ │ │ │ ├── AddNoteTest.kt
│ │ │ │ │ └── GetNotesTest.kt
│ │ │ └── data
│ │ │ │ └── repository
│ │ │ │ └── FakeNoteRepository.kt
│ │ │ └── ExampleUnitTest.kt
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── akshayashokcode
│ │ └── notepad
│ │ ├── HiltTestRunner.kt
│ │ ├── ExampleInstrumentedTest.kt
│ │ ├── di
│ │ └── TestAppModule.kt
│ │ └── feature_note
│ │ └── presentation
│ │ ├── notes
│ │ └── NotesScreenTest.kt
│ │ └── NotesEndToEndTest.kt
├── proguard-rules.pro
└── build.gradle
├── screenshots
├── screenshot1.png
├── screenshot2.png
├── screenshot3.png
├── screenshot4.png
└── screenshot5.png
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── settings.gradle
├── gradle.properties
├── README.md
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | NotePad
3 |
--------------------------------------------------------------------------------
/screenshots/screenshot1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AkshayAshokCode/NotePad/HEAD/screenshots/screenshot1.png
--------------------------------------------------------------------------------
/screenshots/screenshot2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AkshayAshokCode/NotePad/HEAD/screenshots/screenshot2.png
--------------------------------------------------------------------------------
/screenshots/screenshot3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AkshayAshokCode/NotePad/HEAD/screenshots/screenshot3.png
--------------------------------------------------------------------------------
/screenshots/screenshot4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AkshayAshokCode/NotePad/HEAD/screenshots/screenshot4.png
--------------------------------------------------------------------------------
/screenshots/screenshot5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AkshayAshokCode/NotePad/HEAD/screenshots/screenshot5.png
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AkshayAshokCode/NotePad/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/ic_launcher-playstore.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AkshayAshokCode/NotePad/HEAD/app/src/main/ic_launcher-playstore.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/notepad_icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AkshayAshokCode/NotePad/HEAD/app/src/main/res/drawable/notepad_icon.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AkshayAshokCode/NotePad/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AkshayAshokCode/NotePad/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/font/varela_round_regular.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AkshayAshokCode/NotePad/HEAD/app/src/main/res/font/varela_round_regular.ttf
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AkshayAshokCode/NotePad/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AkshayAshokCode/NotePad/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AkshayAshokCode/NotePad/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AkshayAshokCode/NotePad/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/AkshayAshokCode/NotePad/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/AkshayAshokCode/NotePad/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/AkshayAshokCode/NotePad/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/AkshayAshokCode/NotePad/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AkshayAshokCode/NotePad/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AkshayAshokCode/NotePad/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AkshayAshokCode/NotePad/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AkshayAshokCode/NotePad/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AkshayAshokCode/NotePad/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png
--------------------------------------------------------------------------------
/app/src/main/res/values/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3DDC84
4 |
--------------------------------------------------------------------------------
/app/src/test/java/com/akshayashokcode/notepad/feature_note/domain/use_case/AddNoteTest.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.domain.use_case
2 |
3 | import org.junit.Assert.*
4 |
5 | class AddNoteTest
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/NoteApp.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad
2 |
3 | import android.app.Application
4 | import dagger.hilt.android.HiltAndroidApp
5 |
6 | @HiltAndroidApp
7 | class NoteApp:Application()
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 |
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/domain/util/OrderType.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.domain.util
2 |
3 | sealed class OrderType{
4 | data object Ascending:OrderType()
5 | data object Descending:OrderType()
6 | }
7 |
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/domain/use_case/NoteUseCases.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.domain.use_case
2 |
3 | data class NoteUseCases(
4 | val getNotes: GetNotes,
5 | val deleteNote: DeleteNote,
6 | val addNote: AddNote,
7 | val getNote:GetNote
8 | )
9 |
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/presentation/add_edit_note/NoteTextFieldState.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.presentation.add_edit_note
2 |
3 | data class NoteTextFieldState(
4 | val text:String="",
5 | val hint:String="",
6 | val isHintVisible:Boolean=true
7 | )
8 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/presentation/util/Screen.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.presentation.util
2 |
3 | sealed class Screen(val route:String){
4 | data object NotesScreen:Screen("notes_screen")
5 | data object AddEditNoteScreen:Screen("add_edit_note_screen")
6 | }
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/core/util/TestTags.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.core.util
2 |
3 | object TestTags {
4 | const val ORDER_SECTION = "ORDER_SECTION"
5 | const val TITLE_TEXT_FIELD = "TITLE_TEXT_FIELD"
6 | const val CONTENT_TEXT_FIELD = "CONTENT_TEXT_FIELD"
7 | const val NOTE_ITEM = "NOTE_ITEM"
8 | }
--------------------------------------------------------------------------------
/.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 | /.idea/inspectionProfiles/Project_Default.xml
11 | .DS_Store
12 | /build
13 | /captures
14 | .externalNativeBuild
15 | .cxx
16 | local.properties
17 |
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/ui/theme/Shape.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.ui.theme
2 |
3 | import androidx.compose.foundation.shape.RoundedCornerShape
4 | import androidx.compose.material3.Shapes
5 | import androidx.compose.ui.unit.dp
6 |
7 | val Shapes = Shapes(
8 | small = RoundedCornerShape(4.dp),
9 | medium = RoundedCornerShape(4.dp),
10 | large = RoundedCornerShape(0.dp)
11 | )
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #FFBB86FC
4 | #FF202020
5 | #FF202020
6 | #FF03DAC5
7 | #FF018786
8 | #FF000000
9 | #FFFFFFFF
10 |
--------------------------------------------------------------------------------
/app/src/test/java/com/akshayashokcode/notepad/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/domain/use_case/DeleteNote.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.domain.use_case
2 |
3 | import com.akshayashokcode.notepad.feature_note.domain.model.Note
4 | import com.akshayashokcode.notepad.feature_note.domain.repository.NoteRepository
5 |
6 | class DeleteNote(
7 | private val repository: NoteRepository
8 | ) {
9 | suspend operator fun invoke(note: Note){
10 | repository.deleteNote(note)
11 | }
12 | }
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | pluginManagement {
2 | repositories {
3 | gradlePluginPortal()
4 | google()
5 | mavenCentral()
6 | }
7 | }
8 |
9 | dependencyResolutionManagement {
10 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
11 | repositories {
12 | google()
13 | mavenCentral()
14 | // jcenter() // Warning: this repository is going to shut down soon
15 | }
16 | }
17 |
18 | rootProject.name = "NotePad"
19 | include ':app'
20 |
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/domain/use_case/GetNote.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.domain.use_case
2 |
3 | import com.akshayashokcode.notepad.feature_note.domain.model.Note
4 | import com.akshayashokcode.notepad.feature_note.domain.repository.NoteRepository
5 |
6 | class GetNote(
7 | private val repository: NoteRepository
8 | ) {
9 |
10 | suspend operator fun invoke(id:Int): Note?{
11 | return repository.getNoteById(id)
12 | }
13 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/domain/repository/NoteRepository.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.domain.repository
2 |
3 | import com.akshayashokcode.notepad.feature_note.domain.model.Note
4 | import kotlinx.coroutines.flow.Flow
5 |
6 | interface NoteRepository {
7 |
8 | fun getNotes(): Flow>
9 |
10 | suspend fun getNoteById(id: Int): Note?
11 |
12 | suspend fun insertNote(note: Note)
13 |
14 | suspend fun deleteNote(note: Note)
15 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/data/data_source/NoteDataBase.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.data.data_source
2 |
3 | import androidx.room.Database
4 | import androidx.room.RoomDatabase
5 | import com.akshayashokcode.notepad.feature_note.domain.model.Note
6 |
7 | @Database(entities = [Note::class], version = 1)
8 | abstract class NoteDataBase : RoomDatabase() {
9 |
10 | abstract val noteDao:NoteDao
11 | companion object{
12 | const val DATABASE_NAME="notes_db"
13 | }
14 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/presentation/notes/NotesState.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.presentation.notes
2 |
3 | import com.akshayashokcode.notepad.feature_note.domain.model.Note
4 | import com.akshayashokcode.notepad.feature_note.domain.util.NoteOrder
5 | import com.akshayashokcode.notepad.feature_note.domain.util.OrderType
6 |
7 | data class NotesState(
8 | val notes:List = emptyList(),
9 | val noteOrder: NoteOrder=NoteOrder.Date(OrderType.Descending),
10 | val isOrderSectionVisible:Boolean=false
11 | )
12 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/akshayashokcode/notepad/HiltTestRunner.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad
2 |
3 | import android.app.Application
4 | import android.content.Context
5 | import androidx.test.runner.AndroidJUnitRunner
6 | import dagger.hilt.android.testing.HiltTestApplication
7 |
8 | class HiltTestRunner : AndroidJUnitRunner() {
9 |
10 | override fun newApplication(
11 | cl: ClassLoader?,
12 | className: String?,
13 | context: Context?
14 | ): Application {
15 | return super.newApplication(cl, HiltTestApplication::class.java.name, context)
16 | }
17 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/presentation/notes/NotesEvent.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.presentation.notes
2 |
3 | import com.akshayashokcode.notepad.feature_note.domain.model.Note
4 | import com.akshayashokcode.notepad.feature_note.domain.util.NoteOrder
5 |
6 | sealed class NotesEvent {
7 | data class Order(val noteOrder: NoteOrder) : NotesEvent()
8 | data class DeleteNote(val note: Note) : NotesEvent()
9 | data object RestoreNote : NotesEvent()
10 | data object ToggleOrderSection : NotesEvent()
11 | data object CloseOrderSection : NotesEvent()
12 | }
13 |
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/domain/util/NoteOrder.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.domain.util
2 |
3 | sealed class NoteOrder(val orderType: OrderType){
4 | class Title(orderType: OrderType):NoteOrder(orderType)
5 | class Date(orderType: OrderType):NoteOrder(orderType)
6 | class Color(orderType: OrderType):NoteOrder(orderType)
7 |
8 | fun copy(orderType: OrderType):NoteOrder{
9 | return when(this){
10 | is Title->Title(orderType)
11 | is Date->Date(orderType)
12 | is Color->Color(orderType)
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/data/data_source/NoteDao.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.data.data_source
2 |
3 | import androidx.room.*
4 | import com.akshayashokcode.notepad.feature_note.domain.model.Note
5 | import kotlinx.coroutines.flow.Flow
6 |
7 | @Dao
8 | interface NoteDao {
9 |
10 | @Query("SELECT * FROM note")
11 | fun getNotes(): Flow>
12 |
13 | @Query("SELECT * FROM note WHERE id= :id")
14 | suspend fun getNoteById(id: Int): Note?
15 |
16 | @Insert(onConflict = OnConflictStrategy.REPLACE)
17 | suspend fun insertNote(note: Note)
18 |
19 | @Delete
20 | suspend fun deleteNote(note: Note)
21 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/presentation/add_edit_note/AddEditNoteEvent.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.presentation.add_edit_note
2 |
3 | import androidx.compose.ui.focus.FocusState
4 |
5 | sealed class AddEditNoteEvent{
6 | data class EnteredTitle(val value:String):AddEditNoteEvent()
7 | data class ChangeTitleFocus(val focusState: FocusState):AddEditNoteEvent()
8 | data class EnteredContent(val value:String):AddEditNoteEvent()
9 | data class ChangeContentFocus(val focusState: FocusState):AddEditNoteEvent()
10 | data class ChangeColor(val color:Int):AddEditNoteEvent()
11 | data object SaveNote:AddEditNoteEvent()
12 | }
13 |
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/ui/theme/Color.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.ui.theme
2 |
3 | import androidx.compose.ui.graphics.Color
4 |
5 | val DarkGray = Color(0xFF202020)
6 | val LightGray = Color(0xFF444040)
7 | val LightBlue = Color(0xFFD7E8DE)
8 |
9 | val RedOrange = Color(0xffffab91)
10 | val RedPink = Color(0xfff48fb1)
11 | val BabyBlue = Color(0xff81deea)
12 | val Violet = Color(0xffcf94da)
13 | val LightGreen = Color(0xffe7ed9b)
14 |
15 | val LightYellow = Color(0xFFFFE082)
16 | val LightOrange = Color(0xFFFFCC80)
17 | val MintGreen = Color(0xFFC8E6C9)
18 | val LightPurple = Color(0xFFD1C4E9)
19 | val Lavender = Color(0xFFB39DDB)
20 | val LightPink = Color(0xFFFFCDD2)
21 | val PaleGreen = Color(0xFFDCEDC8)
22 | val Cream = Color(0xFFFFF9C4)
23 |
24 |
25 |
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/domain/use_case/AddNote.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.domain.use_case
2 |
3 | import com.akshayashokcode.notepad.feature_note.domain.model.Note
4 | import com.akshayashokcode.notepad.feature_note.domain.repository.NoteRepository
5 | import kotlin.jvm.Throws
6 |
7 | class AddNote(
8 | private val repository:NoteRepository
9 | ) {
10 |
11 | @Throws(Note.InvalidNoteException::class)
12 | suspend operator fun invoke(note: Note){
13 | if(note.title.isBlank()){
14 | throw Note.InvalidNoteException("The title of the note can't be empty.")
15 | }
16 | // Content can be empty - user might want title-only notes or quick saves
17 | repository.insertNote(note)
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/akshayashokcode/notepad/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad
2 |
3 | import androidx.test.platform.app.InstrumentationRegistry
4 | import androidx.test.ext.junit.runners.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22 | assertEquals("com.akshayashokcode.notepad", appContext.packageName)
23 | }
24 | }
--------------------------------------------------------------------------------
/app/src/main/res/values-night/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
--------------------------------------------------------------------------------
/app/src/test/java/com/akshayashokcode/notepad/feature_note/data/repository/FakeNoteRepository.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.data.repository
2 |
3 | import com.akshayashokcode.notepad.feature_note.domain.model.Note
4 | import com.akshayashokcode.notepad.feature_note.domain.repository.NoteRepository
5 | import kotlinx.coroutines.flow.Flow
6 | import kotlinx.coroutines.flow.flow
7 |
8 | class FakeNoteRepository : NoteRepository {
9 |
10 | private val notes = mutableListOf()
11 |
12 | override fun getNotes(): Flow> {
13 | return flow { emit(notes) }
14 | }
15 |
16 | override suspend fun getNoteById(id: Int): Note? {
17 | return notes.find { it.id == id }
18 | }
19 |
20 | override suspend fun insertNote(note: Note) {
21 | notes.add(note)
22 | }
23 |
24 | override suspend fun deleteNote(note: Note) {
25 | notes.remove(note)
26 | }
27 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/data/repository/NoteRepositoryImpl.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.data.repository
2 |
3 | import com.akshayashokcode.notepad.feature_note.data.data_source.NoteDao
4 | import com.akshayashokcode.notepad.feature_note.domain.model.Note
5 | import com.akshayashokcode.notepad.feature_note.domain.repository.NoteRepository
6 | import kotlinx.coroutines.flow.Flow
7 |
8 | class NoteRepositoryImpl(
9 | private val dao:NoteDao
10 | ): NoteRepository {
11 | override fun getNotes(): Flow> {
12 | return dao.getNotes()
13 | }
14 |
15 | override suspend fun getNoteById(id: Int): Note? {
16 | return dao.getNoteById(id)
17 | }
18 |
19 | override suspend fun insertNote(note: Note) {
20 | return dao.insertNote(note)
21 | }
22 |
23 | override suspend fun deleteNote(note: Note) {
24 | return dao.deleteNote(note)
25 | }
26 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/presentation/add_edit_note/components/AppKeyboardFocusManager.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.presentation.add_edit_note.components
2 |
3 | import androidx.compose.runtime.Composable
4 | import androidx.compose.runtime.DisposableEffect
5 | import androidx.compose.ui.platform.LocalContext
6 | import androidx.compose.ui.platform.LocalFocusManager
7 | import com.akshayashokcode.notepad.feature_note.presentation.util.KeyBoardManager
8 |
9 | @Composable
10 | fun AppKeyboardFocusManager() {
11 | val context = LocalContext.current
12 | val focusManager = LocalFocusManager.current
13 | DisposableEffect(key1 = context) {
14 | val keyboardManager = KeyBoardManager(context)
15 | keyboardManager.attachKeyboardDismissListener {
16 | focusManager.clearFocus()
17 | }
18 | onDispose {
19 | keyboardManager.release()
20 | }
21 | }
22 |
23 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
12 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/app/src/main/res/values/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
17 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/ui/theme/Type.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.ui.theme
2 |
3 | import androidx.compose.material3.Typography
4 | import androidx.compose.ui.text.TextStyle
5 | import androidx.compose.ui.text.font.Font
6 | import androidx.compose.ui.text.font.FontFamily
7 | import androidx.compose.ui.text.font.FontWeight
8 | import androidx.compose.ui.unit.sp
9 | import com.akshayashokcode.notepad.R
10 |
11 | // Set of Material typography styles to start with
12 | val Typography = Typography(
13 | bodyLarge = TextStyle(
14 | fontFamily = FontFamily.Default,
15 | fontWeight = FontWeight.Normal,
16 | fontSize = 16.sp
17 | )
18 |
19 | /* Other default text styles to override
20 | labelLarge = TextStyle(
21 | fontFamily = FontFamily.Default,
22 | fontWeight = FontWeight.W500,
23 | fontSize = 14.sp
24 | ),
25 | bodySmall = TextStyle(
26 | fontFamily = FontFamily.Default,
27 | fontWeight = FontWeight.Normal,
28 | fontSize = 12.sp
29 | )
30 | */
31 | )
32 | val customTypography = Typography(
33 | bodyLarge = TextStyle(
34 | fontFamily = FontFamily(Font(R.font.varela_round_regular)),
35 | fontWeight = FontWeight.Normal,
36 | fontSize = 16.sp
37 | )
38 | )
39 |
--------------------------------------------------------------------------------
/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 -Dfile.encoding=UTF-8
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
22 | android.nonTransitiveRClass=false
23 | android.nonFinalResIds=false
24 | android.enableR8.fullMode=false
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/akshayashokcode/notepad/di/TestAppModule.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.di
2 |
3 | import android.app.Application
4 | import androidx.room.Room
5 | import com.akshayashokcode.notepad.feature_note.data.data_source.NoteDataBase
6 | import com.akshayashokcode.notepad.feature_note.data.repository.NoteRepositoryImpl
7 | import com.akshayashokcode.notepad.feature_note.domain.repository.NoteRepository
8 | import com.akshayashokcode.notepad.feature_note.domain.use_case.*
9 | import dagger.Module
10 | import dagger.Provides
11 | import dagger.hilt.InstallIn
12 | import dagger.hilt.components.SingletonComponent
13 | import javax.inject.Singleton
14 |
15 | @Module
16 | @InstallIn(SingletonComponent::class)
17 | object TestAppModule {
18 |
19 | @Provides
20 | @Singleton
21 | fun provideNoteDatabase(app:Application):NoteDataBase{
22 | return Room.inMemoryDatabaseBuilder(
23 | app,
24 | NoteDataBase::class.java
25 | ).build()
26 | }
27 |
28 | @Provides
29 | @Singleton
30 | fun provideNoteRepository(db:NoteDataBase):NoteRepository{
31 | return NoteRepositoryImpl(db.noteDao)
32 | }
33 |
34 | @Provides
35 | @Singleton
36 | fun provideNoteUseCases(repository: NoteRepository):NoteUseCases{
37 | return NoteUseCases(
38 | getNotes = GetNotes(repository),
39 | deleteNote = DeleteNote(repository),
40 | addNote = AddNote(repository),
41 | getNote = GetNote(repository)
42 | )
43 | }
44 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # NotePad
2 | A simple notepad Allows the users to create & edit notes with different color themes to categorize them.
3 |
4 | Users can sort notes by title, date, or color in both ascending & descending order.
5 |
6 | Built with Kotlin using Room DB, Coroutines, Dagger Hilt, LiveData, Canvas, and following Model-View-ViewModel (MVVM) with clean architecture & it’s designed using jetpack compose.
7 |
8 |
9 | ## Features :
10 | - **Create Notes**
11 | - **Delete Notes**
12 | - **Select Note Colours to group notes**
13 | - **Re-edit Notes**
14 | - **Sort Notes by title, date or color**
15 |
16 |
17 | ## ScreenShots :
18 |
19 |
20 |
21 |
22 |
23 |
24 | *You can Install the latest NotePad app from here 👉* [
](https://play.google.com/store/apps/details?id=com.akshayashokcode.notepad)
25 |
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/di/AppModule.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.di
2 |
3 | import android.app.Application
4 | import androidx.room.Room
5 | import com.akshayashokcode.notepad.feature_note.data.data_source.NoteDataBase
6 | import com.akshayashokcode.notepad.feature_note.data.repository.NoteRepositoryImpl
7 | import com.akshayashokcode.notepad.feature_note.domain.repository.NoteRepository
8 | import com.akshayashokcode.notepad.feature_note.domain.use_case.*
9 | import dagger.Module
10 | import dagger.Provides
11 | import dagger.hilt.InstallIn
12 | import dagger.hilt.components.SingletonComponent
13 | import javax.inject.Singleton
14 |
15 | @Module
16 | @InstallIn(SingletonComponent::class)
17 | object AppModule {
18 |
19 | @Provides
20 | @Singleton
21 | fun provideNoteDatabase(app:Application):NoteDataBase{
22 | return Room.databaseBuilder(
23 | app,
24 | NoteDataBase::class.java,
25 | NoteDataBase.DATABASE_NAME
26 | ).build()
27 | }
28 |
29 | @Provides
30 | @Singleton
31 | fun provideNoteRepository(db:NoteDataBase):NoteRepository{
32 | return NoteRepositoryImpl(db.noteDao)
33 | }
34 |
35 | @Provides
36 | @Singleton
37 | fun provideNoteUseCases(repository: NoteRepository):NoteUseCases{
38 | return NoteUseCases(
39 | getNotes = GetNotes(repository),
40 | deleteNote = DeleteNote(repository),
41 | addNote = AddNote(repository),
42 | getNote = GetNote(repository)
43 | )
44 | }
45 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/ui/theme/Theme.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.ui.theme
2 |
3 | import androidx.compose.foundation.isSystemInDarkTheme
4 | import androidx.compose.material3.MaterialTheme
5 | import androidx.compose.material3.darkColorScheme
6 | import androidx.compose.material3.lightColorScheme
7 | import androidx.compose.runtime.Composable
8 | import androidx.compose.ui.graphics.Color
9 | import com.google.accompanist.systemuicontroller.rememberSystemUiController
10 |
11 | private val DarkColorScheme = darkColorScheme(
12 | primary = Color.White,
13 | background = DarkGray,
14 | onBackground = Color.White,
15 | surface = LightBlue,
16 | onSurface = DarkGray
17 | )
18 |
19 | private val LightColorScheme = lightColorScheme(
20 | primary = LightGray,
21 | background = LightBlue,
22 | onBackground = Color.Black,
23 | surface = Color.White,
24 | onSurface = Color.Black
25 | )
26 |
27 | @Composable
28 | fun NotePadTheme(darkTheme: Boolean = isSystemInDarkTheme(), content: @Composable() () -> Unit) {
29 | val colorScheme = if (darkTheme) {
30 | rememberSystemUiController().setSystemBarsColor(
31 | color = Color.Transparent
32 | )
33 | DarkColorScheme
34 | } else {
35 | rememberSystemUiController().setSystemBarsColor(
36 | color = Color.White
37 | )
38 | LightColorScheme
39 | }
40 |
41 |
42 | MaterialTheme(
43 | colorScheme = colorScheme,
44 | typography = Typography,
45 | shapes = Shapes,
46 | content = content
47 | )
48 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/domain/model/Note.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.domain.model
2 |
3 | import androidx.room.Entity
4 | import androidx.room.PrimaryKey
5 | import com.akshayashokcode.notepad.ui.theme.BabyBlue
6 | import com.akshayashokcode.notepad.ui.theme.Cream
7 | import com.akshayashokcode.notepad.ui.theme.Lavender
8 | import com.akshayashokcode.notepad.ui.theme.LightGreen
9 | import com.akshayashokcode.notepad.ui.theme.LightOrange
10 | import com.akshayashokcode.notepad.ui.theme.LightPink
11 | import com.akshayashokcode.notepad.ui.theme.LightPurple
12 | import com.akshayashokcode.notepad.ui.theme.LightYellow
13 | import com.akshayashokcode.notepad.ui.theme.MintGreen
14 | import com.akshayashokcode.notepad.ui.theme.PaleGreen
15 | import com.akshayashokcode.notepad.ui.theme.RedOrange
16 | import com.akshayashokcode.notepad.ui.theme.RedPink
17 | import com.akshayashokcode.notepad.ui.theme.Violet
18 |
19 | @Entity
20 | data class Note(
21 | val title: String,
22 | val content: String,
23 | val timeStamp: Long,
24 | val color: Int,
25 | @PrimaryKey val id: Int? = null
26 | ) {
27 | companion object {
28 | val noteColors = listOf(
29 | RedOrange,
30 | LightGreen,
31 | Violet,
32 | BabyBlue,
33 | RedPink,
34 | LightYellow,
35 | LightOrange,
36 | MintGreen,
37 | LightPurple,
38 | Lavender,
39 | LightPink,
40 | PaleGreen,
41 | Cream
42 | )
43 | }
44 |
45 | class InvalidNoteException(message: String) : Exception(message)
46 | }
47 |
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/domain/use_case/GetNotes.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.domain.use_case
2 |
3 | import com.akshayashokcode.notepad.feature_note.domain.model.Note
4 | import com.akshayashokcode.notepad.feature_note.domain.repository.NoteRepository
5 | import com.akshayashokcode.notepad.feature_note.domain.util.NoteOrder
6 | import com.akshayashokcode.notepad.feature_note.domain.util.OrderType
7 | import kotlinx.coroutines.flow.Flow
8 | import kotlinx.coroutines.flow.map
9 |
10 | class GetNotes(
11 | private val repository: NoteRepository
12 | ) {
13 |
14 | operator fun invoke(
15 | noteOrder: NoteOrder=NoteOrder.Date(OrderType.Descending)
16 | ):Flow>{
17 | return repository.getNotes().map { notes->
18 | when(noteOrder.orderType){
19 | is OrderType.Ascending ->{
20 | when(noteOrder){
21 | is NoteOrder.Title-> notes.sortedBy { it.title.lowercase() }
22 | is NoteOrder.Date-> notes.sortedBy { it.timeStamp }
23 | is NoteOrder.Color-> notes.sortedBy { it.color }
24 | }
25 | }
26 | is OrderType.Descending->{
27 | when(noteOrder){
28 | is NoteOrder.Title-> notes.sortedByDescending { it.title.lowercase() }
29 | is NoteOrder.Date-> notes.sortedByDescending { it.timeStamp }
30 | is NoteOrder.Color-> notes.sortedByDescending{ it.color }
31 | }
32 | }
33 | }
34 | }
35 | }
36 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/presentation/notes/components/DefaultRadioButton.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.presentation.notes.components
2 |
3 | import androidx.compose.foundation.layout.Row
4 | import androidx.compose.foundation.layout.Spacer
5 | import androidx.compose.foundation.layout.width
6 | import androidx.compose.material3.MaterialTheme
7 | import androidx.compose.material3.RadioButton
8 | import androidx.compose.material3.RadioButtonDefaults
9 | import androidx.compose.material3.Text
10 | import androidx.compose.runtime.Composable
11 | import androidx.compose.ui.Alignment
12 | import androidx.compose.ui.Modifier
13 | import androidx.compose.ui.semantics.contentDescription
14 | import androidx.compose.ui.semantics.semantics
15 | import androidx.compose.ui.unit.dp
16 |
17 | @Composable
18 | fun DefaultRadioButton(
19 | text: String,
20 | selected: Boolean,
21 | onSelect: () -> Unit,
22 | modifier: Modifier = Modifier
23 | ) {
24 | Row(
25 | modifier = modifier,
26 | verticalAlignment = Alignment.CenterVertically
27 | ) {
28 | RadioButton(
29 | selected = selected,
30 | onClick = onSelect,
31 | colors = RadioButtonDefaults.colors(
32 | selectedColor = MaterialTheme.colorScheme.primary,
33 | unselectedColor = MaterialTheme.colorScheme.onBackground
34 | ),
35 | modifier = Modifier.semantics {
36 | contentDescription = text
37 | }
38 | )
39 | Spacer(modifier = Modifier.width(8.dp))
40 | Text(text = text, style = MaterialTheme.typography.bodyLarge)
41 | }
42 | }
--------------------------------------------------------------------------------
/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/akshayashokcode/notepad/feature_note/presentation/util/KeyBoardManager.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.presentation.util
2 |
3 | import android.app.Activity
4 | import android.content.Context
5 | import android.graphics.Rect
6 | import android.view.View
7 | import android.view.ViewTreeObserver
8 |
9 | class KeyBoardManager(context: Context) {
10 |
11 | private val activity = context as Activity
12 | private var keyboardDismissListener: KeyboardDismissListener? = null
13 |
14 | private abstract class KeyboardDismissListener(
15 | private val rootView: View,
16 | private val onKeyboardDismiss: () -> Unit
17 | ) : ViewTreeObserver.OnGlobalLayoutListener {
18 | private var isKeyboardClosed: Boolean = false
19 | override fun onGlobalLayout() {
20 | val r = Rect()
21 | rootView.getWindowVisibleDisplayFrame(r)
22 | val screenHeight = rootView.rootView.height
23 | val keypadHeight = screenHeight - r.bottom
24 | if (keypadHeight > screenHeight * 0.15) {
25 | // 0.15 ratio is right enough to determine keypad height.
26 | isKeyboardClosed = false
27 | } else if (!isKeyboardClosed) {
28 | isKeyboardClosed = true
29 | onKeyboardDismiss.invoke()
30 | }
31 | }
32 | }
33 | fun attachKeyboardDismissListener(onKeyboardDismiss: () -> Unit) {
34 | val rootView = activity.findViewById(android.R.id.content)
35 | keyboardDismissListener = object : KeyboardDismissListener(rootView, onKeyboardDismiss) {}
36 | keyboardDismissListener?.let {
37 | rootView.viewTreeObserver.addOnGlobalLayoutListener(it)
38 | }
39 | }
40 |
41 | fun release() {
42 | val rootView = activity.findViewById(android.R.id.content)
43 | keyboardDismissListener?.let {
44 | rootView.viewTreeObserver.removeOnGlobalLayoutListener(it)
45 | }
46 | keyboardDismissListener = null
47 | }
48 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/presentation/notes/components/EmptyScreenText.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.presentation.notes.components
2 |
3 | import androidx.compose.foundation.layout.Arrangement
4 | import androidx.compose.foundation.layout.Column
5 | import androidx.compose.foundation.layout.Spacer
6 | import androidx.compose.foundation.layout.fillMaxSize
7 | import androidx.compose.foundation.layout.width
8 | import androidx.compose.material3.MaterialTheme
9 | import androidx.compose.material3.Text
10 | import androidx.compose.runtime.Composable
11 | import androidx.compose.ui.Alignment
12 | import androidx.compose.ui.Modifier
13 | import androidx.compose.ui.graphics.Color
14 | import androidx.compose.ui.text.TextStyle
15 | import androidx.compose.ui.text.font.FontFamily
16 | import androidx.compose.ui.unit.dp
17 |
18 | @Composable
19 | fun EmptyScreenText(
20 | modifier: Modifier = Modifier
21 | ) {
22 | Column(
23 | modifier = modifier.fillMaxSize(),
24 | horizontalAlignment = Alignment.CenterHorizontally,
25 | verticalArrangement = Arrangement.Center
26 | )
27 | {
28 | Text(
29 | text = "No notes",
30 | style = TextStyle(
31 | color = Color.Gray,
32 | fontStyle = MaterialTheme.typography.headlineSmall.fontStyle,
33 | fontSize = MaterialTheme.typography.headlineSmall.fontSize,
34 | fontWeight = MaterialTheme.typography.headlineSmall.fontWeight,
35 | fontFamily = FontFamily.SansSerif
36 | )
37 | )
38 | Spacer(modifier = Modifier.width(8.dp))
39 | Text(
40 | text = "Tap the Add button to create a note",
41 | style = TextStyle(
42 | color = Color.Gray,
43 | fontStyle = MaterialTheme.typography.bodyMedium.fontStyle,
44 | fontSize = MaterialTheme.typography.bodyMedium.fontSize,
45 | fontWeight = MaterialTheme.typography.bodyMedium.fontWeight,
46 | fontFamily = FontFamily.Default
47 | )
48 | )
49 | }
50 |
51 |
52 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/presentation/notes/components/OrderSection.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.presentation.notes.components
2 |
3 | import androidx.compose.foundation.layout.*
4 | import androidx.compose.material3.Text
5 | import androidx.compose.runtime.Composable
6 | import androidx.compose.ui.Alignment
7 | import androidx.compose.ui.Modifier
8 | import androidx.compose.ui.unit.dp
9 | import androidx.compose.ui.unit.sp
10 | import com.akshayashokcode.notepad.feature_note.domain.util.NoteOrder
11 | import com.akshayashokcode.notepad.feature_note.domain.util.OrderType
12 |
13 |
14 | @Composable
15 | fun OrderSection(
16 | modifier: Modifier = Modifier,
17 | noteOrder: NoteOrder = NoteOrder.Date(OrderType.Descending),
18 | onOrderChange: (NoteOrder) -> Unit
19 | ) {
20 | Column(
21 | modifier = modifier
22 | ) {
23 | Row(
24 | modifier = Modifier.fillMaxWidth(),
25 | verticalAlignment = Alignment.CenterVertically
26 | ) {
27 | Text(text = "Sort by:", fontSize = 16.sp)
28 | Spacer(modifier = Modifier.width(8.dp))
29 | DefaultRadioButton(
30 | text = "Title",
31 | selected = noteOrder is NoteOrder.Title,
32 | onSelect = { onOrderChange(NoteOrder.Title(noteOrder.orderType)) }
33 | )
34 | Spacer(modifier = Modifier.width(8.dp))
35 | DefaultRadioButton(
36 | text = "Date",
37 | selected = noteOrder is NoteOrder.Date,
38 | onSelect = { onOrderChange(NoteOrder.Date(noteOrder.orderType)) }
39 | )
40 | Spacer(modifier = Modifier.width(8.dp))
41 | DefaultRadioButton(
42 | text = "Color",
43 | selected = noteOrder is NoteOrder.Color,
44 | onSelect = { onOrderChange(NoteOrder.Color(noteOrder.orderType)) }
45 | )
46 |
47 | }
48 | Spacer(modifier = Modifier.height(16.dp))
49 | Row(
50 | modifier = Modifier.fillMaxWidth()
51 | ) {
52 | DefaultRadioButton(
53 | text = "Ascending↓",
54 | selected = noteOrder.orderType is OrderType.Ascending,
55 | onSelect = { onOrderChange(noteOrder.copy(OrderType.Ascending)) }
56 | )
57 | Spacer(modifier = Modifier.width(8.dp))
58 | DefaultRadioButton(
59 | text = "Descending↑",
60 | selected = noteOrder.orderType is OrderType.Descending,
61 | onSelect = { onOrderChange(noteOrder.copy(OrderType.Descending)) }
62 | )
63 |
64 | }
65 | }
66 |
67 | }
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/akshayashokcode/notepad/feature_note/presentation/notes/NotesScreenTest.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.presentation.notes
2 |
3 | import androidx.compose.animation.ExperimentalAnimationApi
4 | import androidx.compose.ui.test.*
5 | import androidx.compose.ui.test.junit4.createAndroidComposeRule
6 | import androidx.navigation.compose.NavHost
7 | import androidx.navigation.compose.composable
8 | import androidx.navigation.compose.rememberNavController
9 | import com.akshayashokcode.notepad.core.util.TestTags
10 | import com.akshayashokcode.notepad.di.AppModule
11 | import com.akshayashokcode.notepad.feature_note.presentation.MainActivity
12 | import com.akshayashokcode.notepad.feature_note.presentation.util.Screen
13 | import com.akshayashokcode.notepad.ui.theme.NotePadTheme
14 | import dagger.hilt.android.testing.HiltAndroidRule
15 | import dagger.hilt.android.testing.HiltAndroidTest
16 | import dagger.hilt.android.testing.UninstallModules
17 | import org.junit.Before
18 | import org.junit.Rule
19 | import org.junit.Test
20 |
21 | @HiltAndroidTest
22 | @UninstallModules(AppModule::class)
23 | class NotesScreenTest {
24 |
25 | @get:Rule(order = 0)
26 | val hiltRule = HiltAndroidRule(this)
27 |
28 | @get:Rule(order = 1)
29 | val composeRule = createAndroidComposeRule()
30 |
31 | @ExperimentalAnimationApi
32 | @Before
33 | fun setUp() {
34 | hiltRule.inject()
35 | composeRule.setContent {
36 | val navController = rememberNavController()
37 | NotePadTheme {
38 | NavHost(
39 | navController = navController,
40 | startDestination = Screen.NotesScreen.route
41 | ) {
42 | composable(route = Screen.NotesScreen.route) {
43 | NotesScreen(navController = navController)
44 | }
45 | }
46 | }
47 | }
48 | }
49 |
50 | @Test
51 | fun clickToggleOrderSection_isVisible() {
52 | for(i in 1..3) {
53 | // Click on FAB to get to add note screen
54 | composeRule.onNodeWithContentDescription("Add").performClick()
55 |
56 | // Enter texts in title and content text fields
57 | composeRule
58 | .onNodeWithTag(TestTags.TITLE_TEXT_FIELD)
59 | .performTextInput(i.toString())
60 | composeRule
61 | .onNodeWithTag(TestTags.CONTENT_TEXT_FIELD)
62 | .performTextInput(i.toString())
63 | // Save the new
64 | composeRule.onNodeWithContentDescription("Save").performClick()
65 | }
66 | composeRule.onNodeWithTag(TestTags.ORDER_SECTION).assertDoesNotExist()
67 | composeRule.onNodeWithContentDescription("Sort").performClick()
68 | composeRule.onNodeWithTag(TestTags.ORDER_SECTION).assertIsDisplayed()
69 | }
70 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/presentation/add_edit_note/components/TransparentHintTextField.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.presentation.add_edit_note.components
2 |
3 | import androidx.compose.foundation.layout.Box
4 | import androidx.compose.foundation.layout.fillMaxSize
5 | import androidx.compose.foundation.layout.fillMaxWidth
6 | import androidx.compose.foundation.text.BasicTextField
7 | import androidx.compose.foundation.text.KeyboardOptions
8 | import androidx.compose.material3.Text
9 | import androidx.compose.runtime.Composable
10 | import androidx.compose.ui.Modifier
11 | import androidx.compose.ui.focus.FocusState
12 | import androidx.compose.ui.focus.onFocusChanged
13 | import androidx.compose.ui.graphics.Color
14 | import androidx.compose.ui.platform.testTag
15 | import androidx.compose.ui.text.TextStyle
16 | import androidx.compose.ui.text.input.KeyboardCapitalization
17 |
18 |
19 | @Composable
20 | fun TransparentHintTextFiled(
21 | text: String,
22 | hint: String,
23 | modifier: Modifier = Modifier,
24 | isHintVisible: Boolean = true,
25 | onValueChange: (String) -> Unit,
26 | textStyle: TextStyle = TextStyle(),
27 | singleLine: Boolean = false,
28 | testTag: String = "",
29 | onFocusChange: (FocusState) -> Unit
30 | ) {
31 | Box(modifier = modifier) {
32 | BasicTextField(
33 | value = text,
34 | onValueChange = onValueChange,
35 | singleLine = singleLine,
36 | textStyle = textStyle,
37 | keyboardOptions = KeyboardOptions.Default.copy(capitalization = KeyboardCapitalization.None),
38 | modifier = Modifier
39 | .testTag(testTag)
40 | .fillMaxWidth()
41 | .onFocusChanged { onFocusChange(it) }
42 | )
43 | if (isHintVisible) {
44 | Text(text = hint, style = textStyle, color = Color.DarkGray)
45 | }
46 | }
47 | }
48 |
49 | @Composable
50 | fun TransparentHintTextFiledContent(
51 | text: String,
52 | hint: String,
53 | modifier: Modifier = Modifier,
54 | isHintVisible: Boolean = true,
55 | onValueChange: (String) -> Unit,
56 | textStyle: TextStyle = TextStyle(),
57 | singleLine: Boolean = false,
58 | testTag: String = "",
59 | onFocusChange: (FocusState) -> Unit
60 | ) {
61 | Box(modifier = modifier) {
62 | BasicTextField(
63 | value = text,
64 | onValueChange = onValueChange,
65 | singleLine = singleLine,
66 | textStyle = textStyle,
67 | keyboardOptions = KeyboardOptions.Default.copy(capitalization = KeyboardCapitalization.None),
68 | modifier = Modifier
69 | .testTag(testTag)
70 | .fillMaxSize()
71 | .onFocusChanged { onFocusChange(it) }
72 | )
73 | if (isHintVisible) {
74 | Text(text = hint, style = textStyle, color = Color.DarkGray)
75 | }
76 | }
77 | }
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%" == "" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%" == "" set DIRNAME=.
29 | set APP_BASE_NAME=%~n0
30 | set APP_HOME=%DIRNAME%
31 |
32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34 |
35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
37 |
38 | @rem Find java.exe
39 | if defined JAVA_HOME goto findJavaFromJavaHome
40 |
41 | set JAVA_EXE=java.exe
42 | %JAVA_EXE% -version >NUL 2>&1
43 | if "%ERRORLEVEL%" == "0" goto execute
44 |
45 | echo.
46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47 | echo.
48 | echo Please set the JAVA_HOME variable in your environment to match the
49 | echo location of your Java installation.
50 |
51 | goto fail
52 |
53 | :findJavaFromJavaHome
54 | set JAVA_HOME=%JAVA_HOME:"=%
55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
56 |
57 | if exist "%JAVA_EXE%" goto execute
58 |
59 | echo.
60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61 | echo.
62 | echo Please set the JAVA_HOME variable in your environment to match the
63 | echo location of your Java installation.
64 |
65 | goto fail
66 |
67 | :execute
68 | @rem Setup the command line
69 |
70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
71 |
72 |
73 | @rem Execute Gradle
74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
75 |
76 | :end
77 | @rem End local scope for the variables with windows NT shell
78 | if "%ERRORLEVEL%"=="0" goto mainEnd
79 |
80 | :fail
81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
82 | rem the _cmd.exe /c_ return code!
83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84 | exit /b 1
85 |
86 | :mainEnd
87 | if "%OS%"=="Windows_NT" endlocal
88 |
89 | :omega
90 |
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/presentation/notes/NotesViewModel.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.presentation.notes
2 |
3 | import androidx.compose.runtime.State
4 | import androidx.compose.runtime.mutableStateOf
5 | import androidx.lifecycle.ViewModel
6 | import androidx.lifecycle.viewModelScope
7 | import com.akshayashokcode.notepad.feature_note.domain.model.Note
8 | import com.akshayashokcode.notepad.feature_note.domain.use_case.NoteUseCases
9 | import com.akshayashokcode.notepad.feature_note.domain.util.NoteOrder
10 | import com.akshayashokcode.notepad.feature_note.domain.util.OrderType
11 | import dagger.hilt.android.lifecycle.HiltViewModel
12 | import kotlinx.coroutines.Job
13 | import kotlinx.coroutines.flow.launchIn
14 | import kotlinx.coroutines.flow.onEach
15 | import kotlinx.coroutines.launch
16 | import javax.inject.Inject
17 |
18 | @HiltViewModel
19 | class NotesViewModel @Inject constructor(
20 | private val noteUseCases: NoteUseCases
21 | ) : ViewModel() {
22 |
23 | private val _state = mutableStateOf(NotesState())
24 | val state: State = _state
25 |
26 | private var recentlyDeletedNote: Note? = null
27 |
28 | private var getNotesJob: Job? = null
29 |
30 | init {
31 | getNotes(NoteOrder.Date(OrderType.Descending))
32 |
33 | }
34 |
35 | fun onEvent(event: NotesEvent) {
36 | when (event) {
37 | is NotesEvent.Order -> {
38 | if (state.value.noteOrder::class == event.noteOrder::class &&
39 | state.value.noteOrder.orderType == event.noteOrder.orderType
40 | ) {
41 | return
42 | }
43 | getNotes(event.noteOrder)
44 | }
45 |
46 | is NotesEvent.DeleteNote -> {
47 | viewModelScope.launch {
48 | noteUseCases.deleteNote(event.note)
49 | recentlyDeletedNote = event.note
50 | }
51 | // close order section when clicking FAB
52 | if (state.value.isOrderSectionVisible) {
53 | _state.value = state.value.copy(isOrderSectionVisible = false)
54 | }
55 | }
56 |
57 | is NotesEvent.RestoreNote -> {
58 | viewModelScope.launch {
59 | noteUseCases.addNote(recentlyDeletedNote ?: return@launch)
60 | recentlyDeletedNote = null
61 | }
62 | }
63 |
64 | is NotesEvent.ToggleOrderSection -> {
65 | _state.value = state.value.copy(
66 | isOrderSectionVisible = !state.value.isOrderSectionVisible
67 | )
68 | }
69 |
70 | is NotesEvent.CloseOrderSection -> {
71 | _state.value = state.value.copy(isOrderSectionVisible = false)
72 | }
73 | }
74 | }
75 |
76 | private fun getNotes(noteOrder: NoteOrder) {
77 | getNotesJob?.cancel()
78 | getNotesJob = noteUseCases.getNotes(noteOrder)
79 | .onEach { notes ->
80 | _state.value = state.value.copy(
81 | notes = notes,
82 | noteOrder = noteOrder
83 | )
84 | }
85 | .launchIn(viewModelScope)
86 |
87 | }
88 | }
--------------------------------------------------------------------------------
/app/src/test/java/com/akshayashokcode/notepad/feature_note/domain/use_case/GetNotesTest.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.domain.use_case
2 |
3 | import com.akshayashokcode.notepad.feature_note.data.repository.FakeNoteRepository
4 | import com.akshayashokcode.notepad.feature_note.domain.model.Note
5 | import com.akshayashokcode.notepad.feature_note.domain.util.NoteOrder
6 | import com.akshayashokcode.notepad.feature_note.domain.util.OrderType
7 | import com.google.common.truth.Truth.assertThat
8 | import kotlinx.coroutines.flow.first
9 | import kotlinx.coroutines.runBlocking
10 | import org.junit.Before
11 | import org.junit.Test
12 |
13 | class GetNotesTest {
14 |
15 | private lateinit var getNotes: GetNotes
16 | private lateinit var fakeRepository: FakeNoteRepository
17 |
18 | @Before
19 | fun setUp() {
20 | fakeRepository = FakeNoteRepository()
21 | getNotes = GetNotes(fakeRepository)
22 |
23 | val notesToInsert = mutableListOf()
24 | ('a'..'z').forEachIndexed { index, c ->
25 | notesToInsert.add(
26 | Note(
27 | title = c.toString(),
28 | content = c.toString(),
29 | timeStamp = index.toLong(),
30 | color = index
31 | )
32 | )
33 | }
34 | notesToInsert.shuffle()
35 | runBlocking {
36 | notesToInsert.forEach { fakeRepository.insertNote(it) }
37 | }
38 | }
39 |
40 | @Test
41 | fun `Order notes by title ascending, correct order`() = runBlocking {
42 | val notes = getNotes(NoteOrder.Title(OrderType.Ascending)).first()
43 |
44 | for(i in 0..notes.size - 2) {
45 | assertThat(notes[i].title).isLessThan(notes[i+1].title)
46 | }
47 | }
48 |
49 | @Test
50 | fun `Order notes by title descending, correct order`() = runBlocking {
51 | val notes = getNotes(NoteOrder.Title(OrderType.Descending)).first()
52 |
53 | for(i in 0..notes.size - 2) {
54 | assertThat(notes[i].title).isGreaterThan(notes[i+1].title)
55 | }
56 | }
57 |
58 | @Test
59 | fun `Order notes by date ascending, correct order`() = runBlocking {
60 | val notes = getNotes(NoteOrder.Date(OrderType.Ascending)).first()
61 |
62 | for(i in 0..notes.size - 2) {
63 | assertThat(notes[i].timeStamp).isLessThan(notes[i+1].timeStamp)
64 | }
65 | }
66 |
67 | @Test
68 | fun `Order notes by date descending, correct order`() = runBlocking {
69 | val notes = getNotes(NoteOrder.Date(OrderType.Descending)).first()
70 |
71 | for(i in 0..notes.size - 2) {
72 | assertThat(notes[i].timeStamp).isGreaterThan(notes[i+1].timeStamp)
73 | }
74 | }
75 |
76 | @Test
77 | fun `Order notes by color ascending, correct order`() = runBlocking {
78 | val notes = getNotes(NoteOrder.Color(OrderType.Ascending)).first()
79 |
80 | for(i in 0..notes.size - 2) {
81 | assertThat(notes[i].color).isLessThan(notes[i+1].color)
82 | }
83 | }
84 |
85 | @Test
86 | fun `Order notes by color descending, correct order`() = runBlocking {
87 | val notes = getNotes(NoteOrder.Color(OrderType.Descending)).first()
88 |
89 | for(i in 0..notes.size - 2) {
90 | assertThat(notes[i].color).isGreaterThan(notes[i+1].color)
91 | }
92 | }
93 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/presentation/notes/components/NoteItem.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.presentation.notes.components
2 |
3 | import androidx.compose.foundation.Canvas
4 | import androidx.compose.foundation.layout.*
5 | import androidx.compose.material.icons.Icons
6 | import androidx.compose.material.icons.filled.Delete
7 | import androidx.compose.material3.Icon
8 | import androidx.compose.material3.IconButton
9 | import androidx.compose.material3.MaterialTheme
10 | import androidx.compose.material3.Text
11 | import androidx.compose.runtime.Composable
12 | import androidx.compose.ui.Alignment
13 | import androidx.compose.ui.Modifier
14 | import androidx.compose.ui.geometry.CornerRadius
15 | import androidx.compose.ui.geometry.Offset
16 | import androidx.compose.ui.graphics.Color
17 | import androidx.compose.ui.graphics.Path
18 | import androidx.compose.ui.graphics.drawscope.clipPath
19 | import androidx.compose.ui.platform.testTag
20 | import androidx.compose.ui.text.style.TextOverflow
21 | import androidx.compose.ui.unit.Dp
22 | import androidx.compose.ui.unit.dp
23 | import androidx.core.graphics.ColorUtils
24 | import com.akshayashokcode.notepad.core.util.TestTags
25 | import com.akshayashokcode.notepad.feature_note.domain.model.Note
26 |
27 |
28 | @Composable
29 | fun NoteItem(
30 | note: Note,
31 | modifier: Modifier = Modifier,
32 | cornerRadius: Dp = 10.dp,
33 | cutCornerSize: Dp = 30.dp,
34 | onDeleteClick: () -> Unit
35 | ) {
36 | Box(modifier = modifier.testTag(TestTags.NOTE_ITEM)) {
37 | Canvas(modifier = Modifier.matchParentSize()) {
38 | val clipPath = Path().apply {
39 | lineTo(size.width - cutCornerSize.toPx(), 0f)
40 | lineTo(size.width, cutCornerSize.toPx())
41 | lineTo(size.width, size.height)
42 | lineTo(0f, size.height)
43 | close()
44 | }
45 | clipPath(clipPath) {
46 | drawRoundRect(
47 | color = Color(note.color),
48 | size = size,
49 | cornerRadius = CornerRadius(cornerRadius.toPx())
50 | )
51 | drawRoundRect(
52 | color = Color(
53 | ColorUtils.blendARGB(note.color, 0x000000, 0.2f)
54 | ),
55 | topLeft = Offset(
56 | size.width - cutCornerSize.toPx() + 100f,
57 | cutCornerSize.toPx() + 100f
58 | ),
59 | size = size,
60 | cornerRadius = CornerRadius(cornerRadius.toPx())
61 | )
62 | }
63 | }
64 | Column(
65 | modifier = Modifier
66 | .fillMaxSize()
67 | .padding(16.dp)
68 | .padding(end = 32.dp)
69 | ) {
70 | Text(
71 | text = note.title,
72 | style = MaterialTheme.typography.titleLarge,
73 | color = MaterialTheme.colorScheme.onSurface,
74 | maxLines = 1,
75 | overflow = TextOverflow.Ellipsis
76 | )
77 | Spacer(modifier = Modifier.height(8.dp))
78 | Text(
79 | text = note.content,
80 | style = MaterialTheme.typography.bodyLarge,
81 | color = MaterialTheme.colorScheme.onSurface,
82 | maxLines = 10,
83 | overflow = TextOverflow.Ellipsis
84 | )
85 | }
86 | IconButton(
87 | onClick = onDeleteClick,
88 | modifier = Modifier.align(Alignment.BottomEnd)
89 | ) {
90 | Icon(
91 | imageVector = Icons.Default.Delete,
92 | contentDescription = "Delete Note",
93 | tint = MaterialTheme.colorScheme.onSurface
94 | )
95 |
96 | }
97 | }
98 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/presentation/add_edit_note/AddEditNoteViewModel.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.presentation.add_edit_note
2 |
3 | import androidx.compose.runtime.State
4 | import androidx.compose.runtime.mutableStateOf
5 | import androidx.compose.ui.graphics.toArgb
6 | import androidx.lifecycle.SavedStateHandle
7 | import androidx.lifecycle.ViewModel
8 | import androidx.lifecycle.viewModelScope
9 | import com.akshayashokcode.notepad.feature_note.domain.model.Note
10 | import com.akshayashokcode.notepad.feature_note.domain.use_case.NoteUseCases
11 | import dagger.hilt.android.lifecycle.HiltViewModel
12 | import kotlinx.coroutines.flow.MutableSharedFlow
13 | import kotlinx.coroutines.flow.asSharedFlow
14 | import kotlinx.coroutines.launch
15 | import javax.inject.Inject
16 |
17 | @HiltViewModel
18 | class AddEditNoteViewModel @Inject constructor(
19 | private val noteUseCases: NoteUseCases,
20 | savedStateHandle: SavedStateHandle
21 | ) : ViewModel() {
22 |
23 | private val _noteTitle = mutableStateOf(
24 | NoteTextFieldState(
25 | hint = "Enter title..."
26 | )
27 | )
28 | val noteTitle: State = _noteTitle
29 |
30 | private val _noteContent = mutableStateOf(
31 | NoteTextFieldState(
32 | hint = "Enter some content"
33 | )
34 | )
35 | val noteContent: State = _noteContent
36 |
37 | private val _noteColor = mutableStateOf(Note.noteColors.random().toArgb())
38 | val noteColor: State = _noteColor
39 |
40 | private val _eventFlow = MutableSharedFlow()
41 | val eventFlow = _eventFlow.asSharedFlow()
42 |
43 | private var currentNoteId: Int? = null
44 |
45 | init {
46 | savedStateHandle.get("noteId")?.let { noteId ->
47 | if (noteId != -1) {
48 | viewModelScope.launch {
49 | noteUseCases.getNote(noteId)?.also { note ->
50 | currentNoteId = note.id
51 | _noteTitle.value = noteTitle.value.copy(
52 | text = note.title,
53 | isHintVisible = false
54 | )
55 | //TODO
56 | _noteContent.value = noteContent.value.copy(
57 | text = note.content,
58 | isHintVisible = false
59 | )
60 | _noteColor.value = note.color
61 | }
62 | }
63 | }
64 | }
65 | }
66 |
67 | fun onEvent(event: AddEditNoteEvent) {
68 | when (event) {
69 | is AddEditNoteEvent.EnteredTitle -> {
70 | _noteTitle.value = noteTitle.value.copy(
71 | text = event.value
72 | )
73 | }
74 |
75 | is AddEditNoteEvent.ChangeTitleFocus -> {
76 | _noteTitle.value = noteTitle.value.copy(
77 | isHintVisible = !event.focusState.isFocused &&
78 | noteTitle.value.text.isBlank()
79 | )
80 | }
81 | //TODO
82 | is AddEditNoteEvent.EnteredContent -> {
83 | _noteContent.value = noteContent.value.copy(
84 | text = event.value
85 | )
86 | }
87 |
88 | is AddEditNoteEvent.ChangeContentFocus -> {
89 | _noteContent.value = noteContent.value.copy(
90 | isHintVisible = !event.focusState.isFocused &&
91 | noteContent.value.text.isBlank()
92 | )
93 | }
94 |
95 | is AddEditNoteEvent.ChangeColor -> {
96 | _noteColor.value = event.color
97 | }
98 |
99 | is AddEditNoteEvent.SaveNote -> {
100 | viewModelScope.launch {
101 | try {
102 | noteUseCases.addNote(
103 | Note(
104 | title = noteTitle.value.text.trim().ifEmpty { "Untitled Note" },
105 | content = noteContent.value.text.trim(),
106 | timeStamp = System.currentTimeMillis(),
107 | color = noteColor.value,
108 | id = currentNoteId
109 | )
110 | )
111 | _eventFlow.emit(UiEvent.SaveNote)
112 | } catch (e: Note.InvalidNoteException) {
113 | _eventFlow.emit(
114 | UiEvent.ShowSnackBar(
115 | message = e.message ?: "Couldn't save note"
116 | )
117 | )
118 | }
119 | }
120 | }
121 |
122 | }
123 | }
124 |
125 | sealed class UiEvent {
126 | data class ShowSnackBar(val message: String) : UiEvent()
127 | object SaveNote : UiEvent()
128 | }
129 | }
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.application'
3 | id 'org.jetbrains.kotlin.android'
4 | id 'com.google.dagger.hilt.android'
5 | id 'com.google.devtools.ksp'
6 | id "org.jetbrains.kotlin.plugin.compose" version "2.1.0"
7 | }
8 |
9 | android {
10 | compileSdkVersion 36
11 |
12 | defaultConfig {
13 | applicationId "com.akshayashokcode.notepad"
14 | minSdk 24
15 | targetSdk 36
16 | versionCode 14
17 | versionName "1.4.6"
18 |
19 | testInstrumentationRunner "com.akshayashokcode.notepad.HiltTestRunner"
20 | vectorDrawables {
21 | useSupportLibrary true
22 | }
23 | }
24 |
25 | buildTypes {
26 | release {
27 | minifyEnabled true
28 | shrinkResources true
29 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
30 | }
31 | }
32 |
33 | compileOptions {
34 | sourceCompatibility JavaVersion.VERSION_11
35 | targetCompatibility JavaVersion.VERSION_11
36 | }
37 |
38 | kotlinOptions {
39 | jvmTarget = '11'
40 | }
41 |
42 | buildFeatures {
43 | compose true
44 | }
45 |
46 | packagingOptions {
47 | resources {
48 | excludes += '/META-INF/{AL2.0,LGPL2.1}'
49 | }
50 | }
51 | namespace 'com.akshayashokcode.notepad'
52 | }
53 |
54 | composeCompiler {
55 | reportsDestination = layout.buildDirectory.dir("compose_compiler")
56 | stabilityConfigurationFile = rootProject.layout.projectDirectory.file("stability_config.conf")
57 | enableStrongSkippingMode = true
58 | }
59 |
60 | dependencies {
61 | // Compose BOM - manages all compose library versions
62 | implementation platform("androidx.compose:compose-bom:$compose_bom_version")
63 |
64 | // Core Android libraries
65 | implementation 'androidx.core:core-ktx:1.15.0'
66 | implementation 'androidx.appcompat:appcompat:1.7.1'
67 | implementation 'com.google.android.material:material:1.12.0'
68 |
69 | // Compose UI
70 | implementation 'androidx.compose.ui:ui'
71 | implementation 'androidx.compose.ui:ui-tooling-preview'
72 | implementation 'androidx.compose.ui:ui-graphics'
73 | implementation 'androidx.compose.ui:ui-text-google-fonts'
74 |
75 | // Material3
76 | implementation 'androidx.compose.material3:material3'
77 | implementation 'androidx.compose.material3:material3-window-size-class'
78 | implementation 'androidx.compose.material:material-icons-extended'
79 |
80 | // Activity & Lifecycle
81 | implementation 'androidx.activity:activity-compose:1.9.3'
82 | implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.8.7'
83 | implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.8.7"
84 | implementation "androidx.lifecycle:lifecycle-runtime-compose:2.8.7"
85 |
86 | // Navigation
87 | implementation "androidx.navigation:navigation-compose:2.8.4"
88 |
89 | // Coroutines
90 | implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0'
91 | implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.9.0'
92 |
93 | // Dagger Hilt
94 | implementation "com.google.dagger:hilt-android:2.56.2"
95 | implementation "androidx.hilt:hilt-navigation-compose:1.2.0"
96 | ksp "com.google.dagger:hilt-android-compiler:2.56.2"
97 | ksp "androidx.hilt:hilt-compiler:1.2.0"
98 |
99 | // Room
100 | implementation "androidx.room:room-runtime:2.7.2"
101 | implementation "androidx.room:room-ktx:2.7.2"
102 | ksp "androidx.room:room-compiler:2.7.2"
103 |
104 | // System UI Controller (for status bar)
105 | implementation "com.google.accompanist:accompanist-systemuicontroller:0.32.0"
106 |
107 | // In-App Updates
108 | implementation 'com.google.android.play:app-update:2.1.0'
109 | implementation 'com.google.android.play:app-update-ktx:2.1.0'
110 |
111 | // Testing - Local unit tests
112 | testImplementation "junit:junit:4.13.2"
113 | testImplementation "androidx.test:core:1.6.0"
114 | testImplementation "androidx.arch.core:core-testing:2.2.0"
115 | testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.9.0"
116 | testImplementation "com.google.truth:truth:1.1.3"
117 | testImplementation "com.squareup.okhttp3:mockwebserver:4.9.1"
118 | testImplementation "io.mockk:mockk:1.10.5"
119 |
120 |
121 | // Testing - Instrumentation tests
122 | androidTestImplementation 'androidx.test.ext:junit:1.1.5'
123 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
124 | androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4"
125 | androidTestImplementation "androidx.arch.core:core-testing:2.2.0"
126 | androidTestImplementation "com.google.truth:truth:1.1.3"
127 | androidTestImplementation 'androidx.test.ext:junit:1.1.5'
128 | androidTestImplementation 'androidx.test:core-ktx:1.5.0'
129 | androidTestImplementation "com.squareup.okhttp3:mockwebserver:4.9.1"
130 | androidTestImplementation "io.mockk:mockk-android:1.10.5"
131 | androidTestImplementation 'androidx.test:runner:1.5.2'
132 |
133 | // Hilt testing
134 | androidTestImplementation 'com.google.dagger:hilt-android-testing:2.37'
135 | kspAndroidTest 'com.google.dagger:hilt-android-compiler:2.56.2'
136 |
137 | // Compose testing
138 | androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
139 | debugImplementation 'androidx.compose.ui:ui-tooling'
140 | debugImplementation 'androidx.compose.ui:ui-test-manifest'
141 | }
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/akshayashokcode/notepad/feature_note/presentation/NotesEndToEndTest.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.presentation
2 |
3 | import androidx.compose.animation.ExperimentalAnimationApi
4 | import androidx.compose.ui.test.*
5 | import androidx.compose.ui.test.junit4.createAndroidComposeRule
6 | import androidx.navigation.NavType
7 | import androidx.navigation.compose.NavHost
8 | import androidx.navigation.compose.composable
9 | import androidx.navigation.compose.rememberNavController
10 | import androidx.navigation.navArgument
11 | import com.akshayashokcode.notepad.core.util.TestTags
12 | import com.akshayashokcode.notepad.di.AppModule
13 | import com.akshayashokcode.notepad.feature_note.presentation.add_edit_note.AddEditNoteScreen
14 | import com.akshayashokcode.notepad.feature_note.presentation.notes.NotesScreen
15 | import com.akshayashokcode.notepad.feature_note.presentation.util.Screen
16 | import com.akshayashokcode.notepad.ui.theme.NotePadTheme
17 | import dagger.hilt.android.testing.HiltAndroidRule
18 | import dagger.hilt.android.testing.HiltAndroidTest
19 | import dagger.hilt.android.testing.UninstallModules
20 | import org.junit.Before
21 | import org.junit.Rule
22 | import org.junit.Test
23 |
24 | @HiltAndroidTest
25 | @UninstallModules(AppModule::class)
26 | class NotesEndToEndTest {
27 |
28 | @get:Rule(order = 0)
29 | val hiltRule = HiltAndroidRule(this)
30 |
31 | @get:Rule(order = 1)
32 | val composeRule = createAndroidComposeRule()
33 |
34 | @ExperimentalAnimationApi
35 | @Before
36 | fun setUp() {
37 | hiltRule.inject()
38 | composeRule.setContent {
39 | NotePadTheme() {
40 | val navController = rememberNavController()
41 | NavHost(
42 | navController = navController,
43 | startDestination = Screen.NotesScreen.route
44 | ) {
45 | composable(route = Screen.NotesScreen.route) {
46 | NotesScreen(navController = navController)
47 | }
48 | composable(
49 | route = Screen.AddEditNoteScreen.route +
50 | "?noteId={noteId}¬eColor={noteColor}",
51 | arguments = listOf(
52 | navArgument(
53 | name = "noteId"
54 | ) {
55 | type = NavType.IntType
56 | defaultValue = -1
57 | },
58 | navArgument(
59 | name = "noteColor"
60 | ) {
61 | type = NavType.IntType
62 | defaultValue = -1
63 | },
64 | )
65 | ) {
66 | val color = it.arguments?.getInt("noteColor") ?: -1
67 | AddEditNoteScreen(
68 | navController = navController,
69 | noteColor = color
70 | )
71 | }
72 | }
73 | }
74 | }
75 | }
76 |
77 | @Test
78 | fun saveNewNote_editAfterwards() {
79 | // Click on FAB to get to add note screen
80 | composeRule.onNodeWithContentDescription("Add").performClick()
81 |
82 | // Enter texts in title and content text fields
83 | composeRule
84 | .onNodeWithTag(TestTags.TITLE_TEXT_FIELD)
85 | .performTextInput("test-title")
86 | composeRule
87 | .onNodeWithTag(TestTags.CONTENT_TEXT_FIELD)
88 | .performTextInput("test-content")
89 | // Save the new
90 | composeRule.onNodeWithContentDescription("Save").performClick()
91 |
92 | // Make sure there is a note in the list with our title and content
93 | composeRule.onNodeWithText("test-title").assertIsDisplayed()
94 | // Click on note to edit it
95 | composeRule.onNodeWithText("test-title").performClick()
96 |
97 | // Make sure title and content text fields contain note title and content
98 | composeRule
99 | .onNodeWithTag(TestTags.TITLE_TEXT_FIELD)
100 | .assertTextEquals("test-title")
101 | composeRule
102 | .onNodeWithTag(TestTags.CONTENT_TEXT_FIELD)
103 | .assertTextEquals("test-content")
104 | // Add the text "2" to the title text field
105 | composeRule
106 | .onNodeWithTag(TestTags.TITLE_TEXT_FIELD)
107 | .performTextInput("2")
108 | // Update the note
109 | composeRule.onNodeWithContentDescription("Save").performClick()
110 |
111 | // Make sure the update was applied to the list
112 | composeRule.onNodeWithText("test-title2").assertIsDisplayed()
113 | }
114 |
115 | @Test
116 | fun saveNewNotes_orderByTitleDescending() {
117 | for(i in 1..3) {
118 | // Click on FAB to get to add note screen
119 | composeRule.onNodeWithContentDescription("Add").performClick()
120 |
121 | // Enter texts in title and content text fields
122 | composeRule
123 | .onNodeWithTag(TestTags.TITLE_TEXT_FIELD)
124 | .performTextInput(i.toString())
125 | composeRule
126 | .onNodeWithTag(TestTags.CONTENT_TEXT_FIELD)
127 | .performTextInput(i.toString())
128 | // Save the new
129 | composeRule.onNodeWithContentDescription("Save").performClick()
130 | }
131 |
132 | composeRule.onNodeWithText("1").assertIsDisplayed()
133 | composeRule.onNodeWithText("2").assertIsDisplayed()
134 | composeRule.onNodeWithText("3").assertIsDisplayed()
135 |
136 | composeRule
137 | .onNodeWithContentDescription("Sort")
138 | .performClick()
139 | composeRule
140 | .onNodeWithContentDescription("Title")
141 | .performClick()
142 | composeRule
143 | .onNodeWithContentDescription("Descending")
144 | .performClick()
145 |
146 | composeRule.onAllNodesWithTag(TestTags.NOTE_ITEM)[0]
147 | .assertTextContains("3")
148 | composeRule.onAllNodesWithTag(TestTags.NOTE_ITEM)[1]
149 | .assertTextContains("2")
150 | composeRule.onAllNodesWithTag(TestTags.NOTE_ITEM)[2]
151 | .assertTextContains("1")
152 | }
153 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/presentation/add_edit_note/AddEditNoteScreen.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.presentation.add_edit_note
2 |
3 | import androidx.activity.compose.BackHandler
4 | import androidx.compose.animation.Animatable
5 | import androidx.compose.animation.core.tween
6 | import androidx.compose.foundation.background
7 | import androidx.compose.foundation.border
8 | import androidx.compose.foundation.clickable
9 | import androidx.compose.foundation.layout.*
10 | import androidx.compose.foundation.lazy.LazyRow
11 | import androidx.compose.foundation.lazy.items
12 | import androidx.compose.foundation.shape.CircleShape
13 | import androidx.compose.material.icons.Icons
14 | import androidx.compose.material.icons.filled.Save
15 | import androidx.compose.material3.FloatingActionButton
16 | import androidx.compose.material3.Icon
17 | import androidx.compose.material3.MaterialTheme
18 | import androidx.compose.material3.Scaffold
19 | import androidx.compose.material3.SnackbarHost
20 | import androidx.compose.material3.SnackbarHostState
21 | import androidx.compose.runtime.Composable
22 | import androidx.compose.runtime.LaunchedEffect
23 | import androidx.compose.runtime.remember
24 | import androidx.compose.runtime.rememberCoroutineScope
25 | import androidx.compose.ui.Modifier
26 | import androidx.compose.ui.draw.clip
27 | import androidx.compose.ui.draw.shadow
28 | import androidx.compose.ui.graphics.Color
29 | import androidx.compose.ui.graphics.toArgb
30 | import androidx.compose.ui.unit.dp
31 | import androidx.hilt.navigation.compose.hiltViewModel
32 | import androidx.navigation.NavController
33 | import com.akshayashokcode.notepad.core.util.TestTags
34 | import com.akshayashokcode.notepad.feature_note.domain.model.Note
35 | import com.akshayashokcode.notepad.feature_note.presentation.add_edit_note.components.TransparentHintTextFiled
36 | import com.akshayashokcode.notepad.feature_note.presentation.add_edit_note.components.TransparentHintTextFiledContent
37 | import kotlinx.coroutines.flow.collectLatest
38 | import kotlinx.coroutines.launch
39 |
40 | @Composable
41 | fun AddEditNoteScreen(
42 | navController: NavController,
43 | noteColor: Int,
44 | viewModel: AddEditNoteViewModel = hiltViewModel()
45 | ) {
46 | val titleState = viewModel.noteTitle.value
47 | val contentState = viewModel.noteContent.value
48 |
49 | val snackbarHostState = remember { SnackbarHostState() }
50 |
51 | val noteBackgroundAnimatable = remember {
52 | Animatable(
53 | Color(if (noteColor != -1) noteColor else viewModel.noteColor.value)
54 | )
55 | }
56 | val scope = rememberCoroutineScope()
57 |
58 | BackHandler {
59 | if (titleState.text.isNotBlank() || contentState.text.isNotBlank()) {
60 | if (titleState.text.isBlank() && contentState.text.isNotBlank()) {
61 | viewModel.onEvent(AddEditNoteEvent.EnteredTitle("Untitled Note"))
62 | }
63 | viewModel.onEvent(AddEditNoteEvent.SaveNote)
64 | } else {
65 | navController.navigateUp()
66 | }
67 | }
68 |
69 | LaunchedEffect(key1 = true) {
70 | viewModel.eventFlow.collectLatest { event ->
71 | when (event) {
72 | is AddEditNoteViewModel.UiEvent.ShowSnackBar -> {
73 | snackbarHostState.showSnackbar(
74 | message = event.message
75 | )
76 | }
77 |
78 | is AddEditNoteViewModel.UiEvent.SaveNote -> {
79 | navController.navigateUp()
80 | }
81 | }
82 | }
83 | }
84 |
85 | Scaffold(
86 | floatingActionButton = {
87 | FloatingActionButton(
88 | modifier = Modifier
89 | .offset(x = (-2).dp)
90 | .padding(
91 | bottom = WindowInsets.navigationBars
92 | .asPaddingValues()
93 | .calculateBottomPadding()
94 | ),
95 | onClick = {
96 | viewModel.onEvent(AddEditNoteEvent.SaveNote)
97 | },
98 | containerColor = MaterialTheme.colorScheme.primary,
99 | shape = CircleShape
100 | ) {
101 | Icon(imageVector = Icons.Default.Save, contentDescription = "Save")
102 | }
103 | },
104 | snackbarHost = {
105 | SnackbarHost(hostState = snackbarHostState)
106 | }
107 | ) { padding ->
108 | Column(
109 | modifier = Modifier
110 | .fillMaxSize()
111 | .background(noteBackgroundAnimatable.value)
112 | .padding(padding)
113 | .padding(top = 16.dp, bottom = 16.dp)
114 | ) {
115 | LazyRow(
116 | modifier = Modifier
117 | .fillMaxWidth()
118 | .padding(top = 8.dp, bottom = 8.dp),
119 | horizontalArrangement = Arrangement.spacedBy(12.dp),
120 | contentPadding = PaddingValues(horizontal = 4.dp)
121 | ) {
122 | items(Note.noteColors) { color ->
123 | val colorInt = color.toArgb()
124 | Box(
125 | modifier = Modifier
126 | .size(50.dp)
127 | .shadow(15.dp, CircleShape)
128 | .clip(CircleShape)
129 | .background(color)
130 | .border(
131 | width = 3.dp,
132 | color = if ((if (noteColor != -1) noteColor else viewModel.noteColor.value) == colorInt) {
133 | Color.Black
134 | } else Color.Transparent,
135 | shape = CircleShape
136 | )
137 | .clickable {
138 | scope.launch {
139 | noteBackgroundAnimatable.animateTo(
140 | targetValue = Color(colorInt),
141 | animationSpec = tween(
142 | durationMillis = 500
143 | )
144 | )
145 | }
146 | viewModel.onEvent(AddEditNoteEvent.ChangeColor(colorInt))
147 | }
148 | )
149 | }
150 | }
151 | Spacer(modifier = Modifier.height(16.dp))
152 |
153 | TransparentHintTextFiled(
154 | modifier = Modifier.padding(start = 16.dp, end = 16.dp),
155 | text = titleState.text,
156 | hint = titleState.hint,
157 | onValueChange = {
158 | viewModel.onEvent(AddEditNoteEvent.EnteredTitle(it))
159 | },
160 | onFocusChange = {
161 | viewModel.onEvent(AddEditNoteEvent.ChangeTitleFocus(it))
162 | },
163 | isHintVisible = titleState.isHintVisible,
164 | singleLine = true,
165 | textStyle = MaterialTheme.typography.headlineSmall,
166 | testTag = TestTags.TITLE_TEXT_FIELD
167 | )
168 | Spacer(modifier = Modifier.height(16.dp))
169 | TransparentHintTextFiledContent(
170 | text = contentState.text,
171 | hint = contentState.hint,
172 | onValueChange = {
173 | viewModel.onEvent(AddEditNoteEvent.EnteredContent(it))
174 | },
175 | onFocusChange = {
176 | viewModel.onEvent(AddEditNoteEvent.ChangeContentFocus(it))
177 | },
178 | isHintVisible = contentState.isHintVisible,
179 | textStyle = MaterialTheme.typography.bodyLarge,
180 | modifier = Modifier.fillMaxHeight()
181 | .padding(start = 16.dp, end = 16.dp),
182 | testTag = TestTags.CONTENT_TEXT_FIELD
183 | )
184 | }
185 | }
186 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/presentation/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.presentation
2 |
3 | import android.content.Intent
4 | import android.content.IntentSender
5 | import android.graphics.Color
6 | import android.os.Bundle
7 | import android.util.Log
8 | import androidx.activity.ComponentActivity
9 | import androidx.activity.compose.setContent
10 | import androidx.annotation.Nullable
11 | import androidx.compose.animation.ExperimentalAnimationApi
12 | import androidx.compose.foundation.layout.WindowInsets
13 | import androidx.compose.foundation.layout.ime
14 | import androidx.compose.foundation.layout.windowInsetsPadding
15 | import androidx.compose.material3.MaterialTheme
16 | import androidx.compose.material3.Surface
17 | import androidx.compose.ui.Modifier
18 | import androidx.core.view.WindowCompat
19 | import androidx.navigation.NavType
20 | import androidx.navigation.compose.NavHost
21 | import androidx.navigation.compose.composable
22 | import androidx.navigation.compose.rememberNavController
23 | import androidx.navigation.navArgument
24 | import com.akshayashokcode.notepad.feature_note.presentation.add_edit_note.AddEditNoteScreen
25 | import com.akshayashokcode.notepad.feature_note.presentation.add_edit_note.components.AppKeyboardFocusManager
26 | import com.akshayashokcode.notepad.feature_note.presentation.notes.NotesScreen
27 | import com.akshayashokcode.notepad.feature_note.presentation.util.Screen
28 | import com.akshayashokcode.notepad.ui.theme.NotePadTheme
29 | import com.google.android.material.snackbar.Snackbar
30 | import com.google.android.play.core.appupdate.AppUpdateManager
31 | import com.google.android.play.core.appupdate.AppUpdateManagerFactory
32 | import com.google.android.play.core.install.InstallStateUpdatedListener
33 | import com.google.android.play.core.install.model.AppUpdateType
34 | import com.google.android.play.core.install.model.InstallStatus
35 | import com.google.android.play.core.install.model.UpdateAvailability
36 | import dagger.hilt.android.AndroidEntryPoint
37 |
38 |
39 | @AndroidEntryPoint
40 | class MainActivity : ComponentActivity() {
41 | private val REQUEST_CODE = 11
42 | private lateinit var appUpdateManager: AppUpdateManager
43 | private val TAG = "MainActivity"
44 |
45 | override fun onStart() {
46 | super.onStart()
47 | checkUpdate()
48 | }
49 |
50 | override fun onResume() {
51 | super.onResume()
52 | if (::appUpdateManager.isInitialized) {
53 | appUpdateManager
54 | .appUpdateInfo
55 | .addOnSuccessListener { appUpdateInfo ->
56 | // If the update is downloaded but not installed,
57 | // notify the user to complete the update.
58 | if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) {
59 | popupSnackbarForCompleteUpdate()
60 | } else {
61 | Log.d(TAG, "State of update: ${appUpdateInfo.installStatus()}")
62 | }
63 | }
64 | }
65 | }
66 |
67 | @ExperimentalAnimationApi
68 | override fun onCreate(savedInstanceState: Bundle?) {
69 | super.onCreate(savedInstanceState)
70 | setContent {
71 | AppKeyboardFocusManager()
72 | NotePadTheme {
73 | Surface(
74 | color = MaterialTheme.colorScheme.background,
75 | modifier = Modifier.windowInsetsPadding(WindowInsets.ime)
76 | )
77 | {
78 | val navController = rememberNavController()
79 | NavHost(
80 | navController = navController,
81 | startDestination = Screen.NotesScreen.route
82 | ) {
83 | composable(route = Screen.NotesScreen.route) {
84 | NotesScreen(navController = navController)
85 | }
86 | composable(
87 | route = Screen.AddEditNoteScreen.route +
88 | "?noteId={noteId}¬eColor={noteColor}",
89 | arguments = listOf(
90 | navArgument(
91 | name = "noteId"
92 | ) {
93 | type = NavType.IntType
94 | defaultValue = -1
95 | },
96 | navArgument(
97 | name = "noteColor"
98 | ) {
99 | type = NavType.IntType
100 | defaultValue = -1
101 | }
102 | )
103 |
104 | ) {
105 | val color = it.arguments?.getInt("noteColor") ?: -1
106 | AddEditNoteScreen(
107 | navController = navController,
108 | noteColor = color
109 | )
110 | }
111 | }
112 | }
113 |
114 | }
115 | }
116 | }
117 |
118 | private fun checkUpdate() {
119 | // Returns an intent object that you use to check for an update.
120 | appUpdateManager = AppUpdateManagerFactory.create(this)
121 | val appUpdateInfoTask = appUpdateManager.appUpdateInfo
122 | // Checks that the platform will allow the specified type of update.
123 | appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
124 | if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
125 | && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)
126 | ) {
127 | // Request the update.
128 | try {
129 | appUpdateManager.startUpdateFlowForResult(
130 | // Pass the intent that is returned by 'getAppUpdateInfo()'.
131 | appUpdateInfo,
132 | // Or 'AppUpdateType.FLEXIBLE' for flexible updates.
133 | AppUpdateType.FLEXIBLE,
134 | // The current activity making the update request.
135 | this,
136 | // Include a request code to later monitor this update request.
137 | REQUEST_CODE
138 | )
139 | } catch (e: IntentSender.SendIntentException) {
140 | e.printStackTrace()
141 | }
142 | } else {
143 | Log.d(TAG, "Update Status Not available:${appUpdateInfo.updateAvailability()}")
144 | }
145 | }
146 | appUpdateManager.registerListener(listener)
147 | }
148 |
149 | // Create a listener to track request state updates.
150 | private val listener = InstallStateUpdatedListener { state ->
151 | if (state.installStatus() == InstallStatus.DOWNLOADED) {
152 | // After the update is downloaded, show a notification
153 | // and request user confirmation to restart the app.
154 | popupSnackbarForCompleteUpdate()
155 | }
156 | // Log state or install the update.
157 | Log.d(TAG, "State of update: ${state.installStatus()}")
158 | }
159 |
160 | // Displays the snackbar notification and call to action.
161 | private fun popupSnackbarForCompleteUpdate() {
162 | Snackbar.make(
163 | findViewById(android.R.id.content),
164 | "An update has just been downloaded.",
165 | Snackbar.LENGTH_INDEFINITE
166 | ).apply {
167 | setAction("RESTART") { appUpdateManager.completeUpdate() }
168 | show()
169 | }
170 | }
171 |
172 | override fun onActivityResult(requestCode: Int, resultCode: Int, @Nullable data: Intent?) {
173 | super.onActivityResult(requestCode, resultCode, data)
174 | if (requestCode == REQUEST_CODE) {
175 | Log.d(TAG, "onActivityResult: Updated to Latest Features")
176 | if (resultCode != RESULT_OK) {
177 | Log.e(TAG, "onActivityResult: app download failed")
178 | }
179 | }
180 | }
181 |
182 | override fun onStop() {
183 | if (::appUpdateManager.isInitialized) {
184 | appUpdateManager.unregisterListener(listener)
185 | }
186 | super.onStop()
187 | }
188 | }
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | #
4 | # Copyright © 2015-2021 the original authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 |
19 | ##############################################################################
20 | #
21 | # Gradle start up script for POSIX generated by Gradle.
22 | #
23 | # Important for running:
24 | #
25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
26 | # noncompliant, but you have some other compliant shell such as ksh or
27 | # bash, then to run this script, type that shell name before the whole
28 | # command line, like:
29 | #
30 | # ksh Gradle
31 | #
32 | # Busybox and similar reduced shells will NOT work, because this script
33 | # requires all of these POSIX shell features:
34 | # * functions;
35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»;
37 | # * compound commands having a testable exit status, especially «case»;
38 | # * various built-in commands including «command», «set», and «ulimit».
39 | #
40 | # Important for patching:
41 | #
42 | # (2) This script targets any POSIX shell, so it avoids extensions provided
43 | # by Bash, Ksh, etc; in particular arrays are avoided.
44 | #
45 | # The "traditional" practice of packing multiple parameters into a
46 | # space-separated string is a well documented source of bugs and security
47 | # problems, so this is (mostly) avoided, by progressively accumulating
48 | # options in "$@", and eventually passing that to Java.
49 | #
50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
52 | # see the in-line comments for details.
53 | #
54 | # There are tweaks for specific operating systems such as AIX, CygWin,
55 | # Darwin, MinGW, and NonStop.
56 | #
57 | # (3) This script is generated from the Groovy template
58 | # https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
59 | # within the Gradle project.
60 | #
61 | # You can find Gradle at https://github.com/gradle/gradle/.
62 | #
63 | ##############################################################################
64 |
65 | # Attempt to set APP_HOME
66 |
67 | # Resolve links: $0 may be a link
68 | app_path=$0
69 |
70 | # Need this for daisy-chained symlinks.
71 | while
72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
73 | [ -h "$app_path" ]
74 | do
75 | ls=$( ls -ld "$app_path" )
76 | link=${ls#*' -> '}
77 | case $link in #(
78 | /*) app_path=$link ;; #(
79 | *) app_path=$APP_HOME$link ;;
80 | esac
81 | done
82 |
83 | APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
84 |
85 | APP_NAME="Gradle"
86 | APP_BASE_NAME=${0##*/}
87 |
88 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
89 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
90 |
91 | # Use the maximum available, or set MAX_FD != -1 to use that value.
92 | MAX_FD=maximum
93 |
94 | warn () {
95 | echo "$*"
96 | } >&2
97 |
98 | die () {
99 | echo
100 | echo "$*"
101 | echo
102 | exit 1
103 | } >&2
104 |
105 | # OS specific support (must be 'true' or 'false').
106 | cygwin=false
107 | msys=false
108 | darwin=false
109 | nonstop=false
110 | case "$( uname )" in #(
111 | CYGWIN* ) cygwin=true ;; #(
112 | Darwin* ) darwin=true ;; #(
113 | MSYS* | MINGW* ) msys=true ;; #(
114 | NONSTOP* ) nonstop=true ;;
115 | esac
116 |
117 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
118 |
119 |
120 | # Determine the Java command to use to start the JVM.
121 | if [ -n "$JAVA_HOME" ] ; then
122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
123 | # IBM's JDK on AIX uses strange locations for the executables
124 | JAVACMD=$JAVA_HOME/jre/sh/java
125 | else
126 | JAVACMD=$JAVA_HOME/bin/java
127 | fi
128 | if [ ! -x "$JAVACMD" ] ; then
129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
130 |
131 | Please set the JAVA_HOME variable in your environment to match the
132 | location of your Java installation."
133 | fi
134 | else
135 | JAVACMD=java
136 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
137 |
138 | Please set the JAVA_HOME variable in your environment to match the
139 | location of your Java installation."
140 | fi
141 |
142 | # Increase the maximum file descriptors if we can.
143 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
144 | case $MAX_FD in #(
145 | max*)
146 | MAX_FD=$( ulimit -H -n ) ||
147 | warn "Could not query maximum file descriptor limit"
148 | esac
149 | case $MAX_FD in #(
150 | '' | soft) :;; #(
151 | *)
152 | ulimit -n "$MAX_FD" ||
153 | warn "Could not set maximum file descriptor limit to $MAX_FD"
154 | esac
155 | fi
156 |
157 | # Collect all arguments for the java command, stacking in reverse order:
158 | # * args from the command line
159 | # * the main class name
160 | # * -classpath
161 | # * -D...appname settings
162 | # * --module-path (only if needed)
163 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
164 |
165 | # For Cygwin or MSYS, switch paths to Windows format before running java
166 | if "$cygwin" || "$msys" ; then
167 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
168 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
169 |
170 | JAVACMD=$( cygpath --unix "$JAVACMD" )
171 |
172 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
173 | for arg do
174 | if
175 | case $arg in #(
176 | -*) false ;; # don't mess with options #(
177 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
178 | [ -e "$t" ] ;; #(
179 | *) false ;;
180 | esac
181 | then
182 | arg=$( cygpath --path --ignore --mixed "$arg" )
183 | fi
184 | # Roll the args list around exactly as many times as the number of
185 | # args, so each arg winds up back in the position where it started, but
186 | # possibly modified.
187 | #
188 | # NB: a `for` loop captures its iteration list before it begins, so
189 | # changing the positional parameters here affects neither the number of
190 | # iterations, nor the values presented in `arg`.
191 | shift # remove old arg
192 | set -- "$@" "$arg" # push replacement arg
193 | done
194 | fi
195 |
196 | # Collect all arguments for the java command;
197 | # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
198 | # shell script including quotes and variable substitutions, so put them in
199 | # double quotes to make sure that they get re-expanded; and
200 | # * put everything else in single quotes, so that it's not re-expanded.
201 |
202 | set -- \
203 | "-Dorg.gradle.appname=$APP_BASE_NAME" \
204 | -classpath "$CLASSPATH" \
205 | org.gradle.wrapper.GradleWrapperMain \
206 | "$@"
207 |
208 | # Use "xargs" to parse quoted args.
209 | #
210 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed.
211 | #
212 | # In Bash we could simply go:
213 | #
214 | # readarray ARGS < <( xargs -n1 <<<"$var" ) &&
215 | # set -- "${ARGS[@]}" "$@"
216 | #
217 | # but POSIX shell has neither arrays nor command substitution, so instead we
218 | # post-process each arg (as a line of input to sed) to backslash-escape any
219 | # character that might be a shell metacharacter, then use eval to reverse
220 | # that process (while maintaining the separation between arguments), and wrap
221 | # the whole thing up as a single "set" statement.
222 | #
223 | # This will of course break if any of these variables contains a newline or
224 | # an unmatched quote.
225 | #
226 |
227 | eval "set -- $(
228 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
229 | xargs -n1 |
230 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
231 | tr '\n' ' '
232 | )" '"$@"'
233 |
234 | exec "$JAVACMD" "$@"
235 |
--------------------------------------------------------------------------------
/app/src/main/java/com/akshayashokcode/notepad/feature_note/presentation/notes/NotesScreen.kt:
--------------------------------------------------------------------------------
1 | package com.akshayashokcode.notepad.feature_note.presentation.notes
2 |
3 | import androidx.compose.animation.AnimatedVisibility
4 | import androidx.compose.animation.ExperimentalAnimationApi
5 | import androidx.compose.animation.core.Animatable
6 | import androidx.compose.animation.core.LinearOutSlowInEasing
7 | import androidx.compose.animation.core.MutableTransitionState
8 | import androidx.compose.animation.core.keyframes
9 | import androidx.compose.animation.core.tween
10 | import androidx.compose.animation.fadeIn
11 | import androidx.compose.animation.fadeOut
12 | import androidx.compose.animation.slideInVertically
13 | import androidx.compose.animation.slideOutVertically
14 | import androidx.compose.foundation.Canvas
15 | import androidx.compose.foundation.clickable
16 | import androidx.compose.foundation.interaction.MutableInteractionSource
17 | import androidx.compose.foundation.layout.Arrangement
18 | import androidx.compose.foundation.layout.Box
19 | import androidx.compose.foundation.layout.Column
20 | import androidx.compose.foundation.layout.Row
21 | import androidx.compose.foundation.layout.Spacer
22 | import androidx.compose.foundation.layout.WindowInsets
23 | import androidx.compose.foundation.layout.asPaddingValues
24 | import androidx.compose.foundation.layout.fillMaxSize
25 | import androidx.compose.foundation.layout.fillMaxWidth
26 | import androidx.compose.foundation.layout.height
27 | import androidx.compose.foundation.layout.navigationBars
28 | import androidx.compose.foundation.layout.offset
29 | import androidx.compose.foundation.layout.padding
30 | import androidx.compose.foundation.layout.size
31 | import androidx.compose.foundation.lazy.LazyColumn
32 | import androidx.compose.foundation.lazy.items
33 | import androidx.compose.material.icons.Icons
34 | import androidx.compose.material.icons.automirrored.filled.Sort
35 | import androidx.compose.material.icons.filled.Add
36 | import androidx.compose.material3.Icon
37 | import androidx.compose.material3.IconButton
38 | import androidx.compose.material3.MaterialTheme
39 | import androidx.compose.material3.Scaffold
40 | import androidx.compose.material3.Snackbar
41 | import androidx.compose.material3.SnackbarDuration
42 | import androidx.compose.material3.SnackbarHost
43 | import androidx.compose.material3.SnackbarHostState
44 | import androidx.compose.material3.SnackbarResult
45 | import androidx.compose.material3.Text
46 | import androidx.compose.runtime.Composable
47 | import androidx.compose.runtime.getValue
48 | import androidx.compose.runtime.mutableStateOf
49 | import androidx.compose.runtime.remember
50 | import androidx.compose.runtime.rememberCoroutineScope
51 | import androidx.compose.runtime.setValue
52 | import androidx.compose.ui.Alignment
53 | import androidx.compose.ui.Modifier
54 | import androidx.compose.ui.draw.alpha
55 | import androidx.compose.ui.graphics.toArgb
56 | import androidx.compose.ui.platform.testTag
57 | import androidx.compose.ui.text.TextStyle
58 | import androidx.compose.ui.unit.dp
59 | import androidx.hilt.navigation.compose.hiltViewModel
60 | import androidx.navigation.NavController
61 | import com.akshayashokcode.notepad.core.util.TestTags
62 | import com.akshayashokcode.notepad.feature_note.domain.model.Note
63 | import com.akshayashokcode.notepad.feature_note.presentation.notes.components.EmptyScreenText
64 | import com.akshayashokcode.notepad.feature_note.presentation.notes.components.NoteItem
65 | import com.akshayashokcode.notepad.feature_note.presentation.notes.components.OrderSection
66 | import com.akshayashokcode.notepad.feature_note.presentation.util.Screen
67 | import com.akshayashokcode.notepad.ui.theme.customTypography
68 | import kotlinx.coroutines.launch
69 |
70 | enum class FabState { Idle, Exploded }
71 |
72 | @ExperimentalAnimationApi
73 | @Composable
74 | fun NotesScreen(
75 | navController: NavController,
76 | viewModel: NotesViewModel = hiltViewModel()
77 | ) {
78 | val state = viewModel.state.value
79 | val snackbarHostState = remember { SnackbarHostState() }
80 | val scope = rememberCoroutineScope()
81 | val animVisibleState = remember { MutableTransitionState(false) }
82 | .apply { targetState = true }
83 | val notesAvailable = state.notes.isNotEmpty()
84 |
85 | val primaryColor = MaterialTheme.colorScheme.primary
86 | val explosionColor = remember { Note.noteColors.random() }
87 | val fabSize = remember { Animatable(170f) }
88 | val iconAlpha = remember { Animatable(1f) }
89 | var fabState by remember { mutableStateOf(FabState.Idle) }
90 |
91 | Scaffold(
92 | floatingActionButton = {
93 | AnimatedVisibility(
94 | visibleState = animVisibleState,
95 | enter = fadeIn(
96 | animationSpec = tween(durationMillis = 500, easing = LinearOutSlowInEasing)
97 | )
98 | ) {
99 | Box(
100 | modifier = Modifier
101 | .size(80.dp)
102 | .offset(x = 10.dp)
103 | .padding(
104 | bottom = WindowInsets.navigationBars
105 | .asPaddingValues()
106 | .calculateBottomPadding()
107 | )
108 | .clickable(
109 | indication = null,
110 | interactionSource = remember { MutableInteractionSource() }
111 | ) {
112 | if (fabState == FabState.Idle) {
113 | fabState = FabState.Exploded
114 | scope.launch {
115 | // Fade out icon first
116 | iconAlpha.animateTo(0f, tween(150))
117 | // Then explode background
118 | fabSize.animateTo(
119 | targetValue = 6000f,
120 | animationSpec = keyframes {
121 | durationMillis = 300
122 | 170f at 0
123 | 90f at 80
124 | 6000f at 300
125 | }
126 | )
127 |
128 | // close order section when clicking FAB
129 | viewModel.onEvent(NotesEvent.CloseOrderSection)
130 | navController.navigate(
131 | Screen.AddEditNoteScreen.route +
132 | "?noteColor=${explosionColor.toArgb()}"
133 | )
134 | }
135 | }
136 | },
137 | contentAlignment = Alignment.Center
138 | ) {
139 | // Exploding background
140 | Canvas(modifier = Modifier.matchParentSize()) {
141 | val color =
142 | if (fabState == FabState.Exploded) explosionColor else primaryColor
143 | drawCircle(
144 | color = color,
145 | radius = fabSize.value / 2f,
146 | center = center
147 | )
148 | }
149 | // Static icon that fades out
150 | Icon(
151 | imageVector = Icons.Default.Add,
152 | contentDescription = "Add",
153 | tint = MaterialTheme.colorScheme.onPrimary,
154 | modifier = Modifier.alpha(iconAlpha.value)
155 | )
156 | }
157 | }
158 | },
159 | snackbarHost = {
160 | SnackbarHost(hostState = snackbarHostState) { data ->
161 | Snackbar(
162 | snackbarData = data,
163 | actionColor = MaterialTheme.colorScheme.surface
164 | )
165 | }
166 | }
167 | ) { paddingValues ->
168 | Column(
169 | modifier = Modifier
170 | .fillMaxSize()
171 | .padding(paddingValues)
172 | .padding(start = 16.dp, end = 16.dp)
173 | ) {
174 | Row(
175 | modifier = Modifier.fillMaxWidth(),
176 | horizontalArrangement = Arrangement.SpaceBetween,
177 | verticalAlignment = Alignment.CenterVertically
178 | ) {
179 | Text(
180 | text = "Notes",
181 | style = TextStyle(
182 | color = MaterialTheme.colorScheme.onBackground,
183 | fontStyle = MaterialTheme.typography.headlineMedium.fontStyle,
184 | fontSize = MaterialTheme.typography.headlineMedium.fontSize,
185 | fontWeight = MaterialTheme.typography.headlineMedium.fontWeight,
186 | fontFamily = customTypography.bodyLarge.fontFamily
187 | )
188 | )
189 | if (notesAvailable) {
190 | IconButton(
191 | onClick = {
192 | viewModel.onEvent(NotesEvent.ToggleOrderSection)
193 | }
194 | ) {
195 | Icon(
196 | imageVector = Icons.AutoMirrored.Filled.Sort,
197 | contentDescription = "Sort",
198 | )
199 | }
200 | }
201 | }
202 | AnimatedVisibility(
203 | visible = state.isOrderSectionVisible,
204 | enter = fadeIn() + slideInVertically(),
205 | exit = fadeOut() + slideOutVertically()
206 | ) {
207 | OrderSection(
208 | modifier = Modifier
209 | .fillMaxWidth()
210 | .padding(vertical = 16.dp)
211 | .testTag(TestTags.ORDER_SECTION),
212 | noteOrder = state.noteOrder,
213 | onOrderChange = {
214 | viewModel.onEvent(NotesEvent.Order(it))
215 | }
216 | )
217 | }
218 | Spacer(modifier = Modifier.height(16.dp))
219 |
220 | if (notesAvailable) {
221 | LazyColumn(modifier = Modifier.fillMaxSize()) {
222 | items(state.notes) { note ->
223 | NoteItem(
224 | note = note,
225 | modifier = Modifier
226 | .fillMaxWidth()
227 | .clickable {
228 | // close order section when clicking FAB
229 | viewModel.onEvent(NotesEvent.CloseOrderSection)
230 | navController.navigate(
231 | Screen.AddEditNoteScreen.route +
232 | "?noteId=${note.id}¬eColor=${note.color}"
233 | )
234 | },
235 | onDeleteClick = {
236 | viewModel.onEvent(NotesEvent.DeleteNote(note))
237 | scope.launch {
238 | val result = snackbarHostState.showSnackbar(
239 | message = "Note deleted",
240 | actionLabel = "Undo",
241 | withDismissAction = true,
242 | duration = SnackbarDuration.Short
243 | )
244 | if (result == SnackbarResult.ActionPerformed) {
245 | viewModel.onEvent(NotesEvent.RestoreNote)
246 | }
247 | }
248 | }
249 | )
250 | Spacer(modifier = Modifier.height(16.dp))
251 | if (state.notes.last() == note) {
252 | Spacer(modifier = Modifier.height(60.dp))
253 | }
254 | }
255 | }
256 | } else {
257 | EmptyScreenText()
258 | }
259 | }
260 | }
261 | }
262 |
--------------------------------------------------------------------------------