Skip to content

Commit

Permalink
fix: use TableLayout for anime details
Browse files Browse the repository at this point in the history
  • Loading branch information
MrIkso committed Jun 30, 2024
1 parent 779ebd8 commit 02ba293
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 299 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.mrikso.anitube.app.ui.detail;

import android.content.Context;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;
import androidx.core.util.Pair;
import androidx.fragment.app.Fragment;
Expand All @@ -25,6 +28,7 @@
import com.mrikso.anitube.app.adapters.ScreenshotsAdapter;
import com.mrikso.anitube.app.databinding.FragmentDetailsAnimeBinding;
import com.mrikso.anitube.app.databinding.ItemChipBinding;
import com.mrikso.anitube.app.databinding.ItemDetailsInfoRowBinding;
import com.mrikso.anitube.app.databinding.LayoutReleaseActionBinding;
import com.mrikso.anitube.app.interfaces.OnTorrentClickListener;
import com.mrikso.anitube.app.model.AnimeDetailsModel;
Expand All @@ -38,21 +42,21 @@
import com.mrikso.anitube.app.ui.dialogs.TorrentSelectionDialog;
import com.mrikso.anitube.app.utils.DialogUtils;
import com.mrikso.anitube.app.utils.DownloadUtils;
import com.mrikso.anitube.app.utils.HtmlTextSpanner;
import com.mrikso.anitube.app.utils.IntentUtils;
import com.mrikso.anitube.app.utils.ParserUtils;
import com.mrikso.anitube.app.utils.PreferencesHelper;
import com.mrikso.anitube.app.utils.StringUtils;
import com.mrikso.anitube.app.utils.ViewExtKt;
import com.mrikso.anitube.app.utils.ViewUtils;

import dagger.hilt.android.AndroidEntryPoint;

