Skip to content

Commit

Permalink
Integrated with GreenDao.
Browse files Browse the repository at this point in the history
  • Loading branch information
uncleleonfan committed Oct 21, 2016
1 parent 029225d commit 473137a
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 46 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ http://www.cnblogs.com/tianzhijiexian/p/4297664.html
## 添加好友 ##
发送好友申请-->好友同意或者拒绝-->好友同意则写入数据库

### GreenDAO ###
[Github](https://github.com/greenrobot/greenDAO)

[官网](http://greenrobot.org/greendao/)

[AppBrain](http://www.appbrain.com/stats/libraries/details/greendao/greendao)

[使用文档](http://greenrobot.org/greendao/documentation/)

[中文使用文档](http://www.jianshu.com/p/2f7f48563141)

1. 保存联系人
2. 查询联系人
3. 删除联系人

### 隐藏软键盘 ###

# 聊天界面 #
Expand Down
2 changes: 2 additions & 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: 'org.greenrobot.greendao'


android {
Expand Down Expand Up @@ -29,4 +30,5 @@ dependencies {
compile 'com.roughike:bottom-bar:2.0.2'
compile 'com.android.support:cardview-v7:24.2.0'
compile 'org.greenrobot:eventbus:3.0.0'
compile 'org.greenrobot:greendao:3.2.0'
}

This file was deleted.

4 changes: 4 additions & 0 deletions app/src/main/java/com/itheima/leon/qqdemo/app/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ public static class SpKey {
public static class ExtraKey {
public static final String USER_NAME = "user_name";
}

public static class Database {
public static final String DATABASE_NAME = "qq-db";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.itheima.leon.qqdemo.BuildConfig;
import com.itheima.leon.qqdemo.R;
import com.itheima.leon.qqdemo.adpater.EMMessageListenerAdapter;
import com.itheima.leon.qqdemo.database.DatabaseManager;
import com.itheima.leon.qqdemo.ui.activity.ChatActivity;

import java.util.Iterator;
Expand All @@ -44,10 +45,12 @@ public void onCreate() {
initHuanXin();
initBmob();
initSoundPool();

DatabaseManager.getInstance().init(this);
EMClient.getInstance().chatManager().addMessageListener(mEMMessageListenerAdapter);
}



private void initSoundPool() {
mSoundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
mDuanSound = mSoundPool.load(this, R.raw.duan, 1);
Expand Down
46 changes: 46 additions & 0 deletions app/src/main/java/com/itheima/leon/qqdemo/database/Contact.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.itheima.leon.qqdemo.database;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Generated;

/**
* 创建者: Leon
* 创建时间: 2016/10/21 17:47
* 描述: TODO
*/
@Entity
public class Contact {
public static final String TAG = "Contact";
@Id(autoincrement = true)
private Long id;

private String username;

@Generated(hash = 1642963851)
public Contact(Long id, String username) {
this.id = id;
this.username = username;
}

@Generated(hash = 672515148)
public Contact() {
}

public Long getId() {
return this.id;
}

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

public String getUsername() {
return this.username;
}

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.itheima.leon.qqdemo.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.itheima.leon.qqdemo.app.Constant;

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

/**
* 创建者: Leon
* 创建时间: 2016/10/21 18:57
* 描述: TODO
*/
public class DatabaseManager {
public static final String TAG = "DatabaseManager";

private static DatabaseManager sInstance;
private DaoSession mDaoSession;


public static DatabaseManager getInstance() {
if (sInstance == null) {
synchronized (DatabaseManager.class) {
if (sInstance == null) {
sInstance = new DatabaseManager();
}
}
}
return sInstance;
}

public void init(Context context) {
DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(context, Constant.Database.DATABASE_NAME, null);
SQLiteDatabase writableDatabase = devOpenHelper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(writableDatabase);
mDaoSession = daoMaster.newSession();
}

public void saveContact(String userName) {
Contact contact = new Contact();
contact.setUsername(userName);
mDaoSession.getContactDao().save(contact);
}

public List<String> queryAllContacts() {
List<Contact> list = mDaoSession.getContactDao().queryBuilder().list();
ArrayList<String> contacts = new ArrayList<String>();
for (int i = 0; i < list.size(); i++) {
String contact = list.get(i).getUsername();
contacts.add(contact);
}
return contacts;
}

public void deleteAllContacts() {
ContactDao contactDao = mDaoSession.getContactDao();
contactDao.deleteAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class AddFriendItem {

public String timestamp;

public String isAdded;
public boolean isAdded;


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.itheima.leon.qqdemo.database.DatabaseManager;
import com.itheima.leon.qqdemo.event.AddFriendEvent;
import com.itheima.leon.qqdemo.model.AddFriendItem;
import com.itheima.leon.qqdemo.model.User;
Expand Down Expand Up @@ -38,30 +39,53 @@ public AddFriendPresenterImpl(AddFriendView addFriendView) {
}

@Override
public void searchFriend(String keyword) {
public void searchFriend(final String keyword) {
Log.d(TAG, "searchFriend: " + keyword);
mAddFriendView.onStartSearch();
//search friend
//注:模糊查询只对付费用户开放,付费后可直接使用。
BmobQuery<User> query = new BmobQuery<User>();
query.addWhereContains("username", keyword).addWhereNotEqualTo("username", EMClient.getInstance().getCurrentUser());
query.findObjects(new FindListener<User>() {
ThreadUtils.runOnBackgroundThread(new Runnable() {
@Override
public void done(List<User> list, BmobException e) {
if (e == null && list.size() > 0) {
List<AddFriendItem> listItems = new ArrayList<AddFriendItem>();
for (int i = 0; i <list.size(); i++) {
AddFriendItem item = new AddFriendItem();
item.timestamp = list.get(i).getCreatedAt();
item.userName = list.get(i).getUsername();
listItems.add(item);
public void run() {
//search friend
//注:模糊查询只对付费用户开放,付费后可直接使用。
BmobQuery<User> query = new BmobQuery<User>();
query.addWhereContains("username", keyword).addWhereNotEqualTo("username", EMClient.getInstance().getCurrentUser());
query.findObjects(new FindListener<User>() {
@Override
public void done(List<User> list, BmobException e) {
Log.d(TAG, "done: " + Thread.currentThread().getName());
processResult(list, e);
}
});
}
});

}

private void processResult(List<User> list, BmobException e) {
if (e == null && list.size() > 0) {
final List<AddFriendItem> listItems = new ArrayList<AddFriendItem>();
List<String> contacts = DatabaseManager.getInstance().queryAllContacts();
for (int i = 0; i <list.size(); i++) {
AddFriendItem item = new AddFriendItem();
item.timestamp = list.get(i).getCreatedAt();
item.userName = list.get(i).getUsername();
item.isAdded = contacts.contains(item.userName);
listItems.add(item);
}
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
mAddFriendView.onSearchSuccess(listItems);
} else {
}
});
} else {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
mAddFriendView.onSearchFailed();
}
}
});
});
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.itheima.leon.qqdemo.database.DatabaseManager;
import com.itheima.leon.qqdemo.model.ContactItem;
import com.itheima.leon.qqdemo.presenter.ContactPresenter;
import com.itheima.leon.qqdemo.utils.ThreadUtils;
Expand Down Expand Up @@ -98,6 +99,7 @@ public void run() {
private void startGetContactList() throws HyphenateException {
mContactItems = new ArrayList<ContactItem>();
List<String> contacts = EMClient.getInstance().contactManager().getAllContactsFromServer();
DatabaseManager.getInstance().deleteAllContacts();
if (!contacts.isEmpty()) {
for (int i = 0; i < contacts.size(); i++) {
ContactItem item = new ContactItem();
Expand All @@ -106,10 +108,15 @@ private void startGetContactList() throws HyphenateException {
item.showSection = false;
}
mContactItems.add(item);
saveContactToDatabase(item.userName);
}
}
}

private void saveContactToDatabase(String userName) {
DatabaseManager.getInstance().saveContact(userName);
}

private boolean itemInSameGroup(int i, ContactItem item) {
return i > 0 && (item.getFirstLetter() == mContactItems.get(i - 1).getFirstLetter());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ private void init() {
public void bindView(AddFriendItem addFriendItem) {
mUserName.setText(addFriendItem.userName);
mTimestamp.setText(addFriendItem.timestamp);
if (addFriendItem.isAdded) {
mAdd.setText(getContext().getString(R.string.added));
mAdd.setEnabled(false);
} else {
mAdd.setText(getContext().getString(R.string.add));
mAdd.setEnabled(true);
}
}

@OnClick(R.id.add)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@
<string name="max_unread_count">99+</string>
<string name="receive_new_message">收到新消息</string>
<string name="user_login_another_device">用户在其他设备登录</string>
<string name="added">已添加</string>
</resources>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

classpath 'org.greenrobot:greendao-gradle-plugin:3.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down

0 comments on commit 473137a

Please sign in to comment.