同步更新 homeFragment

This commit is contained in:
renhaoting 2025-11-27 11:57:56 +08:00
parent 1f0d4c9f0b
commit 47db87935a
5 changed files with 319 additions and 4 deletions

View File

@ -0,0 +1,6 @@
package com.gamedog.vididin
object VididinEvents {
const val Event_Sign_State_Changed = 600
}

View File

@ -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)

View File

@ -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<Int, DailySignDayInfoBean>) {
NotifyMan.instance().sendEvent(VididinEvents.Event_Sign_State_Changed, NotifyMan.NotifyData(dataPair))
}
private fun saveDailySignInfo() {
SpUtil.instance().putObject(SpUtil.KEY_DAILY_SIGN, mDailySignData)
}

View File

@ -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<Integer, Map<ICallback, Integer>> 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<ICallback, Integer> 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<eventTypes.length; i++) {
int type = eventTypes[i];
Map<ICallback, Integer> 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<Integer, Map<ICallback, Integer>> entry : mAllTypeObserverMap.entrySet()) {
entry.getValue().remove(listener);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//---------------------- Inner classes ----------------------
public static class NotifyData<T> {
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);
}
}

View File

@ -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<String, TPoolEntity> 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<? extends Runnable> mTaskQueue;
public int getBlockTaskCount() {
if (mTaskQueue != null) {
return mTaskQueue.size();
}
return 0;
}
public TPoolEntity(String tpName, ThreadPoolExecutor mTPoolExecutor,
LinkedBlockingQueue<? extends Runnable> 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();
}
}
}
}