From 47db87935ad9f9fbacaad6e00f66578bc65dd58c Mon Sep 17 00:00:00 2001 From: renhaoting <370797079@qq.com> Date: Thu, 27 Nov 2025 11:57:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E6=9B=B4=E6=96=B0=20homeFrag?= =?UTF-8?q?ment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/gamedog/vididin/VididinEvents.kt | 6 + .../main/fragments/task/WeekStatusView.kt | 23 ++- .../gamedog/vididin/manager/TaskManager.kt | 7 + .../architecture/util/eventbus/NotifyMan.java | 147 ++++++++++++++++++ .../util/eventbus/TPoolAssis.java | 140 +++++++++++++++++ 5 files changed, 319 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/com/gamedog/vididin/VididinEvents.kt create mode 100644 core/architecture/src/main/java/com/ama/core/architecture/util/eventbus/NotifyMan.java create mode 100644 core/architecture/src/main/java/com/ama/core/architecture/util/eventbus/TPoolAssis.java diff --git a/app/src/main/java/com/gamedog/vididin/VididinEvents.kt b/app/src/main/java/com/gamedog/vididin/VididinEvents.kt new file mode 100644 index 0000000..88eca6a --- /dev/null +++ b/app/src/main/java/com/gamedog/vididin/VididinEvents.kt @@ -0,0 +1,6 @@ +package com.gamedog.vididin + +object VididinEvents { + const val Event_Sign_State_Changed = 600 + +} \ No newline at end of file diff --git a/app/src/main/java/com/gamedog/vididin/main/fragments/task/WeekStatusView.kt b/app/src/main/java/com/gamedog/vididin/main/fragments/task/WeekStatusView.kt index 5344213..09aa8eb 100644 --- a/app/src/main/java/com/gamedog/vididin/main/fragments/task/WeekStatusView.kt +++ b/app/src/main/java/com/gamedog/vididin/main/fragments/task/WeekStatusView.kt @@ -10,7 +10,8 @@ import com.gamedog.vididin.R import android.graphics.* import com.ama.core.architecture.BaseApp import com.ama.core.architecture.util.ResUtil -import com.gamedog.vididin.manager.DailySignDayInfoBean +import com.ama.core.architecture.util.eventbus.NotifyMan +import com.gamedog.vididin.VididinEvents import com.gamedog.vididin.manager.TaskManager @@ -55,12 +56,17 @@ class WeekStatusView @JvmOverloads constructor( } + private val mEventCallback: NotifyMan.ICallback = object : NotifyMan.ICallback(true) { + override fun onEvent(data: NotifyMan.NotifyData<*>?) { + invalidate() + } + } init { setupAttributes(attrs) - initLoadDatas() + initDatas() } private fun setupAttributes(attrs: AttributeSet?) { @@ -81,10 +87,20 @@ class WeekStatusView @JvmOverloads constructor( typedArray.recycle() } - private fun initLoadDatas() { + private fun initDatas() { } + override fun onAttachedToWindow() { + super.onAttachedToWindow() + NotifyMan.instance().register(mEventCallback, VididinEvents.Event_Sign_State_Changed) + } + + override fun onDetachedFromWindow() { + super.onDetachedFromWindow() + NotifyMan.instance().unregister(mEventCallback) + } + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { val desiredWidth = MeasureSpec.getSize(widthMeasureSpec) val desiredHeight = (circleRadius * 2 + dayTextSize + rewardTextSize + vertiMagin * 2 + 2*componentGap).toInt() @@ -131,7 +147,6 @@ class WeekStatusView @JvmOverloads constructor( } - fun getBitmap(resId: Int): Bitmap? { return try { BitmapFactory.decodeResource(BaseApp.appContext().resources, resId) diff --git a/app/src/main/java/com/gamedog/vididin/manager/TaskManager.kt b/app/src/main/java/com/gamedog/vididin/manager/TaskManager.kt index c0f8241..df86d1a 100644 --- a/app/src/main/java/com/gamedog/vididin/manager/TaskManager.kt +++ b/app/src/main/java/com/gamedog/vididin/manager/TaskManager.kt @@ -4,6 +4,8 @@ import android.R import com.ama.core.architecture.util.DateUtil import com.ama.core.architecture.util.FileUtil import com.ama.core.architecture.util.SpUtil +import com.ama.core.architecture.util.eventbus.NotifyMan +import com.gamedog.vididin.VididinEvents import com.gamedog.vididin.main.fragments.task.Task import com.gamedog.vididin.main.fragments.task.TaskBean import com.google.gson.GsonBuilder @@ -183,11 +185,16 @@ class TaskManager private constructor() { daySignState.hasSigned = true daySignState.hasWatchedAd = isByAd saveDailySignInfo() + notifySignStateChanged(Pair(dayIndex, daySignState)) return true } return false } + private fun notifySignStateChanged(dataPair: Pair) { + NotifyMan.instance().sendEvent(VididinEvents.Event_Sign_State_Changed, NotifyMan.NotifyData(dataPair)) + } + private fun saveDailySignInfo() { SpUtil.instance().putObject(SpUtil.KEY_DAILY_SIGN, mDailySignData) } diff --git a/core/architecture/src/main/java/com/ama/core/architecture/util/eventbus/NotifyMan.java b/core/architecture/src/main/java/com/ama/core/architecture/util/eventbus/NotifyMan.java new file mode 100644 index 0000000..681a4f4 --- /dev/null +++ b/core/architecture/src/main/java/com/ama/core/architecture/util/eventbus/NotifyMan.java @@ -0,0 +1,147 @@ +package com.ama.core.architecture.util.eventbus; + + + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + + +/** + * private void test() { + * register(new ICallback() { + * @Override + * public void onEvent(NotifyData data) { + * + * } + * }); + * + * sendEvent(1111, new NotifyData("aaaa")); + * } + */ +public class NotifyMan { + private static final NotifyMan instance = new NotifyMan(); + private final String mTpTag; + private Map> mAllTypeObserverMap = new ConcurrentHashMap(); + + + + + private NotifyMan() { + mTpTag = NotifyMan.class.getSimpleName() + "-" + hashCode(); + TPoolAssis.instance().configMyTPool(mTpTag, 10); + } + + public static NotifyMan instance() { + return instance; + } + + public void sendEvent(int eventType, NotifyData eventData) { + Map listeners = mAllTypeObserverMap.get(eventType); + if (listeners != null && listeners.size() > 0) { + if (eventData == null) { + eventData = new NotifyData(); + } + + eventData.mEventType = eventType; + + for (ICallback listener : listeners.keySet()) { + NotifyData finalEventData = eventData; + Runnable runnable = new Runnable() { + @Override + public void run() { + try { + TPoolAssis.instance().runMain(new Runnable() { + @Override + public void run() { + listener.onEvent(finalEventData); + } + }); + + } catch (Exception e) { + e.printStackTrace(); + } + } + }; + + if (listener.mNotifyInMainThread) { + TPoolAssis.instance().runMain(runnable); + } else { + TPoolAssis.instance().runBg(runnable); + } + + } + } + } + + + + /** + * @param listener + * @param eventTypes + * @return + */ + public void register(ICallback listener, int... eventTypes) { + if (eventTypes != null && eventTypes.length > 0 && listener != null) { + for (int i=0; i curTypeListenerList = mAllTypeObserverMap.get(type); + if (curTypeListenerList == null) { + curTypeListenerList = new ConcurrentHashMap<>(); + mAllTypeObserverMap.put(type, curTypeListenerList); + } + + + if (!curTypeListenerList.containsKey(listener)) { + curTypeListenerList.put(listener, 1); + } + } + } + } + + public void unregister(ICallback listener) { + TPoolAssis.instance().runBg(new Runnable() { + @Override + public void run() { + try { + for (Map.Entry> entry : mAllTypeObserverMap.entrySet()) { + entry.getValue().remove(listener); + } + } catch (Exception e) { + e.printStackTrace(); + } + + } + }); + + } + + + //---------------------- Inner classes ---------------------- + public static class NotifyData { + public int mEventType = -123; + public T mData; + + public NotifyData() { + } + + public NotifyData(T data) { + mData = data; + } + + public NotifyData(int type) { + mEventType = type; + } + } + + public static abstract class ICallback { + public boolean mNotifyInMainThread = false; + + public ICallback(boolean notifyInMainThread) { + mNotifyInMainThread = notifyInMainThread; + } + public abstract void onEvent(NotifyData data); + } + + +} \ No newline at end of file diff --git a/core/architecture/src/main/java/com/ama/core/architecture/util/eventbus/TPoolAssis.java b/core/architecture/src/main/java/com/ama/core/architecture/util/eventbus/TPoolAssis.java new file mode 100644 index 0000000..90c9cc2 --- /dev/null +++ b/core/architecture/src/main/java/com/ama/core/architecture/util/eventbus/TPoolAssis.java @@ -0,0 +1,140 @@ +package com.ama.core.architecture.util.eventbus; + + +import android.os.Handler; +import android.os.Looper; +import android.text.TextUtils; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +public class TPoolAssis { + private static final String COMMON_TP_NAME = "common_tp_name"; + private static final int COMMON_TP_THREAD_COUNT = 5; + private static final int DEFAULT_TP_THREAD_COUNT = 1; + private static TPoolAssis instance; + + + private Map mTPoolMap; + private Handler mMainHandler = null; + + + public static TPoolAssis instance() { + if (instance == null) { + synchronized (TPoolAssis.class) { + if (instance == null) { + instance = new TPoolAssis(); + } + } + } + return instance; + } + + + private TPoolAssis() { + mTPoolMap = new ConcurrentHashMap<>(); + createTPool(COMMON_TP_NAME, COMMON_TP_THREAD_COUNT); + } + + private void createTPool(String tpName, int threadCount) { + if (!TextUtils.isEmpty(tpName) && !mTPoolMap.containsKey(tpName)) { + LinkedBlockingQueue taskQueue = new LinkedBlockingQueue<>(); + ThreadPoolExecutor tPoolExecutor = new ThreadPoolExecutor( + threadCount, threadCount, 30, + TimeUnit.SECONDS, taskQueue); + mTPoolMap.put(tpName, new TPoolEntity(tpName, tPoolExecutor, taskQueue)); + } + } + + public void configMyTPool(String tpName, int threadCount) { + if (TextUtils.isEmpty(tpName) + && !tpName.equalsIgnoreCase(COMMON_TP_NAME) + && !mTPoolMap.containsKey(tpName)) { + createTPool(tpName, threadCount); + } + } + + public void runBg(Runnable runnable) { + runBg(COMMON_TP_NAME, runnable); + } + + public void runBg(String tpName, Runnable runnable) { + if (!TextUtils.isEmpty(tpName) && runnable != null) { + if (!mTPoolMap.containsKey(tpName)) { + createTPool(tpName, DEFAULT_TP_THREAD_COUNT); + } + + TPoolEntity tPool = mTPoolMap.get(tpName); + if (tPool != null) { + tPool.mTPoolExecutor.execute(new TPRunnable(runnable)); + } + } + } + + public int getBlockTaskCount(String tpName) { + if (!TextUtils.isEmpty(tpName) && mTPoolMap.containsKey(tpName)) { + return mTPoolMap.get(tpName).getBlockTaskCount(); + } + return 0; + } + + public void clearAllTaskByTag(String tpName) { + if (!TextUtils.isEmpty(tpName) && mTPoolMap.containsKey(tpName)) { + mTPoolMap.get(tpName).mTaskQueue.clear(); + } + } + + + public void runMain(Runnable runnable) { + if (runnable != null) { + if (mMainHandler == null) { + synchronized (this) { + mMainHandler = new Handler(Looper.getMainLooper()); + } + } + mMainHandler.post(runnable); + } + } + + + //****************** Inner classes ******************// + private static class TPoolEntity { + public String mTpName; + public ThreadPoolExecutor mTPoolExecutor; + public LinkedBlockingQueue mTaskQueue; + + public int getBlockTaskCount() { + if (mTaskQueue != null) { + return mTaskQueue.size(); + } + return 0; + } + + public TPoolEntity(String tpName, ThreadPoolExecutor mTPoolExecutor, + LinkedBlockingQueue mTaskQueue) { + this.mTpName = tpName; + this.mTPoolExecutor = mTPoolExecutor; + this.mTaskQueue = mTaskQueue; + } + } + + + public static class TPRunnable implements Runnable { + private Runnable mRealRunnable; + public TPRunnable(Runnable runnable) { + mRealRunnable = runnable; + } + + @Override + public void run() { + try { + mRealRunnable.run(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + +} \ No newline at end of file