Browse Source

完成朋友圈图片发布和朋友圈自动刷新回调

Junqin Wang 8 years ago
parent
commit
7c875bcecf

BIN
app/res/drawable-hdpi/circle_new_tweet_img.png


BIN
app/res/drawable-mdpi/circle_new_tweet_img.png


BIN
app/res/drawable-xhdpi/circle_new_tweet_img.png


BIN
app/res/drawable-xxhdpi/circle_new_tweet_img.png


BIN
app/res/drawable-xxxhdpi/circle_new_tweet_img.png


+ 28 - 2
app/res/layout/circle_new_tweeting.xml

@@ -34,15 +34,41 @@
         app:layout_constraintHorizontal_bias="0.0"
         android:layout_marginStart="8dp"
         android:layout_marginEnd="8dp" />
+    <HorizontalScrollView
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:orientation="horizontal"
+        android:layout_marginTop="8dp"
+        app:layout_constraintTop_toBottomOf="@+id/linearLayout2"
+        app:layout_constraintLeft_toLeftOf="@+id/new_tweet_content"
+        android:scrollbarAlwaysDrawHorizontalTrack="true">
+
+        <LinearLayout
+            android:id="@+id/new_tweet_img_layout"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:orientation="horizontal">
+
+            <ImageView
+                android:id="@+id/new_tweet_add_imgs"
+                android:layout_width="128dp"
+                android:layout_height="128dp"
+                android:src="@drawable/circle_new_tweet_img"/>
+        </LinearLayout>
+    </HorizontalScrollView>
+
 
     <LinearLayout
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:background="@color/grey_light_light"
         android:padding="4dp"
+        android:layout_marginLeft="8dp"
         app:layout_constraintLeft_toLeftOf="@+id/new_tweet_content"
-        android:layout_marginTop="10dp"
-        app:layout_constraintTop_toBottomOf="@+id/new_tweet_content">
+        android:layout_marginTop="8dp"
+        app:layout_constraintTop_toBottomOf="@+id/new_tweet_content"
+        android:id="@+id/linearLayout2"
+        android:layout_marginStart="8dp">
         <ImageView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"

+ 150 - 10
app/src/com/sheishuo/app/circle/activity/NewTweetingActivity.java

@@ -1,22 +1,37 @@
 package com.sheishuo.app.circle.activity;
 
+import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.widget.EditText;
+import android.widget.ImageView;
+import android.widget.LinearLayout;
 import android.widget.TextView;
 import android.widget.Toast;
 
-import com.sheishuo.app.cache.AccountCache;
+import com.bumptech.glide.Glide;
+import com.netease.nim.uikit.common.media.picker.PickImageHelper;
+import com.netease.nim.uikit.common.media.picker.model.PhotoInfo;
 import com.sheishuo.app.R;
+import com.sheishuo.app.cache.AccountCache;
 import com.sheishuo.app.circle.presenter.NewTweetPresenter;
+import com.sheishuo.app.common.util.img.ImgUtil;
 import com.sheishuo.app.common.util.location.LocationHelper;
+import com.sheishuo.app.common.util.net.NetStatus;
 import com.sheishuo.app.common.views.BaseToolbar;
 import com.sheishuo.app.main.activity.MainActivity;
+import com.sheishuo.app.main.fragment.CircleOfFriendsFragment;
 import com.sheishuo.app.uikit_implements.SheishuoUI;
 
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import jp.wasabeef.glide.transformations.ColorFilterTransformation;
+
 /**
  * Created by KN on 2017/7/21.
  */
@@ -27,7 +42,12 @@ public class NewTweetingActivity extends SheishuoUI {
     private BaseToolbar toolbar;
     private EditText tweetContent;
     private TextView locationTV;
+    private LinearLayout imgsLayout;
+    private ImageView addImg;
     private NewTweetPresenter presenter;
+    private List<String> selectedPhotoPathList;
+    private List<Integer> photoIds;
+//    private static MainActivity activity;
 
     public static void start(Context context) {
         start(context, null);
@@ -35,7 +55,7 @@ public class NewTweetingActivity extends SheishuoUI {
 
     public static void start(Context context, Intent extras) {
         Intent intent = new Intent();
-        intent.setClass(context, MainActivity.class);
+        intent.setClass(context, NewTweetingActivity.class);
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
         if (extras != null) {
             intent.putExtras(extras);
@@ -43,7 +63,18 @@ public class NewTweetingActivity extends SheishuoUI {
         context.startActivity(intent);
     }
 
+    public static void startActivityForResult(Context context, Intent extras){
+        Intent intent = new Intent();
+        intent.setClass(context, NewTweetingActivity.class);
+        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
+        if (extras != null) {
+            intent.putExtras(extras);
+        }
+        if (context instanceof MainActivity){
+            ((MainActivity)context).startActivityForResult(intent,0);
+        }
 
+    }
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -51,18 +82,19 @@ public class NewTweetingActivity extends SheishuoUI {
         setContentView(R.layout.circle_new_tweeting);
         findViews();
         init();
+        setResult(CircleOfFriendsFragment.REFRESH_CIRCLE);
     }
 
 
-
-
-    private void findViews(){
+    private void findViews() {
         toolbar = (BaseToolbar) findViewById(R.id.new_tweet_toolbar);
         tweetContent = (EditText) findViewById(R.id.new_tweet_content);
         locationTV = (TextView) findViewById(R.id.new_tweet_location_tv);
+        imgsLayout = (LinearLayout) findViewById(R.id.new_tweet_img_layout);
+        addImg = (ImageView) findViewById(R.id.new_tweet_add_imgs);
     }
 
-    private void init(){
+    private void init() {
 
         presenter = new NewTweetPresenter(this);
 
@@ -78,7 +110,17 @@ public class NewTweetingActivity extends SheishuoUI {
         toolbar.getRightTV().setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
-                presenter.publishNewTweeting(tweetContent.getText().toString());
+                if (NetStatus.IS_BUSY){
+                    showToast("内容发布中,请稍后...");
+                }else {
+                    getHandler().postDelayed(new Runnable() {
+                        @Override
+                        public void run() {
+                            showToast("内容发布中...");
+                            presenter.publishNewTweeting(tweetContent.getText().toString(),photoIds);
+                        }
+                    },1000);
+                }
             }
         });
 
@@ -87,18 +129,81 @@ public class NewTweetingActivity extends SheishuoUI {
         AccountCache.getAccount().setLatitude(LocationHelper.getLatitude());
         AccountCache.getAccount().setLongitude(LocationHelper.getLongitude());
 
-        Log.e(TAG,"location ===> " + LocationHelper.getLatitude() + "   " + LocationHelper.getLongitude());
+        Log.e(TAG, "location ===> " + LocationHelper.getLatitude() + "   " + LocationHelper.getLongitude());
         locationTV.setText(AccountCache.getAccount().getCity() + AccountCache.getAccount().getDistrict());
 
 
+        //初始化照片添加监听事件
+        selectedPhotoPathList = new ArrayList<>();
+        photoIds = new ArrayList<>();
+        addImg.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+
+                PickImageHelper.PickImageOption option = new PickImageHelper.PickImageOption();
+                PickImageHelper.pickImage(context, 0, option);
+
+            }
+        });
+
+    }
+
+    @Override
+    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+        super.onActivityResult(requestCode, resultCode, data);
+        if (resultCode == Activity.RESULT_OK) {
+            for (String key : data.getExtras().keySet()) {
+                Log.e("key", key);
+            }
+
+            Bundle result = data.getExtras();
+            if (!result.getBoolean("from_local")) {
+                //照相机
+                String filePath = result.getString("file_path");
+                imgsLayout.removeAllViews();
+                ImageView imageView = new ImageView(this);
+                imageView.setAdjustViewBounds(true);
+                int toDp = (int) ImgUtil.convertDpToPixel(128, context);
+                imageView.setLayoutParams(new LinearLayout.LayoutParams(toDp, toDp));
+                Glide.with(context).load(filePath)
+                        .centerCrop()
+                        .bitmapTransform(new ColorFilterTransformation(context, 0x7900CCCC))
+                        .into(imageView);
+                presenter.uploadPhoto(filePath);
+            } else {
+                //相册
+                imgsLayout.removeAllViews();
+                List<PhotoInfo> photoInfoList = (List<PhotoInfo>) result.getSerializable("photo_list");
+                for (PhotoInfo info : photoInfoList) {
+                    ImageView imageView = new ImageView(this);
+                    imageView.setAdjustViewBounds(true);
+                    int toDp = (int) ImgUtil.convertDpToPixel(128, context);
+                    imageView.setLayoutParams(new LinearLayout.LayoutParams(toDp, toDp));
+                    Glide.with(context).load(info.getFilePath())
+                            .centerCrop()
+                            .bitmapTransform(new ColorFilterTransformation(context, 0x7900CCCC))
+                            .into(imageView);
+                    imgsLayout.addView(imageView);
+                    selectedPhotoPathList.add(info.getAbsolutePath());
+                }
+
+                presenter.uploadPhoto(selectedPhotoPathList);
+            }
+
+        }
     }
 
+    @Override
+    protected void onDestroy() {
+        super.onDestroy();
+//        activity.onActivityResult(0,CircleOfFriendsFragment.REFRESH_CIRCLE,null);
+    }
 
     /**
      * 以下为开放给Presenter的通用方法
      */
 
-    public void showToast(final String toast){
+    public void showToast(final String toast) {
         getHandler().post(new Runnable() {
             @Override
             public void run() {
@@ -108,9 +213,44 @@ public class NewTweetingActivity extends SheishuoUI {
     }
 
 
-    public void onTweetSuccess(){
+
+    public void onTweetSuccess() {
         showToast("发布成功");
         finish();
     }
 
+
+
+    //当一张照片上传完成后回调
+    public void onOnePhotoUploaded(final int index, final int photoId){
+        getHandler().post(new Runnable() {
+            @Override
+            public void run() {
+                ImageView imageView = (ImageView) imgsLayout.getChildAt(index);
+                String filePath = selectedPhotoPathList.get(index);
+                Glide.with(context).load(filePath).into(imageView);
+                Collections.synchronizedList(photoIds);
+                photoIds.add(photoId);
+
+            }
+        });
+
+    }
+
+
+    //全部照片上传完成
+    public void onAllPhotosUploaded(){
+        getHandler().post(new Runnable() {
+            @Override
+            public void run() {
+                toolbar.getRightTV().setEnabled(true);
+            }
+        });
+    }
+
+
+
+
+
+
 }

+ 81 - 1
app/src/com/sheishuo/app/circle/model/NewTweetModel.java

@@ -1,5 +1,7 @@
 package com.sheishuo.app.circle.model;
 
+import android.util.Log;
+
 import com.google.gson.Gson;
 import com.sheishuo.app.cache.AccountCache;
 import com.sheishuo.app.circle.model.beans.NewTweetingBean;
@@ -7,10 +9,24 @@ import com.sheishuo.app.circle.presenter.NewTweetPresenter;
 import com.sheishuo.app.common.util.net.INet;
 import com.sheishuo.app.common.util.net.NetImpl;
 import com.sheishuo.app.common.util.net.NetInfo;
+import com.sheishuo.app.common.util.net.NetStatus;
 import com.sheishuo.app.common.util.net.ResponseCallback;
 import com.sheishuo.app.login.beans.LoginBean;
 
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
 import okhttp3.FormBody;
+import okhttp3.MultipartBody;
+import okhttp3.RequestBody;
 
 /**
  * Created by KN on 2017/7/21.
@@ -29,23 +45,86 @@ public class NewTweetModel {
 
 
 
-    public void publishNewTweeting(final String content){
+    public void uploadPhoto(final List<String> filesPath){
+        NetStatus.IS_BUSY = true;
+        final int allPhotoNum = filesPath.size();
+        final List<Integer> uploadedNum = new ArrayList<>();
+        uploadedNum.add(0);
+        uploadedNum.set(0,0);
+
+        for (int i = 0;i < filesPath.size();i++){
+            final int finalI = i;
+            new Thread(new Runnable() {
+                @Override
+                public void run() {
+                    final int index = finalI;
+                    net.uploadImgs(filesPath.get(finalI), new ResponseCallback() {
+                        @Override
+                        public void onSuccess(Object object) {
+                            //Log.e("object", (String) object);
+                            presenter.onOnePhotoUploaded(index,(int)object);
+                            presenter.showToast("第" + (finalI + 1) + "张照片上传完毕");
+                        }
+
+                        @Override
+                        public void onFailed() {
+                            presenter.showToast("第" + (finalI + 1) + "张照片上传失败");
+                        }
+                    });
+
+
+                    //同步List
+                    Collections.synchronizedList(uploadedNum);
+                    int temp = uploadedNum.get(0) + 1;
+                    uploadedNum.set(0,temp);
+                    if(uploadedNum.get(0) == allPhotoNum) {
+                        presenter.onAllPhotoUploaded();
+                        NetStatus.IS_BUSY = false;
+                    }
+                }
+
+
+            }).start();
+
+        }
+
+    }
+
+    public void publishNewTweeting(final String content,final List<Integer> photoIds){
+        NetStatus.IS_BUSY = true;
         new Thread(new Runnable() {
             @Override
             public void run() {
                 LoginBean.DBean accuont = AccountCache.getAccount();
+                String pics = "";
+                if (photoIds.size() > 0){
+                    int[] temp = new int[photoIds.size()];
+                    for (int i = 0;i < photoIds.size();i++){
+                        temp[i] = photoIds.get(i);
+                    }
+                    pics = Arrays.toString(temp);
+
+                    pics = pics.replace("[","");
+                    pics = pics.replace("]","");
+                }
+
+
                 FormBody body = new FormBody.Builder()
                         .add("id",accuont.getId())
                         .add("msg",content)
                         .add("local",accuont.getCity() + accuont.getDistrict())
                         .add("latitude",accuont.getLatitude())
                         .add("longitude",accuont.getLongitude())
+                        .add("pic",pics)
                         .build();
 
 
+
+
                 net.post(NetInfo.NEW_TWEETING, body, new ResponseCallback() {
                     @Override
                     public void onSuccess(Object object) {
+                        NetStatus.IS_BUSY = false;
                         bean = new Gson().fromJson((String)object,NewTweetingBean.class);
                         if (0==bean.getC()){
                             presenter.onPublishSuccess();
@@ -56,6 +135,7 @@ public class NewTweetModel {
 
                     @Override
                     public void onFailed() {
+                        NetStatus.IS_BUSY = false;
                         presenter.showToast("发布失败");
                     }
                 });

+ 24 - 2
app/src/com/sheishuo/app/circle/presenter/NewTweetPresenter.java

@@ -4,6 +4,9 @@ import com.sheishuo.app.circle.activity.NewTweetingActivity;
 import com.sheishuo.app.circle.model.NewTweetModel;
 import com.sheishuo.app.main.model.CircleModel;
 
+import java.util.ArrayList;
+import java.util.List;
+
 /**
  * Created by KN on 2017/7/21.
  */
@@ -18,8 +21,26 @@ public class NewTweetPresenter {
     }
 
 
-    public void publishNewTweeting(String content){
-        model.publishNewTweeting(content);
+    public void uploadPhoto(String filePath){
+        List<String> list = new ArrayList<>();
+        list.add(filePath);
+        uploadPhoto(list);
+    }
+
+    public void uploadPhoto(List<String> filesPath){
+        model.uploadPhoto(filesPath);
+    }
+
+    public void onOnePhotoUploaded(int index,int photoId){
+        activity.onOnePhotoUploaded(index,photoId);
+    }
+
+    public void onAllPhotoUploaded(){
+        activity.onAllPhotosUploaded();
+    }
+
+    public void publishNewTweeting(String content,List<Integer> ids){
+        model.publishNewTweeting(content,ids);
     }
 
     public void onPublishSuccess(){
@@ -30,4 +51,5 @@ public class NewTweetPresenter {
         activity.showToast(toast);
     }
 
+
 }

+ 7 - 0
app/src/com/sheishuo/app/common/util/net/INet.java

@@ -1,14 +1,21 @@
 package com.sheishuo.app.common.util.net;
 
+import java.util.List;
+
 import okhttp3.FormBody;
+import okhttp3.MediaType;
+import okhttp3.RequestBody;
 
 /**
  * Created by KN on 2017/7/14.
  */
 
 public interface INet {
+    MediaType JSON = MediaType.parse("application/json; charset=utf-8");
     void baseQuery();
     void post(String url,FormBody body,ResponseCallback callback);
+    void post(String url, RequestBody body,ResponseCallback callback);
     void loginQuery(String mobile,String pwd,ResponseCallback callback);
     void getGiftList(ResponseCallback callback);
+    void uploadImgs(String filePath,ResponseCallback callback);
 }

+ 76 - 0
app/src/com/sheishuo/app/common/util/net/NetImpl.java

@@ -4,15 +4,26 @@ import android.util.Log;
 
 import com.google.gson.Gson;
 import com.google.gson.JsonObject;
+import com.sheishuo.app.cache.AccountCache;
 import com.sheishuo.app.cache.GiftCache;
 import com.sheishuo.app.common.beans.GiftBean;
 import com.sheishuo.app.login.beans.LoginBean;
 
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import java.io.File;
 import java.io.IOException;
+import java.util.List;
 
+import okhttp3.Call;
+import okhttp3.Callback;
 import okhttp3.FormBody;
+import okhttp3.MediaType;
+import okhttp3.MultipartBody;
 import okhttp3.OkHttpClient;
 import okhttp3.Request;
+import okhttp3.RequestBody;
 import okhttp3.Response;
 
 /**
@@ -21,6 +32,7 @@ import okhttp3.Response;
 
 public class  NetImpl implements INet {
 
+
     private static OkHttpClient client = new OkHttpClient.Builder().build();
     private Gson gson = new Gson();
 
@@ -52,6 +64,28 @@ public class  NetImpl implements INet {
     }
 
     @Override
+    public void post(String url, RequestBody body, ResponseCallback callback) {
+        Request request = new Request.Builder()
+                .url(url)
+                .post(body)
+                .build();
+
+        try {
+            Response response = client.newCall(request).execute();
+
+            if (200 == response.code()){
+                callback.onSuccess(response.body().string());
+            }else {
+                callback.onFailed();
+            }
+
+        } catch (IOException e) {
+            e.printStackTrace();
+            callback.onFailed();
+        }
+    }
+
+    @Override
     public void loginQuery(String mobile, String pwd, final ResponseCallback callback) {
         final FormBody body = new FormBody.Builder()
                 .add("mobile",mobile)
@@ -129,5 +163,47 @@ public class  NetImpl implements INet {
 
     }
 
+    @Override
+    public void uploadImgs(String filePath, final ResponseCallback callback) {
+        File file = new File(filePath);
+        MultipartBody.Builder builder = new MultipartBody.Builder();
+        builder.setType(MultipartBody.FORM);
+
+        builder.addFormDataPart("user_id", AccountCache.getAccount().getId());
+        builder.addFormDataPart("upload",file.getName(), RequestBody.create(null,file));
+
+
+        RequestBody body = builder.build();
+        Request request = new Request.Builder()
+                .url(NetInfo.UPLOAD_IMG)
+                .post(body)
+                .build();
+
+        Call call = client.newCall(request);
+        call.enqueue(new Callback() {
+            @Override
+            public void onFailure(Call call, IOException e) {
+                callback.onFailed();
+                e.printStackTrace();
+            }
+
+            @Override
+            public void onResponse(Call call, Response response) throws IOException {
+                try {
+                    JSONObject jsonObject = new JSONObject(response.body().string());
+                    if (jsonObject.getInt("c") == 0){
+                        int imgCode = jsonObject.getJSONObject("d").getInt("aid");
+                        callback.onSuccess(imgCode);
+                    }else {
+                        callback.onFailed();
+                    }
+                } catch (JSONException e) {
+                    e.printStackTrace();
+                    callback.onFailed();
+                }
+            }
+        });
+    }
+
 
 }

+ 1 - 0
app/src/com/sheishuo/app/common/util/net/NetInfo.java

@@ -14,4 +14,5 @@ public class NetInfo {
     public final static String NEW_TWEETING = INDEX + "?m=who&c=social&a=publish";
     public final static String GIFT_LIST = INDEX + "?m=who&c=index&a=gift_list";
     public final static String GET_CODE = INDEX + "?m=who&c=index&a=send_code";
+    public final static String UPLOAD_IMG = INDEX + "?m=who&c=social&a=upload";
 }

+ 4 - 37
app/src/com/sheishuo/app/main/activity/MainActivity.java

@@ -46,6 +46,8 @@ import com.sheishuo.app.uikit_implements.SheishuoUI;
 
 import java.util.ArrayList;
 
+import static com.sheishuo.app.main.fragment.CircleOfFriendsFragment.REFRESH_CIRCLE;
+
 /**
  * 主界面
  * <p/>
@@ -210,42 +212,6 @@ public class MainActivity extends SheishuoUI {
         outState.clear();
     }
 
-//    @Override
-//    public boolean onCreateOptionsMenu(Menu menu) {
-//        MenuInflater inflater = getMenuInflater();
-//        inflater.inflate(R.menu.main_activity_menu, menu);
-//        super.onCreateOptionsMenu(menu);
-//        return true;
-//    }
-//
-//    @Override
-//    public boolean onOptionsItemSelected(MenuItem item) {
-//        switch (item.getItemId()) {
-//            case R.id.about:
-//                startActivity(new Intent(MainActivity.this, SettingsActivity.class));
-//                break;
-//            case R.id.create_normal_team:
-//                ContactSelectActivity.Option option = TeamHelper.getCreateContactSelectOption(null, 50);
-//                NimUIKit.startContactSelect(MainActivity.this, option, REQUEST_CODE_NORMAL);
-//                break;
-//            case R.id.create_regular_team:
-//                ContactSelectActivity.Option advancedOption = TeamHelper.getCreateContactSelectOption(null, 50);
-//                NimUIKit.startContactSelect(MainActivity.this, advancedOption, REQUEST_CODE_ADVANCED);
-//                break;
-//            case R.id.search_advanced_team:
-//                AdvancedTeamSearchActivity.start(MainActivity.this);
-//                break;
-//            case R.id.add_buddy:
-//                AddFriendActivity.start(MainActivity.this);
-//                break;
-//            case R.id.search_btn:
-//                GlobalSearchActivity.start(MainActivity.this);
-//                break;
-//            default:
-//                break;
-//        }
-//        return super.onOptionsItemSelected(item);
-//    }
 
     private void onParseIntent() {
         Intent intent = getIntent();
@@ -288,7 +254,6 @@ public class MainActivity extends SheishuoUI {
 
     @Override
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
-        super.onActivityResult(requestCode, resultCode, data);
         Log.e(TAG,"onActivityResult " + requestCode + "    " + resultCode);
         if (resultCode == Activity.RESULT_OK) {
             if (requestCode == REQUEST_CODE_NORMAL) {
@@ -302,6 +267,8 @@ public class MainActivity extends SheishuoUI {
                 final ArrayList<String> selected = data.getStringArrayListExtra(ContactSelectActivity.RESULT_DATA);
                 TeamCreateHelper.createAdvancedTeam(MainActivity.this, selected);
             }
+        }else if(resultCode == REFRESH_CIRCLE){
+            mainFragment.onActivityResult(requestCode,resultCode,data);
         }
 
     }

+ 53 - 2
app/src/com/sheishuo/app/main/adapter/CircleListAdapter.java

@@ -51,6 +51,7 @@ public class CircleListAdapter  extends RecyclerView.Adapter<CircleListAdapter.C
         this.addAll(bean);
         this.context = fragment.getActivity();
         this.circleFragment = fragment;
+
     }
 
     @Override
@@ -213,6 +214,8 @@ public class CircleListAdapter  extends RecyclerView.Adapter<CircleListAdapter.C
 
     }
 
+
+    //onViewRecycled重置item
     @Override
     public void onViewRecycled(CircleListViewHolder holder) {
         super.onViewRecycled(holder);
@@ -253,12 +256,57 @@ public class CircleListAdapter  extends RecyclerView.Adapter<CircleListAdapter.C
 
 
 
+    //添加数据
+    public void addAll(CircleBean bean){
+        final List<CircleBean.DBean.ListBean> datas = bean.getD().getList();
 
 
-    public void addAll(CircleBean bean){
-        this.data.addAll(bean.getD().getList());
+        //将数据中的所有用户ID存入List
+        List<String> userIds = new ArrayList<>();
+        //遍历发布者ID
+        for (CircleBean.DBean.ListBean data : datas){
+            if (!userIds.contains(data.getUser_id()))
+                userIds.add(data.getUser_id());
+
+            //遍历喜欢者ID
+            for (String likedUser : data.getLike_users()){
+                if (!userIds.contains(likedUser))
+                    userIds.add(likedUser);
+            }
+            //遍历评论者ID
+            for (CircleBean.DBean.ListBean.Comment comment : data.getComments_list()){
+                if (!userIds.contains(comment.getUser_id()))
+                    userIds.add(comment.getUser_id());
+            }
+        }
+
+        NIMClient.getService(UserService.class).fetchUserInfo(userIds).setCallback(new RequestCallback<List<NimUserInfo>>() {
+            @Override
+            public void onSuccess(List<NimUserInfo> nimUserInfos) {
+                //添加数据
+                data.addAll(datas);
+                notifyDataSetChanged();
+                Log.e("添加数据成功","Success");
+            }
+
+            @Override
+            public void onFailed(int i) {
+                Log.e("Failed",i+"");
+            }
+
+            @Override
+            public void onException(Throwable throwable) {
+
+            }
+        });
+
     }
 
+    //清空数据
+    public void clearAll(){
+        data.clear();
+    }
+    //点击事件
     @Override
     public void onClick(View v) {
         if (listener != null){
@@ -273,6 +321,9 @@ public class CircleListAdapter  extends RecyclerView.Adapter<CircleListAdapter.C
 
     }
 
+
+
+    //ViewHolder
     class CircleListViewHolder extends RecyclerView.ViewHolder{
         private CardView layout;
         private LinearLayout commentsLayout;

+ 32 - 7
app/src/com/sheishuo/app/main/fragment/CircleOfFriendsFragment.java

@@ -34,6 +34,8 @@ import com.sheishuo.app.main.presenter.CirclePresenter;
 
 public class CircleOfFriendsFragment extends MainTabFragment{
     private String TAG = this.getClass().getSimpleName();
+    public final static int REFRESH_CIRCLE = 10000;
+    private boolean isLoaded = false;
 
     private Context context = getActivity();
     private CirclePresenter presenter = new CirclePresenter(this);
@@ -77,7 +79,7 @@ public class CircleOfFriendsFragment extends MainTabFragment{
         username.setText(user.getName());
 
 
-
+        isLoaded = true;
 
     }
 
@@ -101,9 +103,7 @@ public class CircleOfFriendsFragment extends MainTabFragment{
             @Override
             public void onClick(View v) {
                 Log.e(TAG,"start NewTweetActivity");
-                //NewTweetingActivity.start(getActivity());
-
-                startActivity(new Intent(getActivity(),NewTweetingActivity.class));
+                NewTweetingActivity.startActivityForResult(getActivity(),null);
             }
         });
 
@@ -114,11 +114,11 @@ public class CircleOfFriendsFragment extends MainTabFragment{
     public void setUserVisibleHint(boolean isVisibleToUser) {
         super.setUserVisibleHint(isVisibleToUser);
         if (isVisibleToUser && toolbar != null) initToolbar();
+
+        if (isVisibleToUser && isLoaded) refreshCircle();
     }
 
-    /**
-     *  以下为开放调用的方法
-     */
+    /**********************以下为开放调用的方法*********************/
 
 
     public void showToast(final String str){
@@ -166,12 +166,37 @@ public class CircleOfFriendsFragment extends MainTabFragment{
         });
     }
 
+    /**
+     * 加载更多朋友圈
+     * @param lastId
+     */
     public void loadCircleListMore(String lastId){
         presenter.loadCircleList(AccountCache.getAccount().getId(),lastId);
     }
 
 
+    /**
+     * 刷新朋友圈
+     */
+    public void refreshCircle(){
+        if (adapter != null){
+            adapter.clearAll();
+            //防止无网络时invalid viewholder
+            recyclerView.smoothScrollToPosition(0);
+            loadCircleListMore("");
+        }
+    }
+
 
 
 
+    @Override
+    public void onActivityResult(int requestCode, int resultCode, Intent data) {
+        super.onActivityResult(requestCode, resultCode, data);
+        switch (resultCode){
+            case REFRESH_CIRCLE:
+                refreshCircle();
+                break;
+        }
+    }
 }

+ 10 - 6
app/src/com/sheishuo/app/main/fragment/HomeFragment.java

@@ -6,6 +6,7 @@ import android.content.Intent;
 import android.os.Bundle;
 import android.support.annotation.NonNull;
 import android.support.design.widget.BottomNavigationView;
+import android.support.v4.app.Fragment;
 import android.support.v4.view.ViewPager;
 import android.support.v4.view.ViewPager.OnPageChangeListener;
 import android.support.v7.widget.Toolbar;
@@ -380,18 +381,21 @@ public class HomeFragment extends TFragment implements OnPageChangeListener, Rem
     @Override
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
         //super.onActivityResult(requestCode, resultCode, data);
-        List<String> test = data.getStringArrayListExtra(ContactSelectActivity.RESULT_DATA);
-        Log.e("返回的用户为",test.get(0));
-
-        Log.e("resultCode",resultCode+"");
-        Log.e("requestCode",requestCode+"");
         switch (resultCode){
             //创建群回调
             case Activity.RESULT_OK:
                 List<String> memberList = data.getStringArrayListExtra(ContactSelectActivity.RESULT_DATA);
-                Log.e("返回的用户为",memberList.get(0));
                 TeamCreateHelper.createAdvancedTeam(context,memberList);
                 break;
+
+            //朋友圈自动刷新回调
+            case CircleOfFriendsFragment.REFRESH_CIRCLE:
+                for (Fragment fragment : getFragmentManager().getFragments()){
+                    if (fragment instanceof CircleOfFriendsFragment)
+                        fragment.onActivityResult(requestCode,resultCode,data);
+
+                }
+                break;
         }
     }
 

+ 5 - 0
app/src/com/sheishuo/app/main/model/CircleModel.java

@@ -7,6 +7,7 @@ import com.sheishuo.app.common.beans.CircleUserLikedBean;
 import com.sheishuo.app.common.util.net.INet;
 import com.sheishuo.app.common.util.net.NetImpl;
 import com.sheishuo.app.common.util.net.NetInfo;
+import com.sheishuo.app.common.util.net.NetStatus;
 import com.sheishuo.app.common.util.net.ResponseCallback;
 import com.sheishuo.app.main.presenter.CirclePresenter;
 
@@ -30,6 +31,7 @@ public class CircleModel implements ICircle {
     //获取朋友圈列表
     @Override
     public void getCircleList(final String user_id, final String last_id) {
+        NetStatus.IS_BUSY = true;
         new Thread(new Runnable() {
             @Override
             public void run() {
@@ -49,11 +51,14 @@ public class CircleModel implements ICircle {
                         }else {
                             onFailed();
                         }
+
+                        NetStatus.IS_BUSY = false;
                     }
 
                     @Override
                     public void onFailed() {
                         presenter.showToast("加载失败,请检查网络");
+                        NetStatus.IS_BUSY = false;
                     }
                 });
 

+ 0 - 1
app/src/com/sheishuo/app/main/presenter/CirclePresenter.java

@@ -34,7 +34,6 @@ public class CirclePresenter {
         if (NetStatus.IS_BUSY){
             fragment.showToast("正在加载中...");
         }else {
-            NetStatus.IS_BUSY = true;
             model.getCircleList(user_id,last_id);
         }
     }