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
1 change: 1 addition & 0 deletions .idea/gradle.xml

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

7 changes: 7 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,11 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'


def room_version = "2.2.5"

implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor

}
38 changes: 38 additions & 0 deletions app/src/main/java/cat/cattutorial/Contact.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cat.cattutorial;

import androidx.annotation.NonNull;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;

@Entity(tableName = "contacts")
public class Contact {
@ColumnInfo(name = "contact_name")
private String name;

@PrimaryKey
@NonNull
@ColumnInfo(name = "contact_number")
private String number;

public Contact(String name, String number) {
this.name = name;
this.number = number;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getNumber() {
return number;
}

public void setNumber(String number) {
this.number = number;
}
}
56 changes: 56 additions & 0 deletions app/src/main/java/cat/cattutorial/ContactAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cat.cattutorial;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

public class ContactAdapter extends ListAdapter<Contact, ContactAdapter.ContactViewHolder> {


protected ContactAdapter() {
super(new DiffCallback());
}

@NonNull
@Override
public ContactViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact, parent, false);
return new ContactViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ContactViewHolder holder, int position) {
holder.tvName.setText(getItem(position).getName());
}

class ContactViewHolder extends RecyclerView.ViewHolder {
TextView tvName;
ImageView imgDelete;

public ContactViewHolder(@NonNull View itemView) {
super(itemView);
tvName = itemView.findViewById(R.id.tv_name);
imgDelete = itemView.findViewById(R.id.img_delete);
}
}

static class DiffCallback extends DiffUtil.ItemCallback<Contact> {

@Override
public boolean areItemsTheSame(@NonNull Contact oldItem, @NonNull Contact newItem) {
return oldItem.getName().equals(newItem.getName());
}

@Override
public boolean areContentsTheSame(@NonNull Contact oldItem, @NonNull Contact newItem) {
return oldItem.getNumber().equals(newItem.getNumber()) && oldItem.getName().equals(newItem.getName());
}
}
}
37 changes: 37 additions & 0 deletions app/src/main/java/cat/cattutorial/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,51 @@
package cat.cattutorial;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import cat.cattutorial.db.AppDatabase;


public class MainActivity extends AppCompatActivity {

RecyclerView recyclerView;
ContactAdapter adapter = new ContactAdapter();
ArrayList<Contact> contacts = new ArrayList<>();
FloatingActionButton fab;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final AppDatabase db = AppDatabase.getInstance(this);
List<Contact> dbContacts = db.contactDao().getContacts();

adapter.submitList(dbContacts);

recyclerView = findViewById(R.id.rv);
recyclerView.setAdapter(adapter);

fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db.contactDao().insertContact(new Contact("Mohamed" + new Random().nextFloat(), new Random().nextInt()+""));
List<Contact> dbContacts = db.contactDao().getContacts();
adapter.submitList(dbContacts);
}
});

}

}
32 changes: 32 additions & 0 deletions app/src/main/java/cat/cattutorial/db/AppDatabase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cat.cattutorial.db;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import cat.cattutorial.Contact;


@Database(entities = {Contact.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {

private static volatile AppDatabase INSTANCE;

public abstract ContactDao contactDao();

// Home Activity -> getInstance if (INSTNACE == null => True ) if (INSTANCE == null -> TRUE) -> intsance = ....
// Main Activity -> getInstance if (INSTNACE == null => True ) ................................................ if( INSTANCE == null -> False)
public static AppDatabase getInstance(Context context) {
if (INSTANCE == null) {
synchronized (AppDatabase.class) {
if (INSTANCE == null)
INSTANCE = Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "app.db")
.allowMainThreadQueries()
.build();
}
}
return INSTANCE;
}
}
23 changes: 23 additions & 0 deletions app/src/main/java/cat/cattutorial/db/ContactDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cat.cattutorial.db;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;

import java.util.List;

import cat.cattutorial.Contact;

@Dao
public interface ContactDao {
@Query("SELECT * FROM contacts")
List<Contact> getContacts();

@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertContact(Contact contact);

@Delete
void delete(Contact contact);
}
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_add.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_delete.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2V7H6v12zM19,4h-3.5l-1,-1h-5l-1,1H5v2h14V4z"/>
</vector>
Binary file added app/src/main/res/drawable/img_hamdy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 14 additions & 5 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:itemCount="3"
tools:listitem="@layout/item_contact"/>


<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_margin="16dp"
android:src="@drawable/ic_add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
49 changes: 49 additions & 0 deletions app/src/main/res/layout/item_contact.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content"
android:layout_marginTop="16dp"
android:background="?attr/selectableItemBackground"
android:elevation="3dp">

<ImageView
android:id="@+id/img_contact"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:src="@drawable/img_hamdy" />

<TextView
android:id="@+id/tv_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:fontFamily="monospace"
app:layout_constraintEnd_toStartOf="@id/img_delete"
android:maxLines="2"
android:ellipsize="end"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@id/img_contact"
app:layout_constraintStart_toEndOf="@id/img_contact"
app:layout_constraintTop_toTopOf="@id/img_contact"
tools:text="Mohamed Hamdy asjdkahsdjsadhjskadasjkdh" />

<ImageView
android:id="@+id/img_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:background="?attr/selectableItemBackground"
android:src="@drawable/ic_delete"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>