Skip to content

Commit 180ceed

Browse files
author
Avadh H. Kachhadiya
committed
first commit
0 parents  commit 180ceed

File tree

69 files changed

+1616
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1616
-0
lines changed

.gitignore

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties

.idea/.gitignore

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/compiler.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/deploymentTargetDropDown.xml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/kotlinc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/migrations.xml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle.kts

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
plugins {
2+
id("com.android.application")
3+
id("org.jetbrains.kotlin.android")
4+
id("kotlin-kapt")
5+
}
6+
7+
android {
8+
namespace = "com.example.roomhandson"
9+
compileSdk = 34
10+
11+
defaultConfig {
12+
applicationId = "com.example.roomhandson"
13+
minSdk = 24
14+
targetSdk = 34
15+
versionCode = 1
16+
versionName = "1.0"
17+
18+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
19+
}
20+
21+
buildFeatures {
22+
viewBinding = true
23+
}
24+
buildTypes {
25+
release {
26+
isMinifyEnabled = false
27+
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
28+
}
29+
}
30+
compileOptions {
31+
sourceCompatibility = JavaVersion.VERSION_1_8
32+
targetCompatibility = JavaVersion.VERSION_1_8
33+
}
34+
kotlinOptions {
35+
jvmTarget = "1.8"
36+
}
37+
}
38+
39+
dependencies {
40+
41+
implementation("androidx.core:core-ktx:1.12.0")
42+
implementation("androidx.appcompat:appcompat:1.6.1")
43+
implementation("com.google.android.material:material:1.11.0")
44+
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
45+
testImplementation("junit:junit:4.13.2")
46+
androidTestImplementation("androidx.test.ext:junit:1.1.5")
47+
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
48+
49+
implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.7.0")
50+
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0")
51+
implementation("androidx.activity:activity-ktx:1.8.2")
52+
implementation("androidx.fragment:fragment-ktx:1.6.2")
53+
54+
// Room DB
55+
kapt("androidx.room:room-compiler:2.6.1")
56+
implementation("androidx.room:room-ktx:2.6.1")
57+
implementation("androidx.room:room-runtime:2.6.1")
58+
}

app/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.example.roomhandson
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.example.roomhandson", appContext.packageName)
23+
}
24+
}

app/src/main/AndroidManifest.xml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<application
6+
android:name=".app.NotesApp"
7+
android:allowBackup="true"
8+
android:dataExtractionRules="@xml/data_extraction_rules"
9+
android:fullBackupContent="@xml/backup_rules"
10+
android:icon="@mipmap/ic_launcher"
11+
android:label="@string/app_name"
12+
android:roundIcon="@mipmap/ic_launcher_round"
13+
android:supportsRtl="true"
14+
android:theme="@style/Theme.RoomHandsOn"
15+
tools:targetApi="31">
16+
<activity
17+
android:name=".MainActivity"
18+
android:exported="true">
19+
<intent-filter>
20+
<action android:name="android.intent.action.MAIN" />
21+
22+
<category android:name="android.intent.category.LAUNCHER" />
23+
</intent-filter>
24+
</activity>
25+
<meta-data
26+
android:name="preloaded_fonts"
27+
android:resource="@array/preloaded_fonts" />
28+
</application>
29+
30+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.example.roomhandson
2+
3+
import android.os.Bundle
4+
import androidx.activity.viewModels
5+
import androidx.appcompat.app.AppCompatActivity
6+
import com.example.roomhandson.app.NotesApp
7+
import com.example.roomhandson.databinding.ActivityMainBinding
8+
import com.example.roomhandson.ui.home.NotesAdapter
9+
import com.example.roomhandson.ui.note.AddNoteFragment
10+
import com.example.roomhandson.utils.AppToast
11+
import com.example.roomhandson.viewmodels.home.NotesViewModel
12+
import com.example.roomhandson.viewmodels.home.NotesViewModelFactory
13+
14+
15+
class MainActivity : AppCompatActivity() {
16+
17+
lateinit var binding: ActivityMainBinding
18+
private val notesViewModel: NotesViewModel by viewModels {
19+
NotesViewModelFactory((application as NotesApp).repository)
20+
}
21+
22+
override fun onCreate(savedInstanceState: Bundle?) {
23+
super.onCreate(savedInstanceState)
24+
binding = ActivityMainBinding.inflate(layoutInflater)
25+
setContentView(binding.root)
26+
27+
binding.noteFAB.setOnClickListener {
28+
onCreateNoteClickHandler(null)
29+
}
30+
31+
setAdapter()
32+
fragmentResultHandler()
33+
}
34+
35+
private fun setAdapter() {
36+
notesViewModel.allNote.observe(this) { note ->
37+
binding.rvNotes.adapter = NotesAdapter(note) {
38+
onCreateNoteClickHandler(it)
39+
}
40+
}
41+
}
42+
43+
private fun onCreateNoteClickHandler(id: Int?) {
44+
binding.noteFAB.hide()
45+
supportFragmentManager.beginTransaction().replace(R.id.homeView, AddNoteFragment(id)).addToBackStack(null)
46+
.commit()
47+
}
48+
49+
private fun fragmentResultHandler() {
50+
supportFragmentManager.setFragmentResultListener("createNote", this) { _, bundle ->
51+
val updatedNoteString = bundle.getString("updatedNoteBundle")
52+
val newNoteString = bundle.getString("newNoteBundle")
53+
val emptyNoteString = bundle.getString("emptyNoteBundle")
54+
val deleteNoteString = bundle.getString("deleteNoteBundle")
55+
56+
val appToast = AppToast()
57+
58+
if (emptyNoteString != null) appToast.showToast(emptyNoteString, this).show()
59+
60+
if (updatedNoteString != null) appToast.showToast(updatedNoteString, this).show()
61+
62+
if (newNoteString != null) appToast.showToast(newNoteString, this).show()
63+
64+
if (deleteNoteString != null) {
65+
appToast.showSnackBar(deleteNoteString, binding.homeView).setAction("Undo") {
66+
notesViewModel.restoreNote()
67+
}.show()
68+
69+
}
70+
}
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example.roomhandson.app
2+
3+
import android.app.Application
4+
import com.example.roomhandson.data.local.databse.NotesDatabase
5+
import com.example.roomhandson.data.repository.NotesRepository
6+
7+
class NotesApp : Application() {
8+
9+
private val database by lazy { NotesDatabase.getDatabase(this) }
10+
val repository by lazy { NotesRepository(database.notesDao()) }
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.example.roomhandson.data.local.dao
2+
3+
import androidx.lifecycle.LiveData
4+
import androidx.room.Dao
5+
import androidx.room.Delete
6+
import androidx.room.Insert
7+
import androidx.room.OnConflictStrategy.Companion.IGNORE
8+
import androidx.room.Query
9+
import androidx.room.Update
10+
import com.example.roomhandson.data.local.enitities.Note
11+
12+
@Dao
13+
interface NotesDao {
14+
@Query("SELECT * FROM notes_table ORDER BY id ASC")
15+
fun getAllNotes(): LiveData<List<Note>>
16+
17+
@Query("SELECT * FROM notes_table WHERE id=:id")
18+
suspend fun getNoteById(id: Int): Note
19+
20+
@Insert(onConflict = IGNORE)
21+
suspend fun createNote(note: Note)
22+
23+
@Update
24+
suspend fun updateNote(note: Note)
25+
26+
@Delete
27+
suspend fun deleteNote(note: Note)
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.example.roomhandson.data.local.databse
2+
3+
import android.content.Context
4+
import androidx.room.Database
5+
import androidx.room.Room
6+
import androidx.room.RoomDatabase
7+
import com.example.roomhandson.data.local.dao.NotesDao
8+
import com.example.roomhandson.data.local.enitities.Note
9+
import com.example.roomhandson.utils.Constants
10+
11+
@Database(entities = [Note::class], version = 1, exportSchema = false)
12+
abstract class NotesDatabase : RoomDatabase() {
13+
14+
abstract fun notesDao(): NotesDao
15+
16+
companion object {
17+
@Volatile
18+
private var INSTANCE: NotesDatabase? = null
19+
20+
fun getDatabase(context: Context): NotesDatabase {
21+
return INSTANCE ?: synchronized(this) {
22+
val instance = Room.databaseBuilder(
23+
context.applicationContext, NotesDatabase::class.java, Constants.NOTE_DB
24+
).build()
25+
26+
INSTANCE = instance
27+
instance
28+
}
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)