-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit a3a67ae
Showing
43 changed files
with
969 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,49 @@ | ||
# Built application files | ||
*.apk | ||
*.ap_ | ||
|
||
# Files for the ART/Dalvik VM | ||
*.dex | ||
|
||
# Java class files | ||
*.class | ||
|
||
# Generated files | ||
bin/ | ||
gen/ | ||
out/ | ||
|
||
# Gradle files | ||
.gradle/ | ||
gradlew.bat | ||
gradlew | ||
build/ | ||
gradle.properties | ||
|
||
# Local configuration file (sdk path, etc) | ||
local.properties | ||
|
||
# Proguard folder generated by Eclipse | ||
proguard/ | ||
|
||
# Log Files | ||
*.log | ||
|
||
# Android Studio Navigation editor temp files | ||
.navigation/ | ||
|
||
# Android Studio captures folder | ||
captures/ | ||
|
||
# Intellij | ||
*.iml | ||
*.iws | ||
.idea | ||
.idea/tasks.xml | ||
.idea/workspace.xml | ||
|
||
# OS | ||
.DS_Store | ||
|
||
# Keystore files | ||
*.jks |
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 @@ | ||
# ContactList-RoomDB-TypeConverters |
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' | ||
id 'kotlin-android-extensions' | ||
} | ||
apply plugin: 'kotlin-kapt' | ||
|
||
|
||
android { | ||
compileSdkVersion 30 | ||
buildToolsVersion "30.0.3" | ||
|
||
defaultConfig { | ||
applicationId "com.example.roomdatabaseexample" | ||
minSdkVersion 16 | ||
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' | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
kotlinOptions { | ||
jvmTarget = '1.8' | ||
} | ||
} | ||
|
||
dependencies { | ||
def room_version = "2.3.0" | ||
|
||
implementation("androidx.room:room-runtime:$room_version") | ||
annotationProcessor "androidx.room:room-compiler:$room_version" | ||
|
||
// To use Kotlin annotation processing tool (kapt) | ||
kapt("androidx.room:room-compiler:$room_version") | ||
|
||
// optional - Kotlin Extensions and Coroutines support for Room | ||
implementation("androidx.room:room-ktx:$room_version") | ||
|
||
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9" | ||
|
||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" | ||
implementation 'androidx.core:core-ktx:1.5.0' | ||
implementation 'androidx.appcompat:appcompat:1.3.0' | ||
implementation 'com.google.android.material:material:1.3.0' | ||
implementation 'androidx.constraintlayout:constraintlayout:2.0.4' | ||
testImplementation 'junit:junit:4.+' | ||
androidTestImplementation 'androidx.test.ext:junit:1.1.2' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.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
app/src/androidTest/java/com/example/roomdatabaseexample/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.roomdatabaseexample | ||
|
||
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.roomdatabaseexample", 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,22 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.example.roomdatabaseexample"> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/Theme.RoomDatabaseExample"> | ||
<activity android:name=".AddRecordActivity"></activity> | ||
<activity android:name=".MainActivity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
51 changes: 51 additions & 0 deletions
51
app/src/main/java/com/example/roomdatabaseexample/AddRecordActivity.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,51 @@ | ||
package com.example.roomdatabaseexample | ||
|
||
import androidx.appcompat.app.AppCompatActivity | ||
import android.os.Bundle | ||
import android.widget.Toast | ||
import com.example.roomdatabaseexample.database.ConactDatabase | ||
import com.example.roomdatabaseexample.model.Contact | ||
import kotlinx.android.synthetic.main.activity_add_record.* | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import java.util.* | ||
|
||
class AddRecordActivity : AppCompatActivity() { | ||
lateinit var database: ConactDatabase | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_add_record) | ||
|
||
database = ConactDatabase.getDatabaseInstance(this) | ||
|
||
|
||
submit.setOnClickListener { | ||
if(validateInput()){ | ||
addContact(inputName.text.toString(), inputMobile.text.toString(), inputGender.text.toString()) | ||
}else{ | ||
Toast.makeText( this@AddRecordActivity,"Please enter proper details", Toast.LENGTH_SHORT).show() | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
private fun validateInput():Boolean { | ||
//basic validation only | ||
return !(inputName.text.toString() == "" || inputGender.text.toString() == "" || inputMobile.text.toString() == "") | ||
} | ||
|
||
private fun addContact(name: String, mobile: String, gender: String){ | ||
GlobalScope.launch(Dispatchers.IO) { | ||
database.getContactDao().insertContact(Contact(0, name, mobile, Date(), gender )) | ||
|
||
withContext(Dispatchers.Main){ | ||
finish() | ||
} | ||
} | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
app/src/main/java/com/example/roomdatabaseexample/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,67 @@ | ||
package com.example.roomdatabaseexample | ||
|
||
|
||
import android.content.Intent | ||
import android.os.Bundle | ||
import androidx.appcompat.app.AlertDialog | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import com.example.roomdatabaseexample.adapter.ContactsAdapter | ||
import com.example.roomdatabaseexample.adapter.IContactAdapter | ||
import com.example.roomdatabaseexample.database.ConactDatabase | ||
import com.example.roomdatabaseexample.model.Contact | ||
|
||
import kotlinx.android.synthetic.main.activity_main.* | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.launch | ||
import java.util.* | ||
|
||
class MainActivity : AppCompatActivity(), IContactAdapter { | ||
lateinit var database: ConactDatabase | ||
lateinit var contactsAdapter: ContactsAdapter | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_main) | ||
database = ConactDatabase.getDatabaseInstance(this) | ||
setRV() | ||
|
||
database.getContactDao().getContacts() | ||
.observe(this, androidx.lifecycle.Observer { response -> | ||
contactsAdapter.contactList = response | ||
|
||
}) | ||
fab.setOnClickListener { | ||
val intent = Intent(this@MainActivity, AddRecordActivity::class.java) | ||
startActivity(intent) | ||
} | ||
|
||
|
||
} | ||
|
||
private fun setRV() { | ||
contactsAdapter = ContactsAdapter(this) | ||
recyclerView.apply { | ||
layoutManager = LinearLayoutManager(this@MainActivity) | ||
setHasFixedSize(true) | ||
adapter = contactsAdapter | ||
} | ||
} | ||
|
||
override fun onItemClicked(contact: Contact) { | ||
val builder = AlertDialog.Builder(this@MainActivity) | ||
.setTitle("Confirm Dialog") | ||
.setMessage("Do you really want to delete the selected contact?") | ||
.setCancelable(false) | ||
.setPositiveButton("Delete") { dialogInterface, which -> | ||
GlobalScope.launch { | ||
database.getContactDao().deleteContact(contact) | ||
} | ||
} | ||
.setNegativeButton("Cancel") { dialogInterface, which -> | ||
|
||
} | ||
val alertDialog: AlertDialog = builder.create() | ||
alertDialog.show() | ||
|
||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
app/src/main/java/com/example/roomdatabaseexample/adapter/ContactsAdapter.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,76 @@ | ||
package com.example.roomdatabaseexample.adapter | ||
|
||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.ImageView | ||
import android.widget.TextView | ||
import androidx.recyclerview.widget.AsyncListDiffer | ||
import androidx.recyclerview.widget.DiffUtil | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.example.roomdatabaseexample.model.Contact | ||
import com.example.roomdatabaseexample.R | ||
import java.text.SimpleDateFormat | ||
import java.util.* | ||
|
||
class ContactsAdapter(private val listener:IContactAdapter) : RecyclerView.Adapter<ContactsAdapter.ContactViewHolder>() { | ||
|
||
class ContactViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | ||
val name = itemView.findViewById<TextView>(R.id.name) | ||
val gender = itemView.findViewById<TextView>(R.id.gender) | ||
val mobile = itemView.findViewById<TextView>(R.id.mobile) | ||
val date = itemView.findViewById<TextView>(R.id.date) | ||
val delete = itemView.findViewById<ImageView>(R.id.delete) | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ContactViewHolder { | ||
val contactViewHolder = ContactViewHolder( | ||
LayoutInflater.from(parent.context).inflate(R.layout.row, parent, false) | ||
) | ||
contactViewHolder.delete.setOnClickListener { | ||
listener.onItemClicked(contactList[contactViewHolder.adapterPosition]) | ||
} | ||
return contactViewHolder | ||
} | ||
|
||
override fun onBindViewHolder(holder: ContactViewHolder, position: Int) { | ||
val contact = contactList[position] | ||
holder.name.text = contact.name | ||
holder.gender.text = "Gender: "+ contact.gender | ||
holder.mobile.text = contact.mobile | ||
holder.date.text = formatDate(contact.date) | ||
|
||
} | ||
|
||
override fun getItemCount() = contactList.size | ||
|
||
|
||
private fun formatDate(date: Date): String { | ||
return SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(date) | ||
} | ||
|
||
//updating List | ||
private val differCallback = object : DiffUtil.ItemCallback<Contact>() { | ||
override fun areItemsTheSame(oldItem: Contact, newItem: Contact): Boolean { | ||
return oldItem == newItem | ||
} | ||
|
||
override fun areContentsTheSame(oldItem: Contact, newItem: Contact): Boolean { | ||
return oldItem.name == newItem.name | ||
} | ||
} | ||
|
||
private val differ = AsyncListDiffer(this, differCallback) | ||
var contactList: List<Contact> | ||
get() = differ.currentList | ||
set(value) { | ||
differ.submitList(value) | ||
} | ||
|
||
|
||
} | ||
|
||
interface IContactAdapter { | ||
fun onItemClicked(contact: Contact) | ||
|
||
} |
Oops, something went wrong.