开发冲刺九

今天基础功能就告一段落了完成了通过二维码扫描加好友,以及设置隐私是否可以通过通讯录就好友

ContactsFriendActivity.java
package com.example.meet.UI;

import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.framework.adapter.CommonAdapter;
import com.example.framework.adapter.CommonViewHolder;
import com.example.framework.base.BaseBackActivity;
import com.example.framework.bmob.BmobManager;
import com.example.framework.bmob.MyMeetUser;
import com.example.framework.bmob.PrivateSet;
import com.example.framework.utils.CommonUtils;
import com.example.framework.utils.LogUtils;
import com.example.meet.R;
import com.example.meet.adapter.AddFriendAdapter;
import com.example.meet.model.AddFriendModel;

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


import io.reactivex.Observable;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.internal.operators.observable.ObservableSubscribeOn;
import io.reactivex.schedulers.Schedulers;

public class ContactsFriendActivity extends BaseBackActivity {

    private RecyclerView mContactView;
    private Map<String, String> contactsMap = new HashMap<>();
    private CommonAdapter<AddFriendModel> adapter;
    private List<AddFriendModel> modelList = new ArrayList<>();
    private Disposable disposable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_friend);
        initView();
    }

    private void initView() {
        mContactView = (RecyclerView) findViewById(R.id.mContactView);
        mContactView.setLayoutManager(new LinearLayoutManager(this));
        mContactView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        adapter = new CommonAdapter<>(modelList, new CommonAdapter.OnBindDataListener<AddFriendModel>() {
            @Override
            public void onBindViewHolder(final AddFriendModel model, CommonViewHolder viewHolder, int type, int position) {
                //加载图片
                viewHolder.setImageUrl(ContactsFriendActivity.this, R.id.iv_photo, model.getPhoto());
                //设置性别
                viewHolder.setImageResource(R.id.iv_sex
                        , model.isSex() ? R.drawable.img_boy_icon : R.drawable.img_girl_icon);
                //设置昵称
                viewHolder.setText(R.id.tv_nickname, model.getNickName());
                //年龄
                viewHolder.setText(R.id.tv_age, model.getAge() + "岁");
                //描述
                viewHolder.setText(R.id.tv_desc, model.getDesc());
                viewHolder.setLinearLayoutVisibility(R.id.ll_contact_info, View.VISIBLE);
                viewHolder.setText(R.id.tv_contact_name, "姓名:" + model.getContactName());
                viewHolder.setText(R.id.tv_contact_phone, "号码:" + model.getContactPhone());
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        UserInfoActivity.startActivity(ContactsFriendActivity.this
                                , model.getUserId());
                    }
                });
            }

            @Override
            public int getLayoutId(int type) {
                return R.layout.layout_search_user_item;
            }
        });

        mContactView.setAdapter(adapter);
        getContacts();
        getUser();
    }

    /**
     * 添加匹配到的用户
     */
    private void getUser() {
        /*
        *
        *
        * */
        
        disposable=Observable.create(new ObservableOnSubscribe<List<PrivateSet>>() {
            @Override
            public void subscribe(final ObservableEmitter<List<PrivateSet>> emitter) throws Exception {
                getContacts();
                BmobManager.getInstance().queryPrivateSet(new FindListener<PrivateSet>() {
                    @Override
                    public void done(List<PrivateSet> list, BmobException e) {
                        if(e==null)
                        {
                            emitter.onNext(list);
                            emitter.onComplete();
                        }
                    }
                });
            }
        }).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<PrivateSet>>() {
                    @Override
                    public void accept(List<PrivateSet> privateSets) throws Exception {
                        fixprivateSets(privateSets);

                    }
                });
                
                
                
                


    }
    /*
    * 过滤
    * */
    private void fixprivateSets(List<PrivateSet> privateSets) {
        List<String> userListPhone=new ArrayList<>();
        for(int  i =0;i<privateSets.size();i++)
        {
            PrivateSet sets=privateSets.get(i);
            String phone=sets.getPhone();
            userListPhone.add(phone);
        }
        if (contactsMap.size() > 0) {
            for (final Map.Entry<String, String> entry : contactsMap.entrySet()) {

                //过滤
                if(userListPhone.contains(entry.getValue()))
                {
                    return;
                }
                BmobManager.getInstance().queryUserByPhone(entry.getValue(), new FindListener<MyMeetUser>() {
                    @Override
                    public void done(List<MyMeetUser> object, BmobException e) {
                        if (e == null) {
                            if (CommonUtils.isEmpty(object)) {
                                MyMeetUser myMeetUser = object.get(0);
                                addContent(myMeetUser, entry.getKey(), entry.getValue());
                                adapter.notifyDataSetChanged();
                            }
                        }
                    }
                });
            }
        }

    }

    /**
     * 获取通讯录联系人
     */
    private void getContacts() {
        Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null, null, null, null);
        String phone;
        String name;
        while (cursor.moveToNext()) {
            name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            contactsMap.put(name, phone);
        }
    }

    /**
     * 添加用户信息数据
     */
    private void addContent(MyMeetUser user, String name, String phone) {
        AddFriendModel model = new AddFriendModel();
        model.setType(AddFriendAdapter.TYPE_CONTENT);
        model.setUserId(user.getObjectId());
        model.setAge(user.getAge());
        model.setDesc(user.getDesc());
        model.setSex(user.isSex());
        model.setPhoto(user.getPhoto());
        model.setNickName(user.getNickName());
        model.setContact(true);
        model.setContactName(name);
        model.setContactPhone(phone);
        modelList.add(model);
    }
    @Override
    public void onDestroy() {

        super.onDestroy();
        if(disposable!=null){
            if(disposable.isDisposed())
            {
                disposable.dispose();
            }
        }
    }
}

 

今天我们将代码整合到了一起,并且做了一些测试

在自己设置了隐私之后自己的朋友就不能通过手机通讯录添加自己,二维码扫描也正常,消息广场测试正常,即时添加好友聊天正常。

目前差不多已经完成了第一阶段的冲刺,这是第一次的整合,明天继续冲刺其他功能

 

posted @ 2020-04-26 22:42  yizhixiaozhu  阅读(141)  评论(0编辑  收藏  举报