package com.idle.wall;

import android.content.Context;
import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.module.AppGlideModule;

// 全局Glide配置
@GlideModule
public class GlideConfig extends AppGlideModule {
    private static final int DISK_CACHE_SIZE = 100 * 1024 * 1024;
    private static final int MEMORY_CACHE_SIZE = (int) (Runtime.getRuntime().maxMemory() / 8);

    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        // 1. 增大磁盘缓存
        builder.setDiskCache(new InternalCacheDiskCacheFactory(context, DISK_CACHE_SIZE));
        // 2. 增大内存缓存
        builder.setMemoryCache(new LruResourceCache(MEMORY_CACHE_SIZE));
        // 3. 降低日志级别，提升性能
        builder.setLogLevel(android.util.Log.ERROR);
    }

    @Override
    public void registerComponents(Context context, Glide glide, Registry registry) {
        // 可添加自定义网络加载器（如OkHttp），提升网络请求速度
        // registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(okHttpClient));
    }

    @Override
    public boolean isManifestParsingEnabled() {
        return false; // 禁用Manifest解析，提升初始化速度
    }
}