import org.jsoup.internal.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class DetailsAnimeFragmemt extends Fragment
implements BaseAnimeAdapter.OnItemClickListener, ScreenshotsAdapter.OnItemClickListener {
Expand All @@ -66,6 +70,7 @@ public class DetailsAnimeFragmemt extends Fragment
private List<ScreenshotModel> screenshotsList = null;
private int mode = 0;
private int animeId;
private int tableRowIndex = 0;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
Expand Down Expand Up @@ -107,6 +112,7 @@ public void onDestroyView() {
similarAnimeAdapter = null;
screenshotsList = null;
franchisesAdapter = null;
tableRowIndex = 0;
}

private void initObservers() {
Expand All @@ -132,7 +138,7 @@ private void initObservers() {
binding.clContent.setVisibility(View.GONE);
binding.loadStateLayout.progressBar.setVisibility(View.GONE);
binding.loadStateLayout.errorLayout.setVisibility(View.VISIBLE);
binding.loadStateLayout.errorMessage.setText("no network");
binding.loadStateLayout.errorMessage.setText(R.string.message_error_no_internet);
break;
}
});
Expand Down Expand Up @@ -206,6 +212,7 @@ private void showDetails(AnimeDetailsModel animeDetails) {
if (!Strings.isNullOrEmpty(description)) {
binding.layoutDescription.llDescription.setVisibility(View.VISIBLE);
var expandableTextView = binding.layoutDescription.tvDescription;
expandableTextView.setVisibility(View.VISIBLE);
expandableTextView.setContent(EncodeUtils.htmlDecode(description));
}

Expand All @@ -230,12 +237,12 @@ private void showDetails(AnimeDetailsModel animeDetails) {
List<BaseAnimeModel> similarAnimeList = animeDetails.getSimilarAnimeList();
if (similarAnimeList != null && !similarAnimeList.isEmpty()) {
binding.layoutSimilar.llSimilar.setVisibility(View.VISIBLE);
similarAnimeAdapter.submitList(similarAnimeList);
similarAnimeAdapter.submitList(similarAnimeList);

}

List<FranchiseModel> franchiseAnimeList = animeDetails.getFranchiseList();
if (franchiseAnimeList != null && !franchiseAnimeList.isEmpty() && franchiseAnimeList.size() > 1 ) {
if (franchiseAnimeList != null && !franchiseAnimeList.isEmpty() && franchiseAnimeList.size() > 1) {
binding.layoutFranchises.llFranchise.setVisibility(View.VISIBLE);
franchisesAdapter.submitList(franchiseAnimeList);

Expand Down Expand Up @@ -326,67 +333,71 @@ public void onDownloadByMagnet(String url) {
}

private void showInfoGroup(AnimeDetailsModel animeDetails) {

SimpleModel yearRelease = animeDetails.getReleaseYear();
if (yearRelease != null) {
binding.layoutInfo.yearLayout.setVisibility(View.VISIBLE);
TextView year = binding.layoutInfo.tvYear;
createClickableTextView(year, yearRelease.getText(), yearRelease.getUrl());
addTableRow(R.string.year, createClickableTextView(yearRelease.getText(), yearRelease.getUrl()));
}

String director = animeDetails.getDirector();
if (!StringUtil.isBlank(director)) {
binding.layoutInfo.directorLayout.setVisibility(View.VISIBLE);
binding.layoutInfo.tvDirector.setText(director);
addTableRow(R.string.director, director);
}

String studio = animeDetails.getStudio();
if (!StringUtil.isBlank(studio)) {
binding.layoutInfo.studioLayout.setVisibility(View.VISIBLE);
binding.layoutInfo.tvStudio.setText(studio);
addTableRow(R.string.studio, studio);
}

String episodes = animeDetails.getEpisodes();
if (!StringUtil.isBlank(episodes)) {
binding.layoutInfo.episodesLayout.setVisibility(View.VISIBLE);
binding.layoutInfo.tvEpisodes.setText(episodes);
addTableRow(R.string.series, episodes);
}

List<SimpleModel> translators = animeDetails.getTranslators();
if (translators != null && !translators.isEmpty()) {
binding.layoutInfo.translationLayout.setVisibility(View.VISIBLE);
String translatorsString =
translators.stream().map(e -> e.getText()).collect(Collectors.joining(", "));
binding.layoutInfo.tvTransators.setText(translatorsString);
addTableRow(R.string.translation, translatorsString);
}

showVoicers(animeDetails.getVoicers());
showDubbers(animeDetails.getDubbersTeamList());

showGenresGroup(animeDetails.getGenres());
}

private void showVoicers(List<SimpleModel> voicers) {
if (voicers != null && !voicers.isEmpty()) {
binding.layoutInfo.dubbersLayout.setVisibility(View.VISIBLE);
String dubbersString = voicers.stream().map(e -> e.getText()).collect(Collectors.joining(", "));
binding.layoutInfo.tvDubbers.setText(dubbersString);
String dubbersString = voicers.stream().map(SimpleModel::getText).collect(Collectors.joining(", ")).trim();
addTableRow(R.string.dubbers, dubbersString);
}
}

private void showDubbers(List<DubbersTeam> dubbers) {
if (dubbers != null && !dubbers.isEmpty()) {
binding.layoutInfo.dubbersLayout.setVisibility(View.VISIBLE);
StringBuilder sb = new StringBuilder();
for (DubbersTeam dubberTeam : dubbers) {
sb.append(dubberTeam.getDubberTeam().getText());
sb.append(": ");
String dubbersString =
dubberTeam.getDubbers().stream().map(e -> e.getText()).collect(Collectors.joining(", "));
dubberTeam.getDubbers().stream().map(SimpleModel::getText).collect(Collectors.joining(", "));
sb.append(dubbersString);
sb.append("\n");
}
binding.layoutInfo.tvDubbers.setText(sb.toString().trim());
addTableRow(R.string.dubbers, sb.toString().trim());
}
}

private void addTableRow(@StringRes int title, @Nullable CharSequence value) {
TableLayout table = binding.layoutInfo.tableLayout;
var row = ItemDetailsInfoRowBinding.inflate(getLayoutInflater(), table, false);
row.tvTitle.setText(title);
row.tvValue.setText(value);
row.tvValue.setClickable(true);
row.tvValue.setMovementMethod(LinkMovementMethod.getInstance());
table.addView(row.getRoot(), tableRowIndex);
tableRowIndex++;
}

private void showGenresGroup(List<SimpleModel> genres) {
if (genres != null && !genres.isEmpty()) {
binding.layoutInfo.genresChipGroup.setVisibility(View.VISIBLE);
Expand Down Expand Up @@ -451,9 +462,10 @@ private void createClickableTextView(TextView tv, String message, String url) {
ViewUtils.makeLinks(tv, links);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
private SpannableString createClickableTextView(String message, String url) {
return ViewExtKt.makeLinks(message, message, (v) -> {
openSearchFragment(message, url);
});
}

private void addOrRemoveFromFavorites(int animeId, boolean isAdd) {
Expand Down
33 changes: 33 additions & 0 deletions app/src/main/java/com/mrikso/anitube/app/utils/ViewExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.mrikso.anitube.app.utils

import android.text.SpannableString
import android.text.Spanned
import android.text.style.ClickableSpan
import android.view.View

fun makeLinks(
text: String,
phrase: String,
/* phraseColor: Int,*/
listener: View.OnClickListener
): SpannableString {
val spannableString = SpannableString(text)
val clickableSpan = object : ClickableSpan() {
/*override fun updateDrawState(ds: TextPaint) {
// ds.color = phraseColor // you can use custom color
ds.isUnderlineText = false // this remove the underline
}*/
override fun onClick(view: View) {
listener.onClick(view)
}
}
val start = text.indexOf(phrase)
val end = start + phrase.length
spannableString.setSpan(
clickableSpan,
start,
end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
return spannableString
}
Loading

0 comments on commit 02ba293

Please sign in to comment.