Android 利用AOP实现防抖功能

2021-06-29  本文已影响0人  Vitaming
@Aspect
public class ClickAspect {
    
    private static LruCache<Integer, Long> clickCache = new LruCache<>(5);

    @Pointcut("execution(@ClickSingle * * (..)) && @annotation(clickSingle)")
    public void click(ClickSingle clickSingle) {
    }


    @Around("click(clickSingle)")
    public void getClickPoint(ProceedingJoinPoint proceedingJoinPoint, ClickSingle clickSingle) throws Throwable {
        if (clickCache.get(proceedingJoinPoint.getSignature().hashCode()) != null) {
            if (clickCache.get(proceedingJoinPoint.getSignature().hashCode()) + clickSingle.value() <= System.currentTimeMillis()) {
                clickCache.put(proceedingJoinPoint.getSignature().hashCode(), System.currentTimeMillis());
                proceedingJoinPoint.proceed();
            }
        } else {
            clickCache.put(proceedingJoinPoint.getSignature().hashCode(), System.currentTimeMillis());
            proceedingJoinPoint.proceed();
        }
    }
}

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ClickSingle {
    int value() default 150;
}

上一篇下一篇

猜你喜欢

热点阅读