-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d236b64
commit a512572
Showing
53 changed files
with
1,272 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
24 changes: 24 additions & 0 deletions
24
Notes App/Notes App/app/src/androidTest/java/com/example/notesapp/ExampleInstrumentedTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
27 changes: 27 additions & 0 deletions
27
Notes App/Notes App/app/src/main/java/com/example/notesapp/Event.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
52 changes: 52 additions & 0 deletions
52
Notes App/Notes App/app/src/main/java/com/example/notesapp/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
Notes App/Notes App/app/src/main/java/com/example/notesapp/NotesDAO.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>> | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
Notes App/Notes App/app/src/main/java/com/example/notesapp/NotesDatabase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!! | ||
} | ||
|
||
|
||
} | ||
} |
Oops, something went wrong.