diff --git a/app/src/main/java/com/huanchengfly/tieba/post/activities/TranslucentThemeActivity.kt b/app/src/main/java/com/huanchengfly/tieba/post/activities/TranslucentThemeActivity.kt index ff164a04..fcf4c855 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/activities/TranslucentThemeActivity.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/activities/TranslucentThemeActivity.kt @@ -18,6 +18,7 @@ import androidx.annotation.ColorInt import androidx.core.content.ContextCompat import androidx.core.text.HtmlCompat import androidx.palette.graphics.Palette +import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import butterknife.BindView import com.bumptech.glide.Glide @@ -26,21 +27,19 @@ import com.bumptech.glide.request.RequestOptions import com.bumptech.glide.request.target.CustomTarget import com.bumptech.glide.request.transition.Transition import com.gyf.immersionbar.ImmersionBar -import com.huanchengfly.tieba.post.BaseApplication +import com.huanchengfly.tieba.post.* import com.huanchengfly.tieba.post.BaseApplication.Companion.translucentBackground -import com.huanchengfly.tieba.post.R import com.huanchengfly.tieba.post.adapters.ThemeColorAdapter +import com.huanchengfly.tieba.post.adapters.WallpaperAdapter +import com.huanchengfly.tieba.post.api.LiteApi +import com.huanchengfly.tieba.post.api.retrofit.doIfSuccess import com.huanchengfly.tieba.post.components.MyImageEngine import com.huanchengfly.tieba.post.components.MyLinearLayoutManager +import com.huanchengfly.tieba.post.components.dividers.HorizontalSpacesDecoration import com.huanchengfly.tieba.post.components.transformations.BlurTransformation -import com.huanchengfly.tieba.post.enableChangingLayoutTransition import com.huanchengfly.tieba.post.interfaces.OnItemClickListener -import com.huanchengfly.tieba.post.toastShort import com.huanchengfly.tieba.post.ui.theme.utils.ThemeUtils -import com.huanchengfly.tieba.post.utils.ColorUtils -import com.huanchengfly.tieba.post.utils.ImageUtil -import com.huanchengfly.tieba.post.utils.PermissionUtil -import com.huanchengfly.tieba.post.utils.ThemeUtil +import com.huanchengfly.tieba.post.utils.* import com.huanchengfly.tieba.post.utils.ThemeUtil.TRANSLUCENT_THEME_DARK import com.huanchengfly.tieba.post.utils.ThemeUtil.TRANSLUCENT_THEME_LIGHT import com.huanchengfly.tieba.post.widgets.theme.TintMaterialButton @@ -51,6 +50,8 @@ import com.yanzhenjie.permission.Action import com.yanzhenjie.permission.runtime.Permission import com.zhihu.matisse.Matisse import com.zhihu.matisse.MimeType +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.launch import java.io.File class TranslucentThemeActivity : BaseActivity(), View.OnClickListener, OnSeekBarChangeListener, ColorPickerDialogListener { @@ -62,6 +63,12 @@ class TranslucentThemeActivity : BaseActivity(), View.OnClickListener, OnSeekBar @BindView(R.id.select_color) lateinit var mSelectColor: View + @BindView(R.id.recommend_wallpapers) + lateinit var recommendWallpapers: View + + @BindView(R.id.wallpapers_rv) + lateinit var recommendWallpapersRv: RecyclerView + @BindView(R.id.progress) lateinit var mProgress: View @@ -80,41 +87,85 @@ class TranslucentThemeActivity : BaseActivity(), View.OnClickListener, OnSeekBar @BindView(R.id.color_theme) lateinit var colorTheme: ViewGroup - private var mAdapter: ThemeColorAdapter? = null + var wallpapers: List? = null + set(value) { + field = value + refreshWallpapers() + } + private val wallpaperAdapter: WallpaperAdapter by lazy { WallpaperAdapter(this) } + + private val themeColorAdapter: ThemeColorAdapter by lazy { ThemeColorAdapter(this) } + + private fun launchUCrop(sourceUri: Uri) { + mProgress.visibility = View.VISIBLE + Glide.with(this) + .asBitmap() + .load(sourceUri) + .into(object : CustomTarget() { + override fun onLoadCleared(placeholder: Drawable?) {} + + override fun onResourceReady(resource: Bitmap, transition: Transition?) { + mProgress.visibility = View.GONE + val file = + ImageUtil.bitmapToFile(resource, File(cacheDir, "origin_background.jpg")) + val sourceFileUri = Uri.fromFile(file) + val destUri = Uri.fromFile(File(filesDir, "cropped_background.jpg")) + val height = BaseApplication.ScreenInfo.EXACT_SCREEN_HEIGHT.toFloat() + val width = BaseApplication.ScreenInfo.EXACT_SCREEN_WIDTH.toFloat() + UCrop.of(sourceFileUri, destUri) + .withAspectRatio(width / height, 1f) + .withOptions(UCrop.Options().apply { + setShowCropFrame(true) + setShowCropGrid(true) + setStatusBarColor( + ColorUtils.getDarkerColor( + ThemeUtils.getColorByAttr( + this@TranslucentThemeActivity, + R.attr.colorPrimary + ) + ) + ) + setToolbarColor( + ThemeUtils.getColorByAttr( + this@TranslucentThemeActivity, + R.attr.colorPrimary + ) + ) + setToolbarWidgetColor( + ThemeUtils.getColorByAttr( + this@TranslucentThemeActivity, + R.attr.colorTextOnPrimary + ) + ) + setActiveControlsWidgetColor( + ThemeUtils.getColorByAttr( + this@TranslucentThemeActivity, + R.attr.colorAccent + ) + ) + setLogoColor( + ThemeUtils.getColorByAttr( + this@TranslucentThemeActivity, + R.attr.colorPrimary + ) + ) + setCompressionFormat(Bitmap.CompressFormat.JPEG) + }) + .start(this@TranslucentThemeActivity) + } + + override fun onLoadFailed(errorDrawable: Drawable?) { + mProgress.visibility = View.GONE + toastShort(R.string.text_load_failed) + } + }) + } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (requestCode == REQUEST_CODE_CHOOSE && resultCode == Activity.RESULT_OK) { val sourceUri = Matisse.obtainResult(data)[0] - Glide.with(this) - .asDrawable() - .load(sourceUri) - .into(object : CustomTarget() { - override fun onLoadCleared(placeholder: Drawable?) {} - - override fun onResourceReady(resource: Drawable, transition: Transition?) { - val bitmap = ImageUtil.drawableToBitmap(resource) - val file = ImageUtil.bitmapToFile(bitmap, File(cacheDir, "origin_background.jpg")) - val sourceFileUri = Uri.fromFile(file) - val destUri = Uri.fromFile(File(filesDir, "cropped_background.jpg")) - val height = BaseApplication.ScreenInfo.EXACT_SCREEN_HEIGHT.toFloat() - val width = BaseApplication.ScreenInfo.EXACT_SCREEN_WIDTH.toFloat() - val uCropOptions = UCrop.Options() - uCropOptions.setShowCropFrame(true) - uCropOptions.setShowCropGrid(true) - uCropOptions.setStatusBarColor(ColorUtils.getDarkerColor(ThemeUtils.getColorByAttr(this@TranslucentThemeActivity, R.attr.colorPrimary))) - uCropOptions.setToolbarColor(ThemeUtils.getColorByAttr(this@TranslucentThemeActivity, R.attr.colorPrimary)) - uCropOptions.setToolbarWidgetColor(ThemeUtils.getColorByAttr(this@TranslucentThemeActivity, R.attr.colorTextOnPrimary)) - uCropOptions.setActiveWidgetColor(ThemeUtils.getColorByAttr(this@TranslucentThemeActivity, R.attr.colorAccent)) - uCropOptions.setActiveControlsWidgetColor(ThemeUtils.getColorByAttr(this@TranslucentThemeActivity, R.attr.colorAccent)) - uCropOptions.setLogoColor(ThemeUtils.getColorByAttr(this@TranslucentThemeActivity, R.attr.colorPrimary)) - uCropOptions.setCompressionFormat(Bitmap.CompressFormat.JPEG) - UCrop.of(sourceFileUri, destUri) - .withAspectRatio(width / height, 1f) - .withOptions(uCropOptions) - .start(this@TranslucentThemeActivity) - } - }) + launchUCrop(sourceUri) } else if (resultCode == Activity.RESULT_OK && requestCode == UCrop.REQUEST_CROP) { mUri = UCrop.getOutput(data!!) invalidateFinishBtn() @@ -125,6 +176,15 @@ class TranslucentThemeActivity : BaseActivity(), View.OnClickListener, OnSeekBar } } + private fun refreshWallpapers() { + if (wallpapers.isNullOrEmpty()) { + recommendWallpapers.visibility = View.GONE + } else { + recommendWallpapers.visibility = View.VISIBLE + wallpaperAdapter.setData(wallpapers) + } + } + private fun refreshBackground() { mProgress.visibility = View.VISIBLE if (mUri == null) { @@ -133,7 +193,7 @@ class TranslucentThemeActivity : BaseActivity(), View.OnClickListener, OnSeekBar return } var bgOptions = RequestOptions.centerCropTransform() - .skipMemoryCache(true) + .skipMemoryCache(true) .diskCacheStrategy(DiskCacheStrategy.NONE) if (blur > 0) { bgOptions = bgOptions.transform(BlurTransformation(blur)) @@ -148,7 +208,7 @@ class TranslucentThemeActivity : BaseActivity(), View.OnClickListener, OnSeekBar val bitmap = ImageUtil.drawableToBitmap(resource) findViewById(R.id.background).background = BitmapDrawable(resources, bitmap) mPalette = Palette.from(bitmap).generate() - mAdapter!!.setPalette(mPalette) + themeColorAdapter.setPalette(mPalette) mSelectColor.visibility = View.VISIBLE mProgress.visibility = View.GONE } @@ -175,23 +235,45 @@ class TranslucentThemeActivity : BaseActivity(), View.OnClickListener, OnSeekBar } listOf( findViewById(R.id.custom_color), - findViewById(R.id.select_pic), - darkColorBtn, - lightColorBtn, - backBtn, - finishBtn + findViewById(R.id.select_pic), + darkColorBtn, + lightColorBtn, + backBtn, + finishBtn ).forEach { it.setOnClickListener(this@TranslucentThemeActivity) } + wallpapers = + CacheUtil.getCache(this, "recommend_wallpapers", List::class.java) as List? colorTheme.enableChangingLayoutTransition() - mAdapter = ThemeColorAdapter(this) - mAdapter!!.onItemClickListener = OnItemClickListener { _: View?, themeColor: Int, _: Int, _: Int -> - appPreferences.translucentPrimaryColor = toString(themeColor) - ThemeUtils.refreshUI(this) + wallpaperAdapter.setOnItemClickListener { _, item, _ -> + launchUCrop(Uri.parse(item)) } + recommendWallpapersRv.addItemDecoration( + HorizontalSpacesDecoration( + 0, + 0, + 16.dpToPx(), + 16.dpToPx(), + false + ) + ) + recommendWallpapersRv.adapter = wallpaperAdapter + recommendWallpapersRv.layoutManager = + LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false) + themeColorAdapter.onItemClickListener = + OnItemClickListener { _: View?, themeColor: Int, _: Int, _: Int -> + appPreferences.translucentPrimaryColor = toString(themeColor) + ThemeUtils.refreshUI(this) + } (findViewById(R.id.select_color_recycler_view) as RecyclerView).apply { - layoutManager = MyLinearLayoutManager(this@TranslucentThemeActivity, MyLinearLayoutManager.HORIZONTAL, false) - adapter = mAdapter + addItemDecoration(HorizontalSpacesDecoration(0, 0, 16.dpToPx(), 16.dpToPx(), false)) + layoutManager = MyLinearLayoutManager( + this@TranslucentThemeActivity, + MyLinearLayoutManager.HORIZONTAL, + false + ) + adapter = themeColorAdapter } alpha = appPreferences.translucentBackgroundAlpha blur = appPreferences.translucentBackgroundBlur @@ -212,6 +294,18 @@ class TranslucentThemeActivity : BaseActivity(), View.OnClickListener, OnSeekBar } refreshBackground() refreshTheme() + fetchWallpapers() + } + + private fun fetchWallpapers() { + launch(IO + job) { + LiteApi.instance + .wallpapersAsync() + .doIfSuccess { + CacheUtil.putCache(this@TranslucentThemeActivity, "recommend_wallpapers", it) + wallpapers = it + } + } } @SuppressLint("ApplySharedPref") diff --git a/app/src/main/java/com/huanchengfly/tieba/post/adapters/WallpaperAdapter.kt b/app/src/main/java/com/huanchengfly/tieba/post/adapters/WallpaperAdapter.kt new file mode 100644 index 00000000..f752219d --- /dev/null +++ b/app/src/main/java/com/huanchengfly/tieba/post/adapters/WallpaperAdapter.kt @@ -0,0 +1,20 @@ +package com.huanchengfly.tieba.post.adapters + +import android.content.Context +import com.huanchengfly.tieba.post.R +import com.huanchengfly.tieba.post.adapters.base.BaseSingleTypeAdapter +import com.huanchengfly.tieba.post.components.MyViewHolder +import com.huanchengfly.tieba.post.utils.ImageUtil + +class WallpaperAdapter(context: Context) : BaseSingleTypeAdapter(context) { + override fun getItemLayoutId(): Int = R.layout.item_wallpaper + + override fun convert(viewHolder: MyViewHolder, item: String, position: Int) { + ImageUtil.load( + viewHolder.getView(R.id.image_view), + ImageUtil.LOAD_TYPE_SMALL_PIC, + item, + true + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/huanchengfly/tieba/post/components/dividers/HorizontalSpacesDecoration.java b/app/src/main/java/com/huanchengfly/tieba/post/components/dividers/HorizontalSpacesDecoration.java deleted file mode 100644 index 3980eb82..00000000 --- a/app/src/main/java/com/huanchengfly/tieba/post/components/dividers/HorizontalSpacesDecoration.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.huanchengfly.tieba.post.components.dividers; - -import android.graphics.Rect; -import android.view.View; - -import androidx.annotation.NonNull; -import androidx.recyclerview.widget.RecyclerView; - -public class HorizontalSpacesDecoration extends RecyclerView.ItemDecoration { - private int top; - private int bottom; - private int left; - private int right; - - public HorizontalSpacesDecoration(int space) { - this(space, space, space, space); - } - - public HorizontalSpacesDecoration(int top, int bottom, int left, int right) { - this.top = top; - this.bottom = bottom; - this.left = left; - this.right = right; - } - - @Override - public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, - @NonNull RecyclerView parent, @NonNull RecyclerView.State state) { - int position = parent.getChildAdapterPosition(view); - RecyclerView.Adapter adapter = parent.getAdapter(); - if (adapter == null) { - return; - } - if (position == 0) { - outRect.left = left; - outRect.right = right / 2; - } else if (position == adapter.getItemCount() - 1) { - outRect.left = left / 2; - outRect.right = right; - } else { - outRect.left = left / 2; - outRect.right = right / 2; - } - outRect.bottom = bottom; - outRect.top = top; - } -} diff --git a/app/src/main/java/com/huanchengfly/tieba/post/components/dividers/HorizontalSpacesDecoration.kt b/app/src/main/java/com/huanchengfly/tieba/post/components/dividers/HorizontalSpacesDecoration.kt new file mode 100644 index 00000000..8d9a0ed9 --- /dev/null +++ b/app/src/main/java/com/huanchengfly/tieba/post/components/dividers/HorizontalSpacesDecoration.kt @@ -0,0 +1,36 @@ +package com.huanchengfly.tieba.post.components.dividers + +import android.graphics.Rect +import android.view.View +import androidx.recyclerview.widget.RecyclerView +import androidx.recyclerview.widget.RecyclerView.ItemDecoration + +class HorizontalSpacesDecoration @JvmOverloads constructor( + private val top: Int, + private val bottom: Int, + private val left: Int, + private val right: Int, + private val spaceOnEdge: Boolean = true +) : ItemDecoration() { + constructor(space: Int) : this(space, space, space, space) + + override fun getItemOffsets( + outRect: Rect, view: View, + parent: RecyclerView, state: RecyclerView.State + ) { + val position = parent.getChildAdapterPosition(view) + val adapter = parent.adapter ?: return + if (position == 0) { + outRect.left = if (spaceOnEdge) left else 0 + outRect.right = right / 2 + } else if (position == adapter.itemCount - 1) { + outRect.left = left / 2 + outRect.right = if (spaceOnEdge) right else 0 + } else { + outRect.left = left / 2 + outRect.right = right / 2 + } + outRect.bottom = bottom + outRect.top = top + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_translucent_theme.xml b/app/src/main/res/layout/activity_translucent_theme.xml index 5c3fbd2e..04b44379 100644 --- a/app/src/main/res/layout/activity_translucent_theme.xml +++ b/app/src/main/res/layout/activity_translucent_theme.xml @@ -52,13 +52,25 @@ + android:paddingTop="8dp" + android:paddingBottom="16dp" + android:paddingStart="16dp" + android:paddingEnd="16dp"> + + @@ -213,6 +225,28 @@ android:layout_width="match_parent" android:layout_height="wrap_content" /> + + + + + + + diff --git a/app/src/main/res/layout/item_wallpaper.xml b/app/src/main/res/layout/item_wallpaper.xml new file mode 100644 index 00000000..961ee34c --- /dev/null +++ b/app/src/main/res/layout/item_wallpaper.xml @@ -0,0 +1,10 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f7a66aeb..98eb10a0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -457,4 +457,5 @@ 界面设置 深色 浅色 + 推荐壁纸