新增【弹框功能优化】

This commit is contained in:
zlzw 2023-09-15 10:04:02 +08:00
parent d1edf6bdd8
commit d383a08013
15 changed files with 622 additions and 19 deletions

View File

@ -0,0 +1,150 @@
package com.yunbao.common.bean;
import com.google.gson.annotations.SerializedName;
import com.yunbao.common.CommonAppConfig;
import com.yunbao.common.utils.StringUtil;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class OpenAdModel extends BaseModel {
public static final int TYPE_HOME = 1;//主页
public static final int TYPE_LIVE = 2;//直播间
public static final int TYPE_LIVE_DELAY = 3;//直播间延迟
public static final int MODEL_SQUARE = 1;//正方形
public static final int MODEL_RECTANGLE = 2;//长方形
public static final int MODEL_BOTTOM = 3;//底部
@SerializedName("id")
private int id;
@SerializedName("popup_location")
private int type = TYPE_HOME;
@SerializedName("activity_url")
private String url;
@SerializedName("image_url")
private String imageUrl;
@SerializedName("display_time")
private int showTime; //持续展示时间
@SerializedName("delay_show_time")
private int delayShowTime;//延迟展示时间
@SerializedName("popup_model")
private int model = MODEL_SQUARE;
@SerializedName("start_show_time")
private String startTime;//活动开始时间
@SerializedName("end_show_time")
private String endTime;//活动结束时间
public OpenAdModel() {
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getUrl() {
if (url.startsWith("http://") || url.startsWith("https://")) {
return CommonAppConfig.HOST + "/" + url;
}
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getShowTime() {
return showTime * 1000;
}
public void setShowTime(int showTime) {
this.showTime = showTime;
}
public int getDelayShowTime() {
return delayShowTime * 1000;
}
public void setDelayShowTime(int delayShowTime) {
this.delayShowTime = delayShowTime;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public int getModel() {
return model;
}
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public void setModel(int model) {
this.model = model;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public boolean isInTime() {
if (StringUtil.isEmpty(startTime, endTime)) {
return true;
}
Date startTime = null;
Date endTime = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
try {
startTime = sdf.parse(this.startTime);
endTime = sdf.parse(this.endTime);
} catch (ParseException e) {
e.printStackTrace();
}
long time = System.currentTimeMillis();
return startTime.getTime() <= time && time <= endTime.getTime();
}
@Override
public String toString() {
return "OpenAdModel{" +
"id=" + id +
", type=" + type +
", url='" + url + '\'' +
", imageUrl='" + imageUrl + '\'' +
", showTime=" + showTime +
", delayShowTime=" + delayShowTime +
", model=" + model +
", startTime='" + startTime + '\'' +
", endTime='" + endTime + '\'' +
'}';
}
}

View File

@ -0,0 +1,39 @@
package com.yunbao.common.dialog;
import android.content.Context;
import androidx.annotation.NonNull;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.impl.FullScreenPopupView;
/**
* 居中弹窗
*/
public abstract class AbsDialogFullScreenPopupWindow extends FullScreenPopupView {
public final Context mContext;
public AbsDialogFullScreenPopupWindow(@NonNull Context context) {
super(context);
this.mContext = context;
}
/**
* <a href="https://github.com/li-xiaojun/XPopup/wiki/5.-%E5%B8%B8%E7%94%A8%E8%AE%BE%E7%BD%AE">参考配置</a>
*/
public abstract void buildDialog(XPopup.Builder builder);
public abstract int bindLayoutId();
@Override
protected int getImplLayoutId() {
return bindLayoutId();
}
public void showDialog() {
XPopup.Builder builder = new XPopup.Builder(mContext);
builder.isDestroyOnDismiss(true);
builder.enableDrag(false);
buildDialog(builder);
builder.asCustom(this).show();
}
}

View File

@ -0,0 +1,70 @@
package com.yunbao.common.dialog;
import android.content.Context;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import com.lxj.xpopup.XPopup;
import com.yunbao.common.R;
import com.yunbao.common.activity.WebViewActivity;
import com.yunbao.common.bean.OpenAdModel;
import com.yunbao.common.glide.ImgLoader;
import com.yunbao.common.interfaces.OnItemClickListener;
public class OpenAdBottomDialogPopup extends AbsDialogPopupWindow {
private ImageView mImageView;
private ImageView mClose;
private OpenAdModel model;
private OnItemClickListener<OpenAdModel> mListener;
public OpenAdBottomDialogPopup(@NonNull Context context, OpenAdModel model) {
super(context);
this.model = model;
}
@Override
public void buildDialog(XPopup.Builder builder) {
}
@Override
public int bindLayoutId() {
return R.layout.dialog_open_bottom_ad;
}
@Override
public void dismiss() {
super.dismiss();
if (mListener != null) {
mListener.onItemClick(model, 0);
mListener = null;
}
}
public OpenAdBottomDialogPopup setListener(OnItemClickListener<OpenAdModel> mListener) {
this.mListener = mListener;
return this;
}
@Override
protected void onCreate() {
super.onCreate();
mImageView = findViewById(R.id.img);
mClose = findViewById(R.id.close);
mImageView.setOnClickListener(v -> {
WebViewActivity.forward(mContext, model.getUrl(), model.getType() != OpenAdModel.TYPE_HOME);
if (mListener != null) {
mListener.onItemClick(model, 1);
mListener = null;
}
dismiss();
});
mClose.setOnClickListener(v -> dismiss());
ImgLoader.display(mContext, model.getImageUrl(), mImageView);
if (model.getShowTime() > 0) {
mClose.postDelayed(this::dismiss, model.getShowTime());
}
}
}

View File

@ -0,0 +1,82 @@
package com.yunbao.common.dialog;
import android.content.Context;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.lxj.xpopup.XPopup;
import com.yunbao.common.R;
import com.yunbao.common.activity.WebViewActivity;
import com.yunbao.common.bean.OpenAdModel;
import com.yunbao.common.glide.ImgLoader;
import com.yunbao.common.interfaces.OnItemClickListener;
import com.yunbao.common.utils.DpUtil;
import com.yunbao.common.utils.ScreenDimenUtil;
public class OpenAdCenterDialogPopup extends AbsDialogFullScreenPopupWindow {
private ImageView mImageView;
private ImageView mClose;
private OpenAdModel model;
private OnItemClickListener<OpenAdModel> mListener;
public OpenAdCenterDialogPopup(@NonNull Context context, OpenAdModel model) {
super(context);
this.model = model;
}
public OpenAdCenterDialogPopup setListener(OnItemClickListener<OpenAdModel> mListener) {
this.mListener = mListener;
return this;
}
@Override
public void buildDialog(XPopup.Builder builder) {
}
@Override
public int bindLayoutId() {
return R.layout.dialog_open_center_ad;
}
@Override
public void dismiss() {
super.dismiss();
if (mListener != null) {
mListener.onItemClick(model, 0);
mListener = null;
}
}
@Override
protected void onCreate() {
super.onCreate();
mImageView = findViewById(R.id.img);
mClose = findViewById(R.id.close);
findViewById(R.id.layout).setOnClickListener(v -> dismiss());
mImageView.setOnClickListener(v -> {
WebViewActivity.forward(mContext, model.getUrl(), model.getType() != OpenAdModel.TYPE_HOME);
if (mListener != null) {
mListener.onItemClick(model, 1);
mListener = null;
}
dismiss();
});
mClose.setOnClickListener(v -> dismiss());
ImgLoader.display(mContext, model.getImageUrl(), mImageView);
int width = ScreenDimenUtil.getInstance().getScreenWdith() - DpUtil.dp2px(40);
int height = (int) (width * 1.4);
if (model.getModel() == OpenAdModel.MODEL_SQUARE) {
height = width;
}
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) mImageView.getLayoutParams();
params.width = width;
params.height = height;
mImageView.setLayoutParams(params);
if (model.getShowTime() > 0) {
mClose.postDelayed(this::dismiss, model.getShowTime());
}
}
}

View File

@ -40,6 +40,7 @@ import com.yunbao.common.bean.MsgSwitchDetailModel;
import com.yunbao.common.bean.NewPeopleInfo;
import com.yunbao.common.bean.NobleRankHideUserListModel;
import com.yunbao.common.bean.NobleTrumpetModel;
import com.yunbao.common.bean.OpenAdModel;
import com.yunbao.common.bean.PkRankBean;
import com.yunbao.common.bean.PrankGiftBean;
import com.yunbao.common.bean.PrankHttpTurntableBean;
@ -969,5 +970,11 @@ public interface PDLiveApi {
@GET("/api/public/?service=gift.getGiftNamingInfo")
Observable<ResponseModel<GiftNamingInfoModel>> getGiftNamingInfo(@Query("gift_id") String giftId);
/**
* 活动弹窗
*/
@GET("/api/public/?service=Home.activityPopup")
Observable<ResponseModel<List<OpenAdModel>>> activityPopup();
}

View File

@ -39,6 +39,7 @@ import com.yunbao.common.bean.LiveUserMailBoxModel;
import com.yunbao.common.bean.MedalAchievementModel;
import com.yunbao.common.bean.NobleRankHideUserListModel;
import com.yunbao.common.bean.NobleTrumpetModel;
import com.yunbao.common.bean.OpenAdModel;
import com.yunbao.common.bean.PkRankBean;
import com.yunbao.common.bean.PrankGiftBean;
import com.yunbao.common.bean.PrankHttpTurntableBean;
@ -2194,6 +2195,29 @@ public class LiveNetManager {
}).isDisposed();
}
public void activityPopup(HttpCallback<List<OpenAdModel>> callback) {
API.get().pdLiveApi(mContext)
.activityPopup()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<ResponseModel<List<OpenAdModel>>>() {
@Override
public void accept(ResponseModel<List<OpenAdModel>> listResponseModel) throws Exception {
if (callback != null) {
callback.onSuccess(listResponseModel.getData().getInfo());
}
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
throwable.printStackTrace();
if (callback != null) {
callback.onError(mContext.getString(R.string.net_error));
}
}
}).isDisposed();
}
/**
* 直播间取消网络请求
*/

View File

@ -0,0 +1,164 @@
package com.yunbao.common.manager;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import com.yunbao.common.CommonAppContext;
import com.yunbao.common.bean.OpenAdModel;
import com.yunbao.common.dialog.OpenAdBottomDialogPopup;
import com.yunbao.common.dialog.OpenAdCenterDialogPopup;
import com.yunbao.common.http.base.HttpCallback;
import com.yunbao.common.http.live.LiveNetManager;
import com.yunbao.common.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class OpenAdManager {
public static final int TYPE_HOME = 1;
public static final int TYPE_LIVE = 2;
private static final String TAG = "-----弹窗-----";
private Map<Integer, Boolean> showMap;
private List<OpenAdModel> list = new ArrayList<>();
private Handler handler = new Handler(Looper.getMainLooper());
private Map<Integer, AdRunnable> runnableMap = new HashMap<>();
private int showType;
private OpenAdManager() {
showMap = new HashMap<>();
init(false);
}
public void reset() {
for (Integer integer : runnableMap.keySet()) {
handler.removeCallbacks(Objects.requireNonNull(runnableMap.get(integer)));
}
runnableMap.clear();
showMap.clear();
list.clear();
}
private static final class MInstanceHolder {
static final OpenAdManager mInstance = new OpenAdManager();
}
public static OpenAdManager getInstance() {
return MInstanceHolder.mInstance;
}
private void init(boolean isShow) {
LiveNetManager.get(CommonAppContext.getTopActivity())
.activityPopup(new HttpCallback<List<OpenAdModel>>() {
@Override
public void onSuccess(List<OpenAdModel> data) {
list = data;
if (isShow) {
show(TYPE_HOME);
}
}
@Override
public void onError(String error) {
System.err.println("弹框列表:" + error);
}
});
}
public synchronized void show(int type) {
if (list.isEmpty()) {
init(true);
return;
}
showType=type;
for (OpenAdModel model : list) {
if (model.getType() == type) {
if (runnableMap.containsKey(model.getId())) {
AdRunnable runnable = runnableMap.get(model.getId());
if (runnable != null) {
Log.d(TAG, "reset: "+model);
handler.removeCallbacks(runnable);
runnableMap.remove(model.getId());
}
}
if (!isShow(model)) {
Log.i(TAG, "show: "+model);
handler.postDelayed(new AdRunnable(model), model.getDelayShowTime());
}else{
Log.i(TAG, "notshow: "+model);
}
}
}
Log.i(TAG, "------------------------------");
}
public synchronized void dismiss() {
Log.d(TAG, "准备dismiss:"+runnableMap.size() );
for (Integer model : runnableMap.keySet()) {
AdRunnable runnable = runnableMap.get(model);
Log.d(TAG, "dismiss:"+runnable);
if (runnable != null) {
runnable.dismiss();
handler.removeCallbacks(runnable);
}
}
runnableMap.clear();
}
private synchronized boolean isShow(OpenAdModel type) {
if (showMap.containsKey(type.getId()) && showMap.get(type.getId()) != null) {
return Boolean.TRUE.equals(showMap.get(type.getId()));
}
return false;
}
private class AdRunnable implements Runnable {
OpenAdModel model;
public AdRunnable(OpenAdModel model) {
this.model = model;
runnableMap.put(model.getId(), this);
}
public void dismiss(){
Log.e(TAG, "dismiss: "+model );
}
@Override
public synchronized void run() {
Log.i(TAG, "run: "+model);
if (model == null) {
ToastUtil.showDebug("model为空");
return;
}
if (!model.isInTime()) {
ToastUtil.showDebug("不在展示时间内:" + model.getStartTime() + "|" + model.getEndTime());
return;
}
if (isShow(model)) {
ToastUtil.showDebug(model.getId() + "|model展示过了");
return;
}
if(model.getType()!=showType){
return;
}
showMap.put(model.getId(), true);
if (model.getModel() == OpenAdModel.MODEL_BOTTOM) {
new OpenAdBottomDialogPopup(CommonAppContext.getTopActivity(), model)
.setListener((bean, position) -> {
})
.showDialog();
} else {
new OpenAdCenterDialogPopup(CommonAppContext.getTopActivity(), model)
.setListener((bean, position) -> {
})
.showDialog();
}
}
}
}

View File

@ -80,7 +80,7 @@ public class ToastUtil {
}
public static void showDebug(String s){
if(BuildConfig.DEBUG){
show(s);
show("开发模式:"+s);
}
}
public static void showDebug(int s){

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@mipmap/background_gift_wall" />
<ImageView
android:id="@+id/close"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginEnd="14dp"
android:layout_marginBottom="12dp"
app:layout_constraintBottom_toTopOf="@+id/img"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@mipmap/ic_open_ad_close" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="12dp"
android:scaleType="fitEnd"
app:layout_constraintBottom_toTopOf="@+id/close"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@drawable/type1" />
<ImageView
android:id="@+id/close"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginBottom="202dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/img"
app:layout_constraintStart_toStartOf="@+id/img"
app:srcCompat="@mipmap/ic_open_ad_close" />
</androidx.constraintlayout.widget.ConstraintLayout>

Binary file not shown.

After

Width:  |  Height:  |  Size: 807 B

View File

@ -76,6 +76,7 @@ import com.yunbao.common.http.live.LiveNetManager;
import com.yunbao.common.http.main.MainNetManager;
import com.yunbao.common.interfaces.CommonCallback;
import com.yunbao.common.manager.IMLoginManager;
import com.yunbao.common.manager.OpenAdManager;
import com.yunbao.common.pay.PayCallback;
import com.yunbao.common.pay.PayPresenter;
import com.yunbao.common.utils.Bus;
@ -560,6 +561,7 @@ public class LiveAudienceActivity extends LiveActivity {
@Override
public void onBackPressed() {
MobclickAgent.onEvent(mContext, "live_room_close", "退出直播间");
OpenAdManager.getInstance().dismiss();
try {
manager.onBackPressed();
if (MicStatusManager.getInstance().getMicStatus() == MicStatusManager.MIC_TYPE_REQUEST) {
@ -776,6 +778,7 @@ public class LiveAudienceActivity extends LiveActivity {
}
}
GiftCacheUtil.getInstance().restart();
new Handler(Looper.getMainLooper()).postDelayed(() -> OpenAdManager.getInstance().show(OpenAdManager.TYPE_LIVE), 400);
}
@Override

View File

@ -59,6 +59,7 @@ import com.yunbao.common.http.HttpCallback;
import com.yunbao.common.http.HttpClient;
import com.yunbao.common.http.live.LiveNetManager;
import com.yunbao.common.manager.IMLoginManager;
import com.yunbao.common.manager.OpenAdManager;
import com.yunbao.common.utils.AppManager;
import com.yunbao.common.utils.Bus;
import com.yunbao.common.utils.DialogUitl;
@ -262,6 +263,7 @@ public class PortraitLiveManager implements LivePlayListener, SocketMessageListe
public void run() {
loading.setVisibility(View.GONE);
enterRoomLeaveHandler.post(enterRoomLeaveRunnable);
OpenAdManager.getInstance().show(OpenAdManager.TYPE_LIVE);
}
};
final Runnable loadTimeoutRunnableGone = new Runnable() {
@ -293,7 +295,7 @@ public class PortraitLiveManager implements LivePlayListener, SocketMessageListe
mLiveRyLinkMicPkPresenter.release();
mLiveRyLinkMicPkPresenter = null;
}
OpenAdManager.getInstance().dismiss();
mLiveBean = data;
mLiveSDK = liveSdk;
mLiveType = liveType;

View File

@ -88,6 +88,7 @@ import com.yunbao.common.interfaces.CommonCallback;
import com.yunbao.common.manager.APKManager;
import com.yunbao.common.manager.IMLoginManager;
import com.yunbao.common.manager.NoviceInstructorManager;
import com.yunbao.common.manager.OpenAdManager;
import com.yunbao.common.manager.imrongcloud.RongcloudIMManager;
import com.yunbao.common.utils.DialogUitl;
import com.yunbao.common.utils.DpUtil;
@ -210,13 +211,14 @@ public class MainActivity extends AbsActivity implements MainAppBarLayoutListene
@Override
protected void onPause() {
super.onPause();
OpenAdManager.getInstance().dismiss();
}
@Override
protected void main() {
ActivityCompat.postponeEnterTransition(this);
ConversationIMListManager.get(this);
OpenAdManager.getInstance();
//在请求一下这个接口给我后台版本号
CommonHttpUtil.getConfig(mContext, new CommonCallback<ConfigBean>() {
@Override
@ -526,7 +528,6 @@ public class MainActivity extends AbsActivity implements MainAppBarLayoutListene
//获取指导员账号
ConversationIMListManager.get(this).getUserInstructor(this);
checkVersion();
}
@Override
@ -875,7 +876,7 @@ public class MainActivity extends AbsActivity implements MainAppBarLayoutListene
NoviceInstructorManager.get(mContext).getNoviceInstructor();
NoviceInstructorManager.get(mContext).checktHomeZdyPop();
initAnchorRecommendBanner();
OpenAdManager.getInstance().show(OpenAdManager.TYPE_HOME);
}
/**
@ -1074,6 +1075,7 @@ public class MainActivity extends AbsActivity implements MainAppBarLayoutListene
if (EasyFloat.isShow("LiveFloatView")) {
EasyFloat.dismiss("LiveFloatView", true);
}
OpenAdManager.getInstance().reset();
super.onBackPressed();
}
}