Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .idea/deploymentTargetDropDown.xml

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

10 changes: 10 additions & 0 deletions .idea/misc.xml

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

11 changes: 11 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
}

android {
Expand Down Expand Up @@ -54,5 +55,15 @@ dependencies {
implementation 'com.google.android.gms:play-services-auth:20.3.0'
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4")
dependencies {
def room_version = "2.4.3"

implementation("androidx.room:room-runtime:$room_version")
annotationProcessor("androidx.room:room-compiler:$room_version")
kapt("androidx.room:room-compiler:$room_version")
implementation("androidx.room:room-ktx:$room_version")
}

}
7 changes: 4 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
android:theme="@style/Theme.BasicApplication"
android:usesCleartextTraffic="true"
tools:targetApi="31">
<activity
android:name=".AddPeopleInfoActivity"
android:exported="false" />
<activity
android:name=".SplashScreenActivity"
android:exported="true"
android:theme="@style/SplashTheme" >
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand All @@ -30,8 +33,6 @@
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name">


<meta-data
android:name="android.app.lib_name"
android:value="" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.neilkrishna.basicapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.room.Room
import com.neilkrishna.basicapplication.databinding.ActivityAddPeopleInfoBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

class AddPeopleInfoActivity : AppCompatActivity() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plz implement the view model for insertion into the database and make use of View model coroutine scope instead of global scope. Also include a circular progress bar which will be displayed while the entry is inserted into the database.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you elaborate about circular progress bar? i didn't get it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

U display the progress bar before u start the transaction to database and hide it as u complete the transaction

lateinit var database: PersonDatabase
private lateinit var binding: ActivityAddPeopleInfoBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityAddPeopleInfoBinding.inflate(layoutInflater)
setContentView(binding.root)
database=PersonDatabase.getDatabase(this)
binding.btnAddPersonInfo.setOnClickListener{

addPerson()}

}

private fun addPerson() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put in error messages if any fields are missing.

val firstName = binding.etFirstName.text.toString()
val lastName = binding.etLastName.text.toString()
val age = binding.etAge.text.toString()
val gender = binding.etGender.text.toString()
val email = binding.etEmail.text.toString()

if(firstName.isNotEmpty() && lastName.isNotEmpty() && age.isNotEmpty() && gender.isNotEmpty() && email.isNotEmpty() ) {
val person = Person(0, firstName, lastName, age.toInt(), gender, email)

GlobalScope.launch(Dispatchers.IO)
{
database.personDao().insertPerson(person)
}

binding.etAge.text.clear()
binding.etGender.text.clear()
binding.etEmail.text.clear()
binding.etFirstName.text.clear()
binding.etLastName.text.clear()
Toast.makeText(this@AddPeopleInfoActivity,"Successfully written",Toast.LENGTH_SHORT).show()
}else Toast.makeText(this@AddPeopleInfoActivity,"PLease Enter Data",Toast.LENGTH_SHORT).show()

}


}
15 changes: 15 additions & 0 deletions app/src/main/java/com/neilkrishna/basicapplication/Person.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.neilkrishna.basicapplication

import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "person")
data class Person(
@PrimaryKey(autoGenerate = true)
val id : Long,
val firstname : String,
val lastname : String,
val age : Int,
val gender : String,
val email : String
)
17 changes: 17 additions & 0 deletions app/src/main/java/com/neilkrishna/basicapplication/PersonDAO.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.neilkrishna.basicapplication

import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query

@Dao
interface PersonDAO {
@Insert
fun insertPerson(person : Person)

@Query("SELECT * FROM person")
fun getPerson() : LiveData<List<Person>>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here u can use Flow and get it as Live data in your View model



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.neilkrishna.basicapplication

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

@Database(entities = [Person::class], version = 1)

abstract class PersonDatabase : RoomDatabase() {

abstract fun personDao() : PersonDAO

companion object{

@Volatile
private var INSTANCE : PersonDatabase? = null

fun getDatabase(context:Context):PersonDatabase{
val tempInstance = INSTANCE
if(tempInstance != null){
return tempInstance
}
synchronized(this){
val instance = Room.databaseBuilder(
context.applicationContext,
PersonDatabase::class.java,
"PersonINFO"
).build()
INSTANCE = instance
return instance
}
}

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.neilkrishna.basicapplication.ui.home

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand All @@ -9,6 +10,7 @@ import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.google.android.material.snackbar.Snackbar
import com.neilkrishna.basicapplication.AddPeopleInfoActivity
import com.neilkrishna.basicapplication.databinding.FragmentHomeBinding

class HomeFragment : Fragment() {
Expand Down Expand Up @@ -44,9 +46,9 @@ class HomeFragment : Fragment() {
viewModel.increaseCountByOne()
}

binding.addPeople.setOnClickListener { view2 ->
Snackbar.make(view2, "Replace with your own action to add person", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
binding.addPeople.setOnClickListener {
val intent = Intent( this@HomeFragment.requireContext(), AddPeopleInfoActivity::class.java )
startActivity(intent)
}
}

Expand Down
74 changes: 74 additions & 0 deletions app/src/main/res/layout/activity_add_people_info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AddPeopleInfoActivity"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center"
android:layout_margin="30dp">



<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:fontFamily="serif"
android:text="Person's Data"
android:textColor="@color/white"
android:textSize="30dp"
android:layout_marginBottom="30dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refer to Material 3 implementation of Edit text.
https://m3.material.io/components/text-fields/overview

also put in input type for the type of input ur expecting.

android:id="@+id/et_first_name"
android:hint= " First Name"
android:layout_marginBottom="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/et_last_name"
android:hint= " Last Name"
android:layout_marginBottom="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"

/>
<EditText
android:id="@+id/et_age"
android:hint= " Age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
/>
<EditText
android:id="@+id/et_gender"
android:hint= " Gender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
/>
<EditText
android:id="@+id/et_email"
android:hint= " Email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
/>

<Button
android:id="@+id/btn_add_person_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text = "Add Person Info"
/>

</androidx.appcompat.widget.LinearLayoutCompat>