Skip to content

Commit

Permalink
Implemented local database (chat list table with associated CRUD oper…
Browse files Browse the repository at this point in the history
…ations) and additon and deletion of chats from chat list
  • Loading branch information
Tiberiu Iustin Zulean committed Nov 13, 2019
1 parent f64260e commit 399c0b1
Show file tree
Hide file tree
Showing 11 changed files with 310 additions and 66 deletions.
13 changes: 12 additions & 1 deletion Android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,20 @@ dependencies {
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.volley:volley:1.1.0'
implementation 'org.java-websocket:Java-WebSocket:1.3.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

implementation "android.arch.persistence.room:runtime:$roomVersion"
annotationProcessor "android.arch.persistence.room:compiler:$roomVersion"

// RxJava support for Room
implementation "android.arch.persistence.room:rxjava2:$roomVersion"

// Guava support for Room, including Optional and ListenableFuture
implementation "android.arch.persistence.room:guava:$roomVersion"

// Test helpers
testImplementation "android.arch.persistence.room:testing:$roomVersion"
}
9 changes: 9 additions & 0 deletions Android/app/src/main/java/database/AppDatabase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package database;

import android.arch.persistence.room.Database;
import android.arch.persistence.room.RoomDatabase;

@Database(entities = {StoredChatList.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
public abstract StoredChatListDao storedChatListDao();
}
30 changes: 30 additions & 0 deletions Android/app/src/main/java/database/AppDatabaseClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package database;

import android.arch.persistence.room.Room;
import android.content.Context;

public class AppDatabaseClient {

private Context mCtx;
private static AppDatabaseClient mInstance;

private AppDatabase appDatabase;

private AppDatabaseClient(Context mCtx) {

this.mCtx = mCtx;

appDatabase = Room.databaseBuilder(mCtx, AppDatabase.class, "MyToDos").build();
}

public static synchronized AppDatabaseClient getInstance(Context mCtx) {
if (mInstance == null) {
mInstance = new AppDatabaseClient(mCtx);
}
return mInstance;
}

public AppDatabase getAppDatabase() {
return appDatabase;
}
}
62 changes: 62 additions & 0 deletions Android/app/src/main/java/database/StoredChatList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package database;

import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;

import java.io.Serializable;

@Entity
public class StoredChatList implements Serializable {

@PrimaryKey(autoGenerate = true)
private int id;

@ColumnInfo(name = "username")
private String username;

@ColumnInfo(name = "last_message_content")
private String lastMessageContent;

@ColumnInfo(name = "last_message_date")
private String lastMessageDate;

/* *
*
* Getters and Setters
*
* */

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getLastMessageContent() {
return lastMessageContent;
}

public void setLastMessageContent(String lastMessageContent) {
this.lastMessageContent = lastMessageContent;
}

public String getLastMessageDate() {
return lastMessageDate;
}

public void setLastMessageDate(String messageDate) {
this.lastMessageDate = lastMessageDate;
}

}
31 changes: 31 additions & 0 deletions Android/app/src/main/java/database/StoredChatListDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package database;

import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Delete;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.Query;
import android.arch.persistence.room.Update;

import java.util.List;

// Interface for the CRUD operations on StoredChatList table

@Dao
public interface StoredChatListDao {

@Query("SELECT * FROM storedchatlist")
List<StoredChatList> getAll();

@Query("DELETE FROM storedchatlist")
void deleteAll();

@Insert
void insert(StoredChatList storedChatList);

@Delete
void delete(StoredChatList storedChatList);

@Update
void update(StoredChatList storedChatList);

}
153 changes: 96 additions & 57 deletions Android/app/src/main/java/teamzero/chat/mobile/ChatList.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

import android.content.DialogInterface;
import android.content.Intent;

import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;
import android.os.AsyncTask;

import android.view.Menu;
import android.view.MenuInflater;
Expand All @@ -24,19 +26,11 @@
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import database.AppDatabaseClient;
import database.StoredChatList;

/*
TODO: 1) Make mock login with welcome message to test out UserDetails population [X]
Expand All @@ -51,8 +45,7 @@ public class ChatList extends AppCompatActivity {
TextView noChatsFoundTextDisplay;
FloatingActionButton newChatBtn;

ArrayList<HashMap<String, String>> chatList = new ArrayList<>();
int totalUsers = 0;
List<StoredChatList> storedChatList = new ArrayList<>();
ProgressDialog pd;

@Override
Expand All @@ -71,30 +64,14 @@ protected void onCreate(Bundle savedInstanceState) {
Snackbar.make(findViewById(R.id.usersList), "Welcome back " + UserDetails.username, Snackbar.LENGTH_SHORT)
.setAction("Action", null).show();

// TODO: Change URL to access JSON content from our server
String url = "https://api.myjson.com/bins/lj6f8";

StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
doOnSuccess(s);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
System.out.println("" + volleyError);
}
});

RequestQueue rQueue = Volley.newRequestQueue(ChatList.this);
rQueue.add(request);
getStoredChatList();

// When the user clicks on a specific chat from his history, go to that conversation
usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

UserDetails.chatWith = chatList.get(position).get("username");
UserDetails.chatWith = storedChatList.get(position).getUsername();

// TODO: Start Activity --> Goto chat page with X person
startActivity(new Intent(ChatList.this, Chat.class));
Expand All @@ -110,33 +87,36 @@ public void onClick(View view) {
});
}

public void doOnSuccess(String s) {

try {
JSONObject obj = new JSONObject(s);

Iterator i = obj.keys();
String key = "";
private void getStoredChatList() {
class GetStoredChatList extends AsyncTask<Void, Void, List<StoredChatList>> {

HashMap<String, String> specificUserDetails;

while(i.hasNext()){
specificUserDetails = new HashMap<>();
key = i.next().toString();
// Populate the hashmap representing chat list details to a specific chat from the list
specificUserDetails.put("username", key);
specificUserDetails.put("last_message", obj.getJSONObject(key).getString("last_message"));
// Add a chat from the list to the final array
chatList.add(specificUserDetails);
totalUsers++;
@Override
protected List<StoredChatList> doInBackground(Void... voids) {
// SELECT * FROM storedchatlist
List<StoredChatList> scl = AppDatabaseClient
.getInstance(getApplicationContext())
.getAppDatabase()
.storedChatListDao()
.getAll();
return scl;
}

} catch (JSONException e) {
e.printStackTrace();
@Override
protected void onPostExecute(List<StoredChatList> scl) {
super.onPostExecute(scl);
// Get the chat list and store it on main thread
storedChatList = scl;
displayChatList();
}
}

// If there are no chats with other users found in history, display a suggestive text
if(totalUsers < 1) {
GetStoredChatList gt = new GetStoredChatList();
gt.execute();
}

public void displayChatList() {

if(storedChatList.size() == 0) {
noChatsFoundTextDisplay.setVisibility(View.VISIBLE);
usersList.setVisibility(View.GONE);
}
Expand All @@ -146,22 +126,24 @@ public void doOnSuccess(String s) {
usersList.setVisibility(View.VISIBLE);

// Overridden the getView of ArrayAdapter in order to access both text1 and text2 (from simple_list_item_2) and write on them right away
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_2, android.R.id.text1, chatList) {
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_2, android.R.id.text1, storedChatList) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text1 = (TextView) view.findViewById(android.R.id.text1);
TextView text2 = (TextView) view.findViewById(android.R.id.text2);

text1.setText(chatList.get(position).get("username"));
text2.setText(chatList.get(position).get("last_message"));
text1.setText(storedChatList.get(position).getUsername());
text2.setText(storedChatList.get(position).getLastMessageContent());
return view;
}
};
usersList.setAdapter(adapter);
}

// Close the progress dialog when action is finished
pd.dismiss();

}

// TODO: Make searching through chats functional
Expand Down Expand Up @@ -196,6 +178,14 @@ public boolean onOptionsItemSelected(MenuItem item) {
signOutOrUnregister("Unregister");
return true;

case R.id.profile_picture:
// TODO: Choose profile picture -- Goto profile page
return true;

case R.id.delete_all_chats:
deleteAllChatsPrompt();
return true;

default:
return super.onOptionsItemSelected(item);
}
Expand Down Expand Up @@ -235,4 +225,53 @@ public void onClick(DialogInterface dialog, int id) {
dialog.show();
}

public void deleteAllChatsPrompt() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setPositiveButton("DELETE", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked DELETE button
deleteAllChats();
}
});

builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog, go back
}
});

// Create the AlertDialog
AlertDialog dialog = builder.create();
dialog.setTitle("Delete all chats");
dialog.setMessage("Are you sure you want to delete all the chats?");
dialog.show();
}

private void deleteAllChats() {
class DeleteAllChats extends AsyncTask<Void, Void, List<StoredChatList>> {

@Override
protected List<StoredChatList> doInBackground(Void... voids) {
// DELETE FROM storedchatlist
AppDatabaseClient
.getInstance(getApplicationContext())
.getAppDatabase()
.storedChatListDao()
.deleteAll();
return null;
}

@Override
protected void onPostExecute(List<StoredChatList> scl) {
super.onPostExecute(scl);
// Refresh
startActivity(new Intent(ChatList.this, ChatList.class));
}
}

DeleteAllChats gt = new DeleteAllChats();
gt.execute();
}

}
Loading

0 comments on commit 399c0b1

Please sign in to comment.