Skip to content

Commit dcf3d84

Browse files
committed
重构
1 parent 83ac4eb commit dcf3d84

7 files changed

Lines changed: 298 additions & 2 deletions

File tree

diycode-app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@
7979
android:name=".activity.MyTopicActivity"
8080
android:theme="@style/AppTheme.NoActionBar">
8181
</activity>
82+
<activity
83+
android:name=".activity.MyTopicActivity2"
84+
android:theme="@style/AppTheme.NoActionBar">
85+
</activity>
8286
<activity
8387
android:name=".activity.AboutActivity"
8488
android:theme="@style/AppTheme.NoActionBar">

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,13 @@ public boolean onNavigationItemSelected(MenuItem item) {
309309
openActivity(LoginActivity.class);
310310
return true;
311311
}
312-
MyTopicActivity.newInstance(this, MyTopicActivity.InfoType.MY_TOPIC);
312+
MyTopicActivity2.newInstance(this, MyTopicActivity2.InfoType.MY_TOPIC);
313313
} else if (id == R.id.nav_collect) {
314314
if (!mDiycode.isLogin()) {
315315
openActivity(LoginActivity.class);
316316
return true;
317317
}
318-
MyTopicActivity.newInstance(this, MyTopicActivity.InfoType.MY_COLLECT);
318+
MyTopicActivity2.newInstance(this, MyTopicActivity2.InfoType.MY_COLLECT);
319319
} else if (id == R.id.nav_about) {
320320
openActivity(AboutActivity.class);
321321
} else if (id == R.id.nav_setting) {
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 20:47:16
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.activity;
24+
25+
import android.content.Context;
26+
import android.content.Intent;
27+
import android.support.v4.app.FragmentManager;
28+
import android.support.v4.app.FragmentTransaction;
29+
import android.view.View;
30+
31+
import com.gcssloop.diycode.R;
32+
import com.gcssloop.diycode.base.app.BaseActivity;
33+
import com.gcssloop.diycode.base.app.ViewHolder;
34+
import com.gcssloop.diycode.fragment.UserCollectionTopicFragment;
35+
import com.gcssloop.diycode.fragment.UserCreateTopicFragment;
36+
import com.gcssloop.diycode.utils.DataCache;
37+
import com.gcssloop.diycode_sdk.api.Diycode;
38+
import com.gcssloop.diycode_sdk.api.user.bean.UserDetail;
39+
40+
import java.io.IOException;
41+
42+
public class MyTopicActivity2 extends BaseActivity {
43+
private DataCache mDataCache;
44+
45+
// 数据类型
46+
enum InfoType {
47+
MY_TOPIC, MY_COLLECT
48+
}
49+
50+
private InfoType current_type = InfoType.MY_TOPIC;
51+
52+
public static void newInstance(Context context, InfoType type) {
53+
Intent intent = new Intent(context, MyTopicActivity2.class);
54+
intent.putExtra("type", type);
55+
context.startActivity(intent);
56+
}
57+
58+
@Override
59+
protected int getLayoutId() {
60+
return R.layout.activity_topic;
61+
}
62+
63+
@Override
64+
protected void initDatas() {
65+
mDiycode = Diycode.getSingleInstance();
66+
mDataCache = new DataCache(this);
67+
Intent intent = getIntent();
68+
InfoType type = (InfoType) intent.getSerializableExtra("type");
69+
if (type != null) {
70+
current_type = type;
71+
} else {
72+
current_type = InfoType.MY_TOPIC;
73+
}
74+
}
75+
76+
@Override
77+
protected void initViews(ViewHolder holder, View root) {
78+
if (!mDiycode.isLogin()) {
79+
toastShort("用户未登录");
80+
return;
81+
}
82+
83+
// 获取用户名
84+
if (mDataCache.getMe() == null) {
85+
try {
86+
UserDetail me = mDiycode.getMeNow();
87+
mDataCache.saveMe(me);
88+
} catch (IOException e) {
89+
e.printStackTrace();
90+
}
91+
}
92+
String username = mDataCache.getMe().getLogin();
93+
94+
// 初始化 Fragment
95+
FragmentManager fragmentManager = getSupportFragmentManager();
96+
FragmentTransaction transaction = fragmentManager.beginTransaction();
97+
98+
if (current_type == InfoType.MY_COLLECT){
99+
setTitle("我的收藏");
100+
UserCollectionTopicFragment fragment1 = UserCollectionTopicFragment.newInstance(username);
101+
transaction.add(R.id.fragment, fragment1);
102+
} else if (current_type == InfoType.MY_TOPIC){
103+
setTitle("我的话题");
104+
UserCreateTopicFragment fragment2 = UserCreateTopicFragment.newInstance(username);
105+
transaction.add(R.id.fragment, fragment2);
106+
}
107+
transaction.commit();
108+
}
109+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public static void newInstance(Context context, int nodeId, String nodeName) {
5353
int NodeId = intent.getIntExtra(Key_Node_ID, 0);
5454
String NodeName = intent.getStringExtra(Key_Node_Name);
5555
setTitle(NodeName);
56+
5657
NodeTopicListFragment fragment = NodeTopicListFragment.newInstance(NodeId);
5758
FragmentManager fragmentManager = getSupportFragmentManager();
5859
FragmentTransaction transaction = fragmentManager.beginTransaction();
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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:03:52
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.base.app;
24+
25+
import android.support.annotation.NonNull;
26+
import android.support.v7.widget.RecyclerView;
27+
28+
import com.gcssloop.diycode.base.recyclerview.SpeedyLinearLayoutManager;
29+
import com.gcssloop.diycode_sdk.api.base.event.BaseEvent;
30+
import com.gcssloop.recyclerview.adapter.multitype.HeaderFooterAdapter;
31+
32+
import java.util.List;
33+
34+
public abstract class SimpleRefreshRecyclerFragment<T, Event extends BaseEvent<List<T>>> extends
35+
RefreshRecyclerFragment<T, Event> {
36+
37+
@NonNull @Override protected RecyclerView.LayoutManager getRecyclerViewLayoutManager() {
38+
return new SpeedyLinearLayoutManager(getContext());
39+
}
40+
41+
@Override protected void onRefresh(Event event, HeaderFooterAdapter adapter) {
42+
adapter.clearDatas();
43+
adapter.addDatas(event.getBean());
44+
toast("刷新成功");
45+
}
46+
47+
@Override protected void onLoadMore(Event event, HeaderFooterAdapter adapter) {
48+
adapter.addDatas(event.getBean());
49+
}
50+
51+
@Override protected void onError(Event event, String postType) {
52+
toast("加载失败");
53+
}
54+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 20:45:09
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.fragment;
24+
25+
import android.content.Context;
26+
import android.os.Bundle;
27+
import android.support.annotation.NonNull;
28+
import android.support.v7.widget.RecyclerView;
29+
30+
import com.gcssloop.diycode.base.app.SimpleRefreshRecyclerFragment;
31+
import com.gcssloop.diycode.fragment.provider.TopicProvider;
32+
import com.gcssloop.diycode_sdk.api.topic.bean.Topic;
33+
import com.gcssloop.diycode_sdk.api.user.event.GetUserCollectionTopicListEvent;
34+
import com.gcssloop.recyclerview.adapter.multitype.HeaderFooterAdapter;
35+
36+
public class UserCollectionTopicFragment extends SimpleRefreshRecyclerFragment<Topic,
37+
GetUserCollectionTopicListEvent> {
38+
private static String Key_User_Login_Name = "Key_User_Login_Name";
39+
private String loginName;
40+
41+
public static UserCollectionTopicFragment newInstance(String user_login_name) {
42+
Bundle args = new Bundle();
43+
args.putString(Key_User_Login_Name, user_login_name);
44+
UserCollectionTopicFragment fragment = new UserCollectionTopicFragment();
45+
fragment.setArguments(args);
46+
return fragment;
47+
}
48+
49+
@Override public void initData(HeaderFooterAdapter adapter) {
50+
Bundle args = getArguments();
51+
loginName = args.getString(Key_User_Login_Name);
52+
loadMore();
53+
}
54+
55+
@Override
56+
protected void setRecyclerViewAdapter(Context context, RecyclerView recyclerView,
57+
HeaderFooterAdapter adapter) {
58+
adapter.register(Topic.class, new TopicProvider(context));
59+
}
60+
61+
@NonNull @Override protected String request(int offset, int limit) {
62+
return mDiycode.getUserCollectionTopicList(loginName, offset, limit);
63+
}
64+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 20:34:58
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.fragment;
24+
25+
import android.content.Context;
26+
import android.os.Bundle;
27+
import android.support.annotation.NonNull;
28+
import android.support.v7.widget.RecyclerView;
29+
30+
import com.gcssloop.diycode.base.app.SimpleRefreshRecyclerFragment;
31+
import com.gcssloop.diycode.fragment.provider.TopicProvider;
32+
import com.gcssloop.diycode_sdk.api.topic.bean.Topic;
33+
import com.gcssloop.diycode_sdk.api.user.event.GetUserCreateTopicListEvent;
34+
import com.gcssloop.recyclerview.adapter.multitype.HeaderFooterAdapter;
35+
36+
public class UserCreateTopicFragment extends SimpleRefreshRecyclerFragment<Topic,
37+
GetUserCreateTopicListEvent> {
38+
private static String Key_User_Login_Name = "Key_User_Login_Name";
39+
private String loginName;
40+
41+
public static UserCreateTopicFragment newInstance(String user_login_name) {
42+
Bundle args = new Bundle();
43+
args.putString(Key_User_Login_Name, user_login_name);
44+
UserCreateTopicFragment fragment = new UserCreateTopicFragment();
45+
fragment.setArguments(args);
46+
return fragment;
47+
}
48+
49+
@Override public void initData(HeaderFooterAdapter adapter) {
50+
Bundle args = getArguments();
51+
loginName = args.getString(Key_User_Login_Name);
52+
loadMore();
53+
}
54+
55+
@Override
56+
protected void setRecyclerViewAdapter(Context context, RecyclerView recyclerView,
57+
HeaderFooterAdapter adapter) {
58+
adapter.register(Topic.class, new TopicProvider(context));
59+
}
60+
61+
@NonNull @Override protected String request(int offset, int limit) {
62+
return mDiycode.getUserCreateTopicList(loginName, null, offset, limit);
63+
}
64+
}

0 commit comments

Comments
 (0)