Skip to content

Commit

Permalink
Mod 版本信息页展示截图 (#683)
Browse files Browse the repository at this point in the history
* Mod 版本信息页展示截图

* 使用RecyclerView展示截图

* 解决截图回收再重载后缩放问题
  • Loading branch information
MovTery authored Oct 26, 2024
1 parent 18bb5d9 commit 89a776b
Show file tree
Hide file tree
Showing 10 changed files with 432 additions and 8 deletions.
4 changes: 4 additions & 0 deletions FCL/src/main/java/com/tungsten/fcl/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ class MainActivity : FCLActivity(), OnSelectListener, View.OnClickListener {
override fun loadVersions(modRepository: RemoteModRepository): Stream<RemoteMod.Version> {
throw IOException()
}

override fun loadScreenshots(modRepository: RemoteModRepository): MutableList<RemoteMod.Screenshot> {
throw IOException()
}
})
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import android.view.View;
import android.widget.ListView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.tungsten.fcl.R;
import com.tungsten.fcl.setting.Profile;
Expand Down Expand Up @@ -59,6 +62,10 @@ public class RemoteModInfoPage extends FCLTempPage implements View.OnClickListen
private FCLTextView description;
private FCLTextView mcmod;
private FCLImageButton website;
private FCLProgressBar screenshotLoading;
private FCLImageView screenshotRetry;
private FCLTextView screenshotNoResult;
private RecyclerView screenshotView;

public RemoteModInfoPage(Context context, int id, FCLUILayout parent, int resId, DownloadPage page, RemoteMod addon, Profile.ProfileVersion version, @Nullable RemoteModVersionPage.DownloadCallback callback) {
super(context, id, parent, resId);
Expand All @@ -85,6 +92,10 @@ public void create() {
description = findViewById(R.id.description);
mcmod = findViewById(R.id.mcmod);
website = findViewById(R.id.website);
screenshotView = findViewById(R.id.screenshot_recyclerView);
screenshotLoading = findViewById(R.id.screenshot_loading);
screenshotRetry = findViewById(R.id.screenshot_retry);
screenshotNoResult = findViewById(R.id.screenshot_no_result);

retry.setOnClickListener(this);
mcmod.setOnClickListener(this);
Expand All @@ -110,6 +121,7 @@ public void onStart() {
this.tag.setText(tag);

loadModVersions();
loadScreenshots();
}

private void loadGameVersions() {
Expand Down Expand Up @@ -139,6 +151,25 @@ private void loadModVersions() {
}).start();
}

private void loadScreenshots() {
setScreenshotLoading(true);

Task.supplyAsync(() -> addon.getData().loadScreenshots(repository)).whenComplete(Schedulers.androidUIThread(), ((result, exception) -> {
if (exception == null) {
if (result.isEmpty()) {
screenshotNoResult.setVisibility(View.VISIBLE);
} else {
RemoteModScreenshotAdapter adapter = new RemoteModScreenshotAdapter(getContext(), result);
screenshotView.setLayoutManager(new LinearLayoutManager(getContext()));
screenshotView.setAdapter(adapter);
}
} else {
setScreenshotFailed();
}
setScreenshotLoading(false);
})).start();
}

private SimpleMultimap<String, RemoteMod.Version, List<RemoteMod.Version>> sortVersions(Stream<RemoteMod.Version> versions) {
SimpleMultimap<String, RemoteMod.Version, List<RemoteMod.Version>> classifiedVersions
= new SimpleMultimap<>(HashMap::new, ArrayList::new);
Expand Down Expand Up @@ -173,6 +204,22 @@ public void setFailed() {
});
}

private void setScreenshotLoading(boolean loading) {
Schedulers.androidUIThread().execute(() -> {
screenshotLoading.setVisibility(loading ? View.VISIBLE : View.GONE);
if (loading) {
screenshotRetry.setVisibility(View.GONE);
}
});
}

private void setScreenshotFailed() {
Schedulers.androidUIThread().execute(() -> {
screenshotRetry.setVisibility(View.VISIBLE);
screenshotLoading.setVisibility(View.GONE);
});
}

@Override
public Task<?> refresh(Object... param) {
return null;
Expand All @@ -198,5 +245,8 @@ public void onClick(View v) {
if (v == website && StringUtils.isNotBlank(addon.getPageUrl())) {
AndroidUtils.openLink(getContext(), addon.getPageUrl());
}
if (v == screenshotRetry) {
loadScreenshots();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.tungsten.fcl.ui.download

import android.content.Context
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.tungsten.fcl.R
import com.tungsten.fcl.databinding.ViewModScreenshotBinding
import com.tungsten.fclcore.mod.RemoteMod.Screenshot
import com.tungsten.fclcore.util.StringUtils
import com.tungsten.fcllibrary.component.view.FCLTextView


class RemoteModScreenshotAdapter(
val context: Context,
private val screenshotList: List<Screenshot>
) :
RecyclerView.Adapter<RemoteModScreenshotAdapter.ScreenshotViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ScreenshotViewHolder {
return ScreenshotViewHolder(
DataBindingUtil.inflate(
LayoutInflater.from(context),
R.layout.view_mod_screenshot,
parent,
false
)
)
}

override fun getItemCount(): Int = screenshotList.size

override fun onBindViewHolder(holder: ScreenshotViewHolder, position: Int) {
holder.setScreenshot(screenshotList[position])
}

class ScreenshotViewHolder(val binding: ViewModScreenshotBinding) :
RecyclerView.ViewHolder(binding.root) {
fun setScreenshot(screenshot: Screenshot) {
binding.retry.setOnClickListener { loadScreenshotImage(screenshot.imageUrl) }

binding.screenshot.setImageDrawable(null)
loadScreenshotImage(screenshot.imageUrl)

binding.title.setVisibleIfNotBlank(screenshot.title)
binding.description.setVisibleIfNotBlank(screenshot.description)
}

private fun loadScreenshotImage(imageUrl: String) {
binding.apply {
setLoading(true)
Glide.with(screenshot)
.load(imageUrl)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.fitCenter()
.addListener(object : RequestListener<Drawable> {
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<Drawable>,
isFirstResource: Boolean
): Boolean {
setLoading(false)
setFailed()
return false
}

override fun onResourceReady(
resource: Drawable,
model: Any,
target: Target<Drawable>,
dataSource: DataSource,
isFirstResource: Boolean
): Boolean {
setLoading(false)
return false
}
})
.into(screenshot)
}
}

private fun setLoading(loading: Boolean) {
binding.loading.visibility = if (loading) View.VISIBLE else View.GONE
if (loading) binding.retry.visibility = View.GONE
}

private fun setFailed() {
binding.retry.visibility = View.VISIBLE
}

private fun FCLTextView.setVisibleIfNotBlank(text: String?) {
visibility = if (StringUtils.isNotBlank(text)) View.VISIBLE else View.GONE
this.text = text
}
}
}
74 changes: 68 additions & 6 deletions FCL/src/main/res/layout/page_download_addon_info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,76 @@

</com.tungsten.fcllibrary.component.view.FCLLinearLayout>

<ListView
android:divider="@android:color/darker_gray"
android:dividerHeight="1dp"
android:background="@drawable/bg_container_white"
android:id="@+id/version_list"
<com.tungsten.fcllibrary.component.view.FCLLinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"/>
android:layout_marginTop="10dp"
android:baselineAligned="false">

<com.tungsten.fcllibrary.component.view.FCLConstraintLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:layout_marginEnd="10dp"
android:orientation="vertical"
android:paddingHorizontal="8dp"
android:background="@drawable/bg_container_white">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/screenshot_recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<com.tungsten.fcllibrary.component.view.FCLProgressBar
android:id="@+id/screenshot_loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_gravity="center_horizontal"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>

<com.tungsten.fcllibrary.component.view.FCLImageView
android:id="@+id/screenshot_retry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_baseline_refresh_24"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>

<com.tungsten.fcllibrary.component.view.FCLTextView
android:id="@+id/screenshot_no_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_gravity="center_horizontal"
android:visibility="gone"
android:text="@string/mods_screenshot_no_result"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>

</com.tungsten.fcllibrary.component.view.FCLConstraintLayout>

<ListView
android:divider="@android:color/darker_gray"
android:dividerHeight="1dp"
android:background="@drawable/bg_container_white"
android:id="@+id/version_list"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>

</com.tungsten.fcllibrary.component.view.FCLLinearLayout>

</com.tungsten.fcllibrary.component.view.FCLLinearLayout>

Expand Down
48 changes: 48 additions & 0 deletions FCL/src/main/res/layout/view_mod_screenshot.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<com.tungsten.fcllibrary.component.view.FCLLinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<com.tungsten.fcllibrary.component.view.FCLProgressBar
android:id="@+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>

<com.tungsten.fcllibrary.component.view.FCLImageView
android:id="@+id/screenshot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:scaleType="fitCenter"
android:adjustViewBounds="true"/>

<com.tungsten.fcllibrary.component.view.FCLImageView
android:id="@+id/retry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:scaleType="fitCenter"
android:src="@drawable/ic_baseline_refresh_24"
android:visibility="gone"/>

<com.tungsten.fcllibrary.component.view.FCLTextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"/>

<com.tungsten.fcllibrary.component.view.FCLTextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="11sp"
android:gravity="center_horizontal"/>

</com.tungsten.fcllibrary.component.view.FCLLinearLayout>
</layout>
1 change: 1 addition & 0 deletions FCL/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@
<string name="mods_restore">回滚</string>
<string name="mods_url">官方页面</string>
<string name="mods_update_modpack_mod_warning">更新整合包中的 Mod 可能导致整合包损坏,使整合包无法正常启动。该操作不可逆,确定要更新吗?</string>
<string name="mods_screenshot_no_result">没有截图可以展示</string>

<string name="profile_shared">公有目录</string>
<string name="profile_private">私有目录</string>
Expand Down
1 change: 1 addition & 0 deletions FCL/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@
<string name="mods_restore">Rollback</string>
<string name="mods_url">Official Page</string>
<string name="mods_update_modpack_mod_warning">Updating mods in a modpack can lead to irreparable results, possibly corrupting the modpack so that it cannot start. Are you sure you want to update?</string>
<string name="mods_screenshot_no_result">No screenshots available to display</string>

<string name="profile_shared">Shared Directory</string>
<string name="profile_private">Private Directory</string>
Expand Down
Loading

0 comments on commit 89a776b

Please sign in to comment.