Skip to content

Commit 3c52bce

Browse files
committed
重构
1 parent 828392b commit 3c52bce

6 files changed

Lines changed: 147 additions & 4 deletions

File tree

diycode-app/src/main/java/com/gcssloop/diycode/activity/MyTopicActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static void newInstance(Context context, InfoType type) {
5757

5858
@Override
5959
protected int getLayoutId() {
60-
return R.layout.activity_topic;
60+
return R.layout.activity_fragment;
6161
}
6262

6363
@Override

diycode-app/src/main/java/com/gcssloop/diycode/activity/TopicActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static void newInstance(Context context, int nodeId, String nodeName) {
4848
}
4949

5050
@Override protected int getLayoutId() {
51-
return R.layout.activity_topic;
51+
return R.layout.activity_fragment;
5252
}
5353

5454
@Override protected void initViews(ViewHolder holder, View root) {

diycode-app/src/main/java/com/gcssloop/diycode/fragment/NotificationsFragment.java

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,70 @@
2727
import android.support.v7.widget.RecyclerView;
2828

2929
import com.gcssloop.diycode.fragment.base.SimpleRefreshRecyclerFragment;
30+
import com.gcssloop.diycode.fragment.provider.NotificationsProvider;
3031
import com.gcssloop.diycode_sdk.api.notifications.bean.Notification;
3132
import com.gcssloop.diycode_sdk.api.notifications.event.GetNotificationsListEvent;
3233
import com.gcssloop.recyclerview.adapter.multitype.HeaderFooterAdapter;
3334

35+
import java.util.ArrayList;
36+
import java.util.List;
37+
3438
public class NotificationsFragment extends SimpleRefreshRecyclerFragment<Notification,
3539
GetNotificationsListEvent> {
36-
@Override public void initData(HeaderFooterAdapter adapter) {
40+
private static String TYPE_NodeChanged = "NodeChanged"; // 节点变更
41+
private static String TYPE_TopicReply = "TopicReply"; // Topic 回复
42+
private static String TYPE_NewsReply = "Hacknews"; // News 回复
43+
private static String TYPE_Mention = "Mention"; // 有人提及
44+
private static String MENTION_TYPE_TopicReply = "Reply"; // - Topic 回复中提及
45+
private static String MENTION_TYPE_NewReply = "HacknewsReply"; // - News 回复中提及
46+
private static String MENTION_TYPE_ProjectReply = "ProjectReply"; // - 项目 回复中提及
3747

48+
@Override public void initData(HeaderFooterAdapter adapter) {
49+
loadMore();
3850
}
3951

4052
@Override
4153
protected void setRecyclerViewAdapter(Context context, RecyclerView recyclerView,
4254
HeaderFooterAdapter adapter) {
43-
55+
adapter.register(Notification.class, new NotificationsProvider(getContext()));
4456
}
4557

4658
@NonNull @Override protected String request(int offset, int limit) {
4759
return mDiycode.getNotificationsList(offset, limit);
4860
}
61+
62+
@Override
63+
protected void onRefresh(GetNotificationsListEvent event, HeaderFooterAdapter adapter) {
64+
List<Notification> data = clearData(event.getBean());
65+
adapter.clearDatas();
66+
adapter.addDatas(data);
67+
}
68+
69+
@Override
70+
protected void onLoadMore(GetNotificationsListEvent event, HeaderFooterAdapter adapter) {
71+
List<Notification> data = clearData(event.getBean());
72+
adapter.addDatas(data);
73+
}
74+
75+
/**
76+
* 清洗数据,主要清洗对象
77+
* 1. HackNew 的回复 type = Hacknews
78+
* 2. HackNew 的提及 type = Mention, mention_type = HacknewsReply
79+
* 3. Project 的提及 type = Mention, mention_type = ProjectReply
80+
* <p>
81+
* 保留数据
82+
* 1. Topic 的回复 type = TopicReply
83+
* 2. Topic 的提及 type = Mention, mention_type = Reply
84+
*/
85+
public List<Notification> clearData(List<Notification> datas) {
86+
List<Notification> clearDatas = new ArrayList<>();
87+
for (Notification data : datas) {
88+
if (data.getType().equals(TYPE_TopicReply) ||
89+
(data.getType().equals(TYPE_Mention) && data.getMention_type().equals
90+
(MENTION_TYPE_TopicReply))) {
91+
clearDatas.add(data);
92+
}
93+
}
94+
return clearDatas;
95+
}
4996
}

diycode-app/src/main/java/com/gcssloop/diycode/fragment/provider/NotificationsProvider.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,76 @@
2424

2525
import android.content.Context;
2626
import android.support.annotation.NonNull;
27+
import android.text.Html;
28+
import android.text.Spanned;
29+
import android.view.View;
30+
import android.widget.ImageView;
31+
import android.widget.TextView;
2732

2833
import com.gcssloop.diycode.R;
34+
import com.gcssloop.diycode.activity.TopicContentActivity;
35+
import com.gcssloop.diycode.activity.UserActivity;
36+
import com.gcssloop.diycode.utils.HtmlUtil;
37+
import com.gcssloop.diycode.utils.ImageUtils;
2938
import com.gcssloop.diycode_sdk.api.notifications.bean.Notification;
39+
import com.gcssloop.diycode_sdk.api.user.bean.User;
3040
import com.gcssloop.recyclerview.adapter.base.RecyclerViewHolder;
3141
import com.gcssloop.recyclerview.adapter.multitype.BaseViewProvider;
3242

3343
public class NotificationsProvider extends BaseViewProvider<Notification> {
44+
private static String TYPE_NodeChanged = "NodeChanged"; // 节点变更
45+
private static String TYPE_TopicReply = "TopicReply"; // Topic 回复
46+
private static String TYPE_NewsReply = "Hacknews"; // News 回复
47+
private static String TYPE_Mention = "Mention"; // 有人提及
48+
private static String MENTION_TYPE_TopicReply = "Reply"; // - Topic 回复中提及
49+
private static String MENTION_TYPE_NewReply = "HacknewsReply"; // - News 回复中提及
50+
private static String MENTION_TYPE_ProjectReply = "ProjectReply"; // - 项目 回复中提及
51+
3452
public NotificationsProvider(@NonNull Context context) {
3553
super(context, R.layout.item_notification);
3654
}
3755

3856
@Override public void onBindView(RecyclerViewHolder holder, Notification bean) {
3957

58+
final User actor = bean.getActor();
59+
String suffix = "";
60+
String desc = "";
61+
int topic_id = -1;
62+
if (bean.getType().equals(TYPE_TopicReply)) {
63+
suffix = "回复了话题:";
64+
desc = bean.getReply().getTopic_title();
65+
topic_id = bean.getReply().getTopic_id();
66+
} else if (bean.getType().equals(TYPE_Mention) && bean.getMention_type().equals
67+
(MENTION_TYPE_TopicReply)) {
68+
suffix = "提到了你:";
69+
desc = bean.getMention().getBody_html();
70+
topic_id = bean.getMention().getTopic_id();
71+
}
72+
String type = actor.getLogin() + " " + suffix;
73+
74+
ImageView imageView = holder.get(R.id.avatar);
75+
ImageUtils.loadImage(mContext, actor.getAvatar_url(), imageView);
76+
holder.setText(R.id.notification_type, type);
77+
78+
Spanned result_desc = Html.fromHtml(HtmlUtil.removeP(desc));
79+
TextView text_desc = holder.get(R.id.desc);
80+
text_desc.setText(result_desc);
81+
82+
holder.setOnClickListener(new View.OnClickListener() {
83+
@Override
84+
public void onClick(View v) {
85+
UserActivity.newInstance(mContext, actor);
86+
}
87+
}, R.id.avatar, R.id.notification_type);
88+
89+
final int final_topic_id = topic_id;
90+
holder.get(R.id.item).setOnClickListener(new View.OnClickListener() {
91+
@Override
92+
public void onClick(View v) {
93+
TopicContentActivity.newInstance(mContext, final_topic_id);
94+
}
95+
});
4096
}
97+
98+
4199
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2017 GcsSloop
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* Last modified 2017-04-09 21:31:26
17+
*
18+
* GitHub: https://github.com/GcsSloop
19+
* Website: http://www.gcssloop.com
20+
* Weibo: http://weibo.com/GcsSloop
21+
*/
22+
23+
package com.gcssloop.diycode.utils;
24+
25+
import android.content.Context;
26+
import android.widget.ImageView;
27+
28+
import com.bumptech.glide.Glide;
29+
import com.bumptech.glide.load.engine.DiskCacheStrategy;
30+
31+
public class ImageUtils {
32+
public static void loadImage(Context context, String url, ImageView imageView) {
33+
String url2 = url;
34+
if (url.contains("diycode"))
35+
url2 = url.replace("large_avatar", "avatar");
36+
Glide.with(context).load(url2).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);
37+
}
38+
}
File renamed without changes.

0 commit comments

Comments
 (0)