Skip to content

Commit

Permalink
Notes App by Sudip
Browse files Browse the repository at this point in the history
  • Loading branch information
sudipbhakat07 committed Oct 3, 2021
1 parent d236b64 commit a512572
Show file tree
Hide file tree
Showing 53 changed files with 1,272 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Notes App/Notes App/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
6 changes: 6 additions & 0 deletions Notes App/Notes App/.idea/compiler.xml

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

20 changes: 20 additions & 0 deletions Notes App/Notes App/.idea/gradle.xml

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

30 changes: 30 additions & 0 deletions Notes App/Notes App/.idea/jarRepositories.xml

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

16 changes: 16 additions & 0 deletions Notes App/Notes App/.idea/misc.xml

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

8 changes: 8 additions & 0 deletions Notes App/Notes App/.idea/vcs.xml

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

1 change: 1 addition & 0 deletions Notes App/Notes App/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
61 changes: 61 additions & 0 deletions Notes App/Notes App/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
}

android {
compileSdkVersion 30
buildToolsVersion "30.0.0"

defaultConfig {
applicationId "com.example.coroutineslesson"
minSdkVersion 24
targetSdkVersion 30
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
buildFeatures {
dataBinding true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
}
}

dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.21"
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation 'androidx.room:room-runtime:2.3.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
// ViewModel
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1")
// LiveData
implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.3.1")
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2'
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0")
implementation 'androidx.room:room-ktx:2.3.0'
kapt 'androidx.room:room-compiler:2.3.0'
}
21 changes: 21 additions & 0 deletions Notes App/Notes App/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.notesapp

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.example.notesapp", appContext.packageName)
}
}
29 changes: 29 additions & 0 deletions Notes App/Notes App/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notesapp">

<application
android:allowBackup="true"
android:icon="@drawable/notes"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.NotesApp">
<activity
android:name=".SlashScreenActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
</activity>

<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.notesapp

/**
* Used as a wrapper for data that is exposed via a LiveData that represents an event.
*/
open class Event<out T>(private val content: T) {

var hasBeenHandled = false
private set // Allow external read but not write

/**
* Returns the content and prevents its use again.
*/
fun getContentIfNotHandled(): T? {
return if (hasBeenHandled) {
null
} else {
hasBeenHandled = true
content
}
}

/**
* Returns the content, even if it's already been handled.
*/
fun peekContent(): T = content
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.example.notesapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.notesapp.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
lateinit var notesViewModel: NotesViewModel
lateinit var adapter: NotesRecyclerViewAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this,R.layout.activity_main)
val notesDAO = NotesDatabase.getInstance(this).notesDAO
val repository = NotesRepository(notesDAO)
val factory = NotesViewModelFactory(repository)
notesViewModel = ViewModelProvider(this,factory)[NotesViewModel :: class.java]
binding.viewModel = notesViewModel
binding.lifecycleOwner = this
recyclerViewInit()
notesViewModel.message.observe(this, Observer {
it.getContentIfNotHandled()?.let {
Toast.makeText(this,it,Toast.LENGTH_SHORT).show()
}
})
}
private fun recyclerViewInit() {
binding.recyclerView.layoutManager = LinearLayoutManager(this)
adapter = NotesRecyclerViewAdapter(this,
{selectedItem : NotesEntity -> listItemClicked(selectedItem)})
binding.recyclerView.adapter = adapter
displayNotesList()

}
private fun displayNotesList() {
notesViewModel.notes.observe(this, Observer {
Log.i("MyTag",it.toString() )
adapter.setList(it)
adapter.notifyDataSetChanged()
})
}
private fun listItemClicked(notesEntity: NotesEntity) {
// Toast.makeText(this,"Selected name is ${notesEntity.name}",Toast.LENGTH_SHORT).show()
notesViewModel.initUpdateOrDelete(notesEntity)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.notesapp

import androidx.lifecycle.LiveData
import androidx.room.*

@Dao
interface NotesDAO {

@Insert
suspend fun insertNotesDAO(notesEntity : NotesEntity) : Long

@Update
suspend fun updateNotesDAO(notesEntity : NotesEntity) :Int

@Delete
suspend fun deleteNotesDAO(notesEntity : NotesEntity):Int

@Query("DELETE FROM NOTES")
suspend fun deleteAll(): Int

@Query("SELECT * FROM notes")
fun getAllNotes() : LiveData<List<NotesEntity>>

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.notesapp

import android.content.Context
import androidx.room.*

@Database(entities = [NotesEntity::class],version = 2)
abstract class NotesDatabase : RoomDatabase() {

abstract val notesDAO : NotesDAO

companion object {
@Volatile
private var notesDatabaseInstance : NotesDatabase? = null
fun getInstance(context: Context) : NotesDatabase {
if (notesDatabaseInstance == null) {
synchronized(this) {
notesDatabaseInstance = Room.databaseBuilder(
context,
NotesDatabase :: class.java,
"notesDatabase").build()
}
}
return notesDatabaseInstance!!
}


}
}
Loading

0 comments on commit a512572

Please sign in to comment.