Skip to content

Commit

Permalink
Uploading app
Browse files Browse the repository at this point in the history
  • Loading branch information
KetanBhangale committed Jul 6, 2021
0 parents commit a3a67ae
Show file tree
Hide file tree
Showing 43 changed files with 969 additions and 0 deletions.
49 changes: 49 additions & 0 deletions .gitignore
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# ContactList-RoomDB-TypeConverters
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
61 changes: 61 additions & 0 deletions 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'
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'
}
21 changes: 21 additions & 0 deletions 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.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)
}
}
22 changes: 22 additions & 0 deletions app/src/main/AndroidManifest.xml
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>
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 app/src/main/java/com/example/roomdatabaseexample/MainActivity.kt
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()

}
}
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)

}
Loading

0 comments on commit a3a67ae

Please sign in to comment.