Created
October 18, 2017 21:04
-
-
Save SayChunKim/798cd7c5372ea493f8005636857ac2f7 to your computer and use it in GitHub Desktop.
Firebase Realtime Database Workshop
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
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout | |
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" | |
android:paddingBottom="16dp" | |
android:paddingTop="16dp" | |
android:paddingLeft="16dp" | |
android:paddingRight="16dp" | |
tools:context="com.sckim.fbtodoapp.MainActivity"> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/task_list" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_alignParentTop="true" | |
android:layout_above="@+id/add_task_wrapper" | |
android:layout_marginBottom="12dp" | |
android:orientation="vertical" | |
android:scrollbars="none" | |
/> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
android:id="@+id/add_task_wrapper" | |
android:layout_alignParentBottom="true" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentStart="true"> | |
<EditText | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="7" | |
android:inputType="text" | |
android:id="@+id/add_task_box" | |
/> | |
<Button | |
android:id="@+id/add_task_button" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:text="@string/add_task_button" | |
android:layout_weight="3"/> | |
</LinearLayout> | |
</RelativeLayout> |
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
package com.sckim.fbtodoapp; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.text.TextUtils; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
import com.google.firebase.database.ChildEventListener; | |
import com.google.firebase.database.DataSnapshot; | |
import com.google.firebase.database.DatabaseError; | |
import com.google.firebase.database.DatabaseReference; | |
import com.google.firebase.database.FirebaseDatabase; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class MainActivity extends AppCompatActivity { | |
private static final String TAG = MainActivity.class.getSimpleName(); | |
private RecyclerView recyclerView; | |
private LinearLayoutManager linearLayoutManager; | |
private RecyclerViewAdapter recyclerViewAdapter; | |
private EditText addTaskBox; | |
private List<Task> allTask; | |
private DatabaseReference databaseReference; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
databaseReference = FirebaseDatabase.getInstance().getReference(); | |
allTask = new ArrayList<Task>(); | |
recyclerView = (RecyclerView)findViewById(R.id.task_list); | |
linearLayoutManager = new LinearLayoutManager(getApplicationContext()); | |
recyclerView.setLayoutManager(linearLayoutManager); | |
addTaskBox = (EditText)findViewById(R.id.add_task_box); | |
Button addTaskButton = (Button)findViewById(R.id.add_task_button); | |
addTaskButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
String enteredTask = addTaskBox.getText().toString(); | |
if (TextUtils.isEmpty(enteredTask)){ | |
Toast.makeText(MainActivity.this, "You need input something !", Toast.LENGTH_LONG).show(); | |
return; | |
} | |
if (enteredTask.length() < 6){ | |
Toast.makeText(MainActivity.this, "Task count must be more than 6", Toast.LENGTH_LONG).show(); | |
} | |
Task taskObject = new Task(enteredTask); | |
databaseReference.push().setValue(taskObject); | |
addTaskBox.setText(""); | |
} | |
}); | |
databaseReference.addChildEventListener(new ChildEventListener() { | |
@Override | |
public void onChildAdded(DataSnapshot dataSnapshot, String s) { | |
getAllTask(dataSnapshot); | |
} | |
@Override | |
public void onChildChanged(DataSnapshot dataSnapshot, String s) { | |
getAllTask(dataSnapshot); | |
} | |
@Override | |
public void onChildRemoved(DataSnapshot dataSnapshot) { | |
taskDeletion(dataSnapshot); | |
} | |
@Override | |
public void onChildMoved(DataSnapshot dataSnapshot, String s) { | |
} | |
@Override | |
public void onCancelled(DatabaseError databaseError) { | |
} | |
}); | |
} | |
private void getAllTask(DataSnapshot dataSnapshot){ | |
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){ | |
String taskTitle = singleSnapshot.getValue(String.class); | |
allTask.add(new Task(taskTitle)); | |
recyclerViewAdapter = new RecyclerViewAdapter(MainActivity.this, allTask); | |
recyclerView.setAdapter(recyclerViewAdapter); | |
} | |
} | |
private void taskDeletion(DataSnapshot dataSnapshot){ | |
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){ | |
String taskTitle = singleSnapshot.getValue(String.class); | |
for (int i = 0; i< allTask.size(); i++){ | |
if(allTask.get(i).getTask().equals(taskTitle)){ | |
allTask.remove(i); | |
} | |
} | |
Log.d(TAG,"Task title" + taskTitle); | |
recyclerViewAdapter.notifyDataSetChanged(); | |
recyclerViewAdapter = new RecyclerViewAdapter(MainActivity.this, allTask); | |
recyclerView.setAdapter(recyclerViewAdapter); | |
} | |
} | |
} |
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
package com.sckim.fbtodoapp; | |
import android.content.Context; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import java.util.List; | |
/** | |
* Created by SC Kim on 10/19/2017. | |
*/ | |
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders>{ | |
private List<Task> task; | |
protected Context context; | |
public RecyclerViewAdapter (Context context, List<Task> task){ | |
this.task = task; | |
this.context = context; | |
} | |
@Override | |
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) { | |
RecyclerViewHolders viewHolder = null; | |
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.to_do_list, parent, false); | |
viewHolder = new RecyclerViewHolders(layoutView, task); | |
return viewHolder; | |
} | |
@Override | |
public void onBindViewHolder(RecyclerViewHolders holder, int position) { | |
holder.categoryTitle.setText(task.get(position).getTask()); | |
} | |
@Override | |
public int getItemCount() { | |
return this.task.size(); | |
} | |
} |
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
package com.sckim.fbtodoapp; | |
import android.support.v7.widget.RecyclerView; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import com.google.firebase.database.DataSnapshot; | |
import com.google.firebase.database.DatabaseError; | |
import com.google.firebase.database.DatabaseReference; | |
import com.google.firebase.database.FirebaseDatabase; | |
import com.google.firebase.database.Query; | |
import com.google.firebase.database.ValueEventListener; | |
import java.util.List; | |
/** | |
* Created by SC Kim on 10/19/2017. | |
*/ | |
public class RecyclerViewHolders extends RecyclerView.ViewHolder{ | |
private static final String TAG = RecyclerViewHolders.class.getSimpleName(); | |
public ImageView deleteIcon; | |
public TextView categoryTitle; | |
public List<Task> taskObject; | |
public RecyclerViewHolders(final View itemView, final List<Task> taskObject) { | |
super(itemView); | |
this.taskObject = taskObject; | |
categoryTitle = (TextView)itemView.findViewById(R.id.task_title); | |
deleteIcon = (ImageView)itemView.findViewById(R.id.task_delete); | |
deleteIcon.setOnClickListener(new View.OnClickListener(){ | |
@Override | |
public void onClick(View v) { | |
//What happens when X is clicked. | |
Toast.makeText(v.getContext(), "Delete icon is triggered",Toast.LENGTH_LONG).show(); | |
String taskTitle = taskObject.get(getAdapterPosition()).getTask(); | |
DatabaseReference ref = FirebaseDatabase.getInstance().getReference(); | |
final Query applesQuery = ref.orderByChild("task").equalTo(taskTitle); | |
applesQuery.addListenerForSingleValueEvent(new ValueEventListener() { | |
@Override | |
public void onDataChange(DataSnapshot dataSnapshot) { | |
for (DataSnapshot appleSnapshot: dataSnapshot.getChildren()){ | |
appleSnapshot.getRef().removeValue(); | |
} | |
} | |
@Override | |
public void onCancelled(DatabaseError databaseError) { | |
Log.e(TAG, "onCanceled", databaseError.toException()); | |
} | |
}); | |
} | |
} | |
); | |
} | |
} |
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
package com.sckim.fbtodoapp; | |
/** | |
* Created by SC Kim on 10/19/2017. | |
*/ | |
public class Task { | |
private String task; | |
public Task() {} | |
public Task (String task){ | |
this.task = task; | |
} | |
public String getTask(){ | |
return task; | |
} | |
} |
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
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
android:paddingTop="16dp" | |
android:paddingBottom="16dp"> | |
<TextView | |
android:id="@+id/task_title" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="80" | |
android:layout_marginLeft="16dp" | |
android:text="@string/app_name" | |
android:textSize="16sp" | |
android:textColor="#000000"/> | |
<ImageView | |
android:id="@+id/task_delete" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="20" | |
android:layout_marginRight="16dp" | |
android:contentDescription="@string/app_name" | |
android:src="@drawable/ic_clear_black_24dp" /> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment