重构专题页面相关逻辑
其他toolbar逻辑移除
This commit is contained in:
@ -2,101 +2,44 @@ package com.gh.gamecenter;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.text.TextUtils;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.webkit.DownloadListener;
|
||||
import android.webkit.WebChromeClient;
|
||||
import android.webkit.WebSettings;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.gh.base.BaseActivity;
|
||||
import com.gh.common.util.CheckLoginUtils;
|
||||
import com.gh.common.util.CollectionUtils;
|
||||
import com.gh.common.util.DisplayUtils;
|
||||
import com.gh.common.util.EntranceUtils;
|
||||
import com.gh.common.util.ShareUtils;
|
||||
import com.gh.gamecenter.entity.CommentnumEntity;
|
||||
import com.gh.gamecenter.entity.ConcernEntity;
|
||||
import com.gh.gamecenter.entity.NewsEntity;
|
||||
import com.gh.gamecenter.entity.ToolBoxEntity;
|
||||
import com.gh.gamecenter.entity.UserDataEntity;
|
||||
import com.gh.gamecenter.retrofit.Response;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
import com.google.gson.Gson;
|
||||
import com.jakewharton.rxbinding.view.RxView;
|
||||
import com.tencent.tauth.Tencent;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import butterknife.BindView;
|
||||
import rx.android.schedulers.AndroidSchedulers;
|
||||
import rx.functions.Action1;
|
||||
import rx.schedulers.Schedulers;
|
||||
import com.halo.assistant.fragment.WebFragment;
|
||||
import com.halo.assistant.ui.IntentFactory;
|
||||
|
||||
/**
|
||||
* Created by khy on 2016/10/18.
|
||||
*/
|
||||
public class WebActivity extends BaseActivity implements View.OnClickListener{
|
||||
|
||||
@BindView(R.id.news_webview)
|
||||
WebView webView;
|
||||
@BindView(R.id.web_progressbar)
|
||||
ProgressBar progressBar;
|
||||
@BindView(R.id.web_comment)
|
||||
TextView webComment;
|
||||
@BindView(R.id.news_bottom)
|
||||
RelativeLayout newsBottom;
|
||||
@BindView(R.id.actionbar_tv_title)
|
||||
TextView newsTitle;
|
||||
|
||||
View mShareIv;
|
||||
ImageView mCollectionIv;
|
||||
|
||||
private static final int WEB_COMMENT_REQUEST = 9; // 刷新文章web页面评论数
|
||||
private static final String KEY_ISTOOLS = "isTools";
|
||||
private static final String KEY_ISCOLLECTIONTOOLS = "isCollectionTools";
|
||||
|
||||
private ToolBoxEntity mToolBoxEntity;
|
||||
|
||||
private String newsId;
|
||||
|
||||
private int commentNum;
|
||||
|
||||
private boolean mIsTools;
|
||||
private boolean mIsCollectionTools;
|
||||
@Deprecated
|
||||
public class WebActivity extends CommonActivity {
|
||||
|
||||
@NonNull
|
||||
public static Intent getWebIntent(Context context) {
|
||||
Intent intent = new Intent(context, WebActivity.class);
|
||||
intent.putExtra(EntranceUtils.KEY_GAMENAME, context.getString(R.string.disclaimer_title));
|
||||
intent.putExtra(EntranceUtils.KEY_URL, context.getString(R.string.disclaimer_url));
|
||||
return intent;
|
||||
return new IntentFactory.Builder(context)
|
||||
.setArgs(intent.getExtras())
|
||||
.setActivity(WebActivity.class)
|
||||
.setFragment(WebFragment.class).build();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static void startWebByCollectionTools(Context context, ToolBoxEntity toolBoxEntity, boolean isCollectionTools) {
|
||||
public static Intent getWebByCollectionTools(Context context, ToolBoxEntity toolBoxEntity, boolean isCollectionTools) {
|
||||
Intent intent = new Intent(context, WebActivity.class);
|
||||
intent.putExtra(EntranceUtils.KEY_URL, toolBoxEntity.getUrl());
|
||||
intent.putExtra(KEY_ISTOOLS, true);
|
||||
intent.putExtra(WebFragment.KEY_ISTOOLS, true);
|
||||
// intent.putExtra("gameName", toolBoxEntity.getName());
|
||||
intent.putExtra(ToolBoxEntity.TAG, toolBoxEntity);
|
||||
intent.putExtra(KEY_ISCOLLECTIONTOOLS, isCollectionTools);
|
||||
context.startActivity(intent);
|
||||
intent.putExtra(WebFragment.KEY_ISCOLLECTIONTOOLS, isCollectionTools);
|
||||
return new IntentFactory.Builder(context)
|
||||
.setArgs(intent.getExtras())
|
||||
.setActivity(WebActivity.class)
|
||||
.setFragment(WebFragment.class).build();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@ -106,7 +49,10 @@ public class WebActivity extends BaseActivity implements View.OnClickListener{
|
||||
intent.putExtra(EntranceUtils.KEY_GAMENAME, concernEntity.getGameName());
|
||||
intent.putExtra(EntranceUtils.KEY_NEWSID, concernEntity.getId());
|
||||
intent.putExtra(EntranceUtils.KEY_ENTRANCE, entrance);
|
||||
return intent;
|
||||
return new IntentFactory.Builder(context)
|
||||
.setArgs(intent.getExtras())
|
||||
.setActivity(WebActivity.class)
|
||||
.setFragment(WebFragment.class).build();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@ -116,287 +62,20 @@ public class WebActivity extends BaseActivity implements View.OnClickListener{
|
||||
intent.putExtra(EntranceUtils.KEY_GAMENAME, newsEntity.getGameName());
|
||||
intent.putExtra(EntranceUtils.KEY_NEWSID, newsEntity.getId());
|
||||
intent.putExtra(EntranceUtils.KEY_ENTRANCE, entrance);
|
||||
return intent;
|
||||
return new IntentFactory.Builder(context)
|
||||
.setArgs(intent.getExtras())
|
||||
.setActivity(WebActivity.class)
|
||||
.setFragment(WebFragment.class).build();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static Intent getIntentByUrl(Context context, String url) {
|
||||
Intent intent = new Intent(context, WebActivity.class);
|
||||
intent.putExtra(EntranceUtils.KEY_URL, url);
|
||||
return intent;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getLayoutId() {
|
||||
return R.layout.activity_web;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
// 刷新评论数
|
||||
if (resultCode == WEB_COMMENT_REQUEST && newsId != null && data != null && data.getExtras() != null) {
|
||||
webComment.setText( getString(R.string.web_newscomment_count, data.getExtras().getInt("commentNum")));
|
||||
} else if (requestCode == com.tencent.connect.common.Constants.REQUEST_QQ_SHARE
|
||||
|| requestCode == com.tencent.connect.common.Constants.REQUEST_QZONE_SHARE) {
|
||||
Tencent.onActivityResultData(requestCode, resultCode, data, ShareUtils.getInstance(this).QqShareListener);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
String webUrl;
|
||||
String webTitle;
|
||||
|
||||
mIsTools = getIntent().getBooleanExtra(KEY_ISTOOLS, false);
|
||||
if (getIntent().getBundleExtra(EntranceUtils.KEY_DATA) != null) {
|
||||
Bundle data = getIntent().getBundleExtra(EntranceUtils.KEY_DATA);
|
||||
webUrl = data.getString(EntranceUtils.KEY_URL);
|
||||
webTitle = "";
|
||||
} else {
|
||||
webUrl = getIntent().getStringExtra(EntranceUtils.KEY_URL);
|
||||
webTitle = getIntent().getStringExtra(EntranceUtils.KEY_GAMENAME);
|
||||
newsId = getIntent().getStringExtra(EntranceUtils.KEY_NEWSID);
|
||||
}
|
||||
|
||||
// 增加actionBar Button
|
||||
RelativeLayout reuse_actionbar = findViewById(R.id.reuse_actionbar);
|
||||
mShareIv = LayoutInflater.from(this).inflate(R.layout.menu_action_share, reuse_actionbar, false); // 绑定parentView 否则会丢失宽高
|
||||
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mShareIv.getLayoutParams();
|
||||
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
|
||||
params.addRule(RelativeLayout.CENTER_VERTICAL);
|
||||
reuse_actionbar.addView(mShareIv, params);
|
||||
|
||||
mCollectionIv = (ImageView) LayoutInflater.from(this).inflate(R.layout.menu_action_collection, reuse_actionbar, false);
|
||||
RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) mCollectionIv.getLayoutParams();
|
||||
params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
|
||||
params2.addRule(RelativeLayout.CENTER_VERTICAL);
|
||||
params2.setMargins(0, 0 ,params.width, 0);
|
||||
reuse_actionbar.addView(mCollectionIv, params2);
|
||||
|
||||
|
||||
if (mIsTools) {
|
||||
mToolBoxEntity = getIntent().getParcelableExtra(ToolBoxEntity.TAG);
|
||||
mIsCollectionTools = getIntent().getBooleanExtra(KEY_ISCOLLECTIONTOOLS, false);
|
||||
mShareIv.setVisibility(View.VISIBLE);
|
||||
mCollectionIv.setVisibility(View.VISIBLE);
|
||||
initCollection();
|
||||
} else {
|
||||
mShareIv.setVisibility(View.GONE);
|
||||
mCollectionIv.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
|
||||
setNavigationTitle(webTitle);
|
||||
|
||||
webView.loadUrl(webUrl);
|
||||
|
||||
WebSettings settings = webView.getSettings();
|
||||
settings.setJavaScriptEnabled(true);
|
||||
settings.setSupportZoom(true);
|
||||
|
||||
//用webview打开url
|
||||
webView.setWebViewClient(new WebViewClient() {
|
||||
// @Override
|
||||
// public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
||||
// view.loadUrl(url);
|
||||
// return true;
|
||||
// }
|
||||
|
||||
// @Override
|
||||
// public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
|
||||
// 广告拦截操作
|
||||
// if (!url.contains(xxxx)) {
|
||||
// return new WebResourceResponse(null, null, null);
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
});
|
||||
|
||||
// 页面里的下载跳转到自带浏览器
|
||||
webView.setDownloadListener(new DownloadListener() {
|
||||
@Override
|
||||
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
|
||||
Uri uri = Uri.parse(url);
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
|
||||
startActivity(intent);
|
||||
}
|
||||
});
|
||||
|
||||
//设置加载进度条
|
||||
webView.setWebChromeClient(new WebChromeClient() {
|
||||
|
||||
@Override
|
||||
public void onProgressChanged(WebView view, int newProgress) {
|
||||
progressBar.setProgress(newProgress);
|
||||
if (newProgress == 100) {
|
||||
progressBar.setVisibility(View.GONE);
|
||||
if (newsBottom.getVisibility() == View.GONE && newsId != null) {
|
||||
newsBottom.setVisibility(View.VISIBLE);
|
||||
}
|
||||
} else {
|
||||
if (progressBar.getVisibility() == View.GONE) {
|
||||
progressBar.setVisibility(View.VISIBLE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceivedTitle(WebView view, String title) {
|
||||
super.onReceivedTitle(view, title);
|
||||
if (TextUtils.isEmpty(newsId) && TextUtils.isEmpty(newsTitle.getText().toString()) && !mIsTools) {
|
||||
newsTitle.setEllipsize(TextUtils.TruncateAt.END);
|
||||
newsTitle.setPadding(DisplayUtils.dip2px(getApplication(), 30), 0, DisplayUtils.dip2px(getApplication(), 30), 0);
|
||||
newsTitle.setSingleLine();
|
||||
newsTitle.setText(title);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (newsId != null) {
|
||||
getNewsCommentNum();
|
||||
}
|
||||
|
||||
if (mShareIv != null && mToolBoxEntity != null) {
|
||||
RxView.clicks(mShareIv)
|
||||
.throttleFirst(1, TimeUnit.SECONDS)
|
||||
.subscribe(new Action1<Void>() {
|
||||
@Override
|
||||
public void call(Void aVoid) {
|
||||
showShare(mToolBoxEntity.getUrl(), mToolBoxEntity.getDes(), mToolBoxEntity.getIcon(), mToolBoxEntity.getName(), null, true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (mIsCollectionTools && mToolBoxEntity != null) {
|
||||
getToolsById(); // 对比查看是否修改
|
||||
}
|
||||
|
||||
webComment.setOnClickListener(this);
|
||||
mCollectionIv.setOnClickListener(this);
|
||||
}
|
||||
|
||||
private void initCollection() {
|
||||
if (mToolBoxEntity != null) {
|
||||
mCollectionIv.setVisibility(View.VISIBLE);
|
||||
if (mToolBoxEntity.getUserData() != null && mToolBoxEntity.getUserData().isToolkitFavorite()) {
|
||||
mCollectionIv.setImageResource(R.drawable.detail_collection_select);
|
||||
} else {
|
||||
mCollectionIv.setImageResource(R.drawable.detail_collection_unselect);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void getToolsById() {
|
||||
RetrofitManager.getInstance(this)
|
||||
.getApi()
|
||||
.getToolBoxById(mToolBoxEntity.getId())
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Response<List<ToolBoxEntity>>() {
|
||||
@Override
|
||||
public void onResponse(List<ToolBoxEntity> response) {
|
||||
super.onResponse(response);
|
||||
if (response.size() == 0) return;
|
||||
|
||||
ToolBoxEntity toolBoxEntity = response.get(0);
|
||||
Gson gson = new Gson();
|
||||
|
||||
String newEntity = gson.toJson(toolBoxEntity);
|
||||
String entity = gson.toJson(mToolBoxEntity);
|
||||
if (!newEntity.equals(entity)) {
|
||||
CollectionUtils.INSTANCE.patchCollection(WebActivity.this, toolBoxEntity.getId(), CollectionUtils.CollectionType.toolkit);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void getNewsCommentNum() {
|
||||
RetrofitManager.getInstance(this).getApi()
|
||||
.getNewsCommentnum(newsId)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Response<List<CommentnumEntity>>() {
|
||||
|
||||
@Override
|
||||
public void onNext(List<CommentnumEntity> response) {
|
||||
super.onNext(response);
|
||||
if (response.size() > 0 && response.get(0).getNum() > 0) {
|
||||
commentNum = response.get(0).getNum();
|
||||
webComment.setText("查看评论(" + response.get(0).getNum() + ")");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void onClick(View v) {
|
||||
if (v == webComment) {
|
||||
Intent intent = MessageDetailActivity.getIntentById(this, newsId, commentNum, null, mEntrance + "+(光环浏览器)");
|
||||
startActivityForResult(intent, WEB_COMMENT_REQUEST);
|
||||
} else if (v == mCollectionIv) {
|
||||
CheckLoginUtils.checkLogin(this, new CheckLoginUtils.OnLoggenInListener() {
|
||||
@Override
|
||||
public void onLoggedIn() {
|
||||
mCollectionIv.setEnabled(false);
|
||||
if (mToolBoxEntity.getUserData() != null && mToolBoxEntity.getUserData().isToolkitFavorite()) {
|
||||
CollectionUtils.INSTANCE.deleteCollection(WebActivity.this, mToolBoxEntity.getId(),
|
||||
CollectionUtils.CollectionType.toolkit,
|
||||
new CollectionUtils.OnCollectionListener() {
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
mToolBoxEntity.getUserData().setToolkitFavorite(false);
|
||||
mCollectionIv.setEnabled(true);
|
||||
mCollectionIv.setImageResource(R.drawable.detail_collection_unselect);
|
||||
toast(getString(R.string.collection_cancel));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError() {
|
||||
mCollectionIv.setEnabled(true);
|
||||
toast(getString(R.string.collection_cancel_failure));
|
||||
}
|
||||
});
|
||||
|
||||
} else {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("_id", mToolBoxEntity.getId());
|
||||
CollectionUtils.INSTANCE.postCollection(WebActivity.this, new JSONObject(map).toString(),
|
||||
CollectionUtils.CollectionType.toolkit,
|
||||
new CollectionUtils.OnCollectionListener() {
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
UserDataEntity userData = mToolBoxEntity.getUserData();
|
||||
if (userData == null) {
|
||||
userData = new UserDataEntity();
|
||||
mToolBoxEntity.setUserData(userData);
|
||||
}
|
||||
userData.setToolkitFavorite(true);
|
||||
mCollectionIv.setEnabled(true);
|
||||
mCollectionIv.setImageResource(R.drawable.detail_collection_select);
|
||||
toast(getString(R.string.collection_success));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError() {
|
||||
mCollectionIv.setEnabled(true);
|
||||
toast(getString(R.string.collection_failure));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
||||
if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
|
||||
webView.goBack();// 返回前一个页面
|
||||
return true;
|
||||
}
|
||||
return super.onKeyDown(keyCode, event);
|
||||
return new IntentFactory.Builder(context)
|
||||
.setArgs(intent.getExtras())
|
||||
.setActivity(WebActivity.class)
|
||||
.setFragment(WebFragment.class).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user