Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #11: Added local database #66

Merged
merged 1 commit into from
Oct 1, 2016
Merged
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 app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
apply plugin: 'realm-android'

android {
compileSdkVersion 24
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import android.widget.ImageView;
import android.widget.LinearLayout;

import org.fossasia.susi.ai.MainActivity;
import org.fossasia.susi.ai.activities.MainActivity;
import org.fossasia.susi.ai.R;

/**
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:name=".MainApplication"
android:label="@string/app_name"
android:theme="@style/AppTheme">

<activity
android:name=".MainActivity"
android:name=".activities.MainActivity"
android:label="@string/app_name" />

<activity
android:name=".SplashScreen"
android:name=".activities.SplashScreen"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/org/fossasia/susi/ai/MainApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.fossasia.susi.ai;

import android.app.Application;

import io.realm.Realm;
import io.realm.RealmConfiguration;

/**
* Created by
* --Vatsal Bajpai on
* --30/09/16 at
* --10:26 PM
*/

public class MainApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// The Realm file will be located in Context.getFilesDir() with name "default.realm"
Realm.init(this);
RealmConfiguration config = new RealmConfiguration.Builder().build();
Realm.setDefaultConfiguration(config);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.fossasia.susi.ai;
package org.fossasia.susi.ai.activities;

import android.content.Context;
import android.net.ConnectivityManager;
Expand All @@ -11,6 +11,7 @@
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand All @@ -19,19 +20,21 @@
import android.widget.ImageView;
import android.widget.LinearLayout;

import org.fossasia.susi.ai.R;
import org.fossasia.susi.ai.adapters.recyclerAdapters.ChatFeedRecyclerAdapter;
import org.fossasia.susi.ai.model.ChatMessage;
import org.json.JSONException;
import org.json.JSONObject;
import org.loklak.android.tools.JsonIO;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.realm.Realm;
import io.realm.RealmResults;

public class MainActivity extends AppCompatActivity {

Expand All @@ -49,56 +52,86 @@ public class MainActivity extends AppCompatActivity {
LinearLayout sendMessageLayout;

private ChatFeedRecyclerAdapter recyclerAdapter;
private Realm realm;

public static String TAG = MainActivity.class.getName();
RealmResults<ChatMessage> chatMessageDatabaseList;

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

private void init() {
ButterKnife.bind(this);
realm = Realm.getDefaultInstance();

setupAdapter();
}

private void setupAdapter() {

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);

rvChatFeed.setLayoutManager(linearLayoutManager);
rvChatFeed.setHasFixedSize(true);
List<ChatMessage> chatMessageList = new ArrayList<>();
recyclerAdapter = new ChatFeedRecyclerAdapter(this, this, chatMessageList);

rvChatFeed.setAdapter(recyclerAdapter);
chatMessageDatabaseList = realm.where(ChatMessage.class).findAll();
recyclerAdapter = new ChatFeedRecyclerAdapter(this, this, chatMessageDatabaseList);

rvChatFeed.addOnLayoutChangeListener(new View.OnLayoutChangeListener(){
rvChatFeed.setAdapter(recyclerAdapter);
rvChatFeed.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View view, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
if (bottom < oldBottom) {
rvChatFeed.postDelayed(new Runnable() {
@Override
public void run() {
int scrollTo = rvChatFeed.getAdapter().getItemCount() - 1;
scrollTo = scrollTo>=0 ? scrollTo : 0;
scrollTo = scrollTo >= 0 ? scrollTo : 0;
rvChatFeed.scrollToPosition(scrollTo);
}
}, 10);
}
}
});

}

private void sendMessage(String query) {
ChatMessage chatMessage = new ChatMessage(query, true, false);
updateDatabase(query, true, false);
recyclerAdapter.addMessage(chatMessage, true);
computeOtherMessage(query);
}

private void computeOtherMessage(final String query) {
new asksusi().execute(query);
new AskSusi().execute(query);
}

private void updateDatabase(final String message, final boolean mine, final boolean image) {
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm bgRealm) {
ChatMessage chatMessage = bgRealm.createObject(ChatMessage.class);
chatMessage.setContent(message);
chatMessage.setIsMine(mine);
chatMessage.setIsImage(image);
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
Log.v(TAG, "update successful!");
}
}, new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
Log.e(TAG, error.getMessage());
}
});
}


// private void sendMessage() {
// ChatMessage chatMessage = new ChatMessage(null, true, true);
// recyclerAdapter.addMessage(chatMessage, true);
Expand Down Expand Up @@ -141,7 +174,7 @@ protected void onResume() {
);
}

// TODO Removed OnClick for Image for now
// TODO Removed OnClick for Image for now
// @OnClick({R.id.iv_image, R.id.btn_send})
@OnClick(R.id.btn_send)
public void onClick(View view) {
Expand All @@ -160,7 +193,7 @@ public void onClick(View view) {
}
}

private class asksusi extends AsyncTask<String, Void, String> {
private class AskSusi extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... query) {
String response = null;
Expand All @@ -184,6 +217,7 @@ protected void onPostExecute(String response) {
return;
}
ChatMessage chatMessage = new ChatMessage(response, false, false);
updateDatabase(response, false, false);
recyclerAdapter.addMessage(chatMessage, true);
}

Expand All @@ -192,4 +226,10 @@ private boolean isNetworkConnected() {
return cm.getActiveNetworkInfo() != null;
}
}

@Override
protected void onDestroy() {
super.onDestroy();
realm.close();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.fossasia.susi.ai;
package org.fossasia.susi.ai.activities;

import android.content.Intent;
import android.os.Bundle;
Expand All @@ -8,6 +8,8 @@

import com.bumptech.glide.Glide;

import org.fossasia.susi.ai.R;

import butterknife.BindView;
import butterknife.ButterKnife;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import android.view.ViewGroup;
import android.widget.Toast;

import org.fossasia.susi.ai.ChatMessage;
import org.fossasia.susi.ai.R;
import org.fossasia.susi.ai.adapters.viewHolders.ChatViewHolder;
import org.fossasia.susi.ai.model.ChatMessage;

import java.util.ArrayList;
import java.util.List;
import io.realm.RealmChangeListener;
import io.realm.RealmResults;

/**
* Created by
Expand All @@ -30,12 +30,12 @@ public class ChatFeedRecyclerAdapter extends RecyclerView.Adapter<ChatViewHolder
public static final int OTHER_IMAGE = 3;

private Context currContext;
private List<ChatMessage> itemList;
private RealmResults<ChatMessage> itemList;
private Activity activity;
private String TAG = ChatFeedRecyclerAdapter.class.getSimpleName();
private RecyclerView recyclerView;

public ChatFeedRecyclerAdapter(Activity activity, Context curr_context, List<ChatMessage> itemList) {
public ChatFeedRecyclerAdapter(Activity activity, Context curr_context, RealmResults<ChatMessage> itemList) {
this.itemList = itemList;
this.currContext = curr_context;
this.activity = activity;
Expand All @@ -53,15 +53,19 @@ public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
this.recyclerView = null;
}

public void addMessage(ChatMessage chatMessage, boolean shouldScrollToBottom) {
public void addMessage(final ChatMessage chatMessage, final boolean shouldScrollToBottom) {
if (itemList == null) {
itemList = new ArrayList<>();
}
itemList.add(chatMessage);
notifyItemInserted(itemList.size() - 1);
if (recyclerView != null && shouldScrollToBottom) {
recyclerView.smoothScrollToPosition(itemList.size() - 1);
}
itemList.addChangeListener(new RealmChangeListener<RealmResults<ChatMessage>>() {
@Override
public void onChange(RealmResults<ChatMessage> element) {
notifyItemInserted(itemList.size() - 1);
if (recyclerView != null && shouldScrollToBottom) {
recyclerView.smoothScrollToPosition(itemList.size() - 1);
}
}
});

}

public void addMessage(ChatMessage chatMessage) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package org.fossasia.susi.ai;
package org.fossasia.susi.ai.model;

import io.realm.RealmObject;

/**
* Created by himanshusoni on 06/09/15.
*/
public class ChatMessage {
public class ChatMessage extends RealmObject {
private boolean isImage, isMine;
private String content;

public ChatMessage() {

}

public ChatMessage(String message, boolean mine, boolean image) {
content = message;
isMine = mine;
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
tools:context=".activities.MainActivity">

<android.support.v7.widget.RecyclerView
android:id="@+id/rv_chat_feed"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/splash_screen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
tools:context=".SplashScreen"
tools:context=".activities.SplashScreen"
tools:src="@drawable/susi_image" />
2 changes: 1 addition & 1 deletion app/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<menu 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" tools:context=".MainActivity">
xmlns:tools="http://schemas.android.com/tools" tools:context=".activities.MainActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ buildscript {
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath "io.realm:realm-gradle-plugin:2.0.0"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down