diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/subtitles/SubtitlesFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/subtitles/SubtitlesFragment.kt index cfa353c3..a457d770 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/subtitles/SubtitlesFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/subtitles/SubtitlesFragment.kt @@ -37,6 +37,7 @@ import com.lagradost.cloudstream3.utils.UIHelper.hideSystemUI import com.lagradost.cloudstream3.utils.UIHelper.navigate import com.lagradost.cloudstream3.utils.UIHelper.popCurrentPage import kotlinx.android.synthetic.main.subtitle_settings.* +import java.io.File const val SUBTITLE_KEY = "subtitle_settings" const val SUBTITLE_AUTO_SELECT_KEY = "subs_auto_select" @@ -48,9 +49,10 @@ data class SaveCaptionStyle( @JsonProperty("windowColor") var windowColor: Int, @CaptionStyleCompat.EdgeType @JsonProperty("edgeType") var edgeType: Int, - @JsonProperty("edgeColor") var edgeColor: Int, + @JsonProperty("edgeColor") var edgeColor: Int, @FontRes @JsonProperty("typeface") var typeface: Int?, + @JsonProperty("typefaceFilePath") var typefaceFilePath: String?, /**in dp**/ @JsonProperty("elevation") var elevation: Int, /**in sp**/ @@ -64,17 +66,26 @@ class SubtitlesFragment : Fragment() { val applyStyleEvent = Event() fun Context.fromSaveToStyle(data: SaveCaptionStyle): CaptionStyleCompat { - val typeface = data.typeface return CaptionStyleCompat( data.foregroundColor, data.backgroundColor, data.windowColor, data.edgeType, data.edgeColor, - if (typeface == null) Typeface.SANS_SERIF else ResourcesCompat.getFont( - this, - typeface - ) + data.typefaceFilePath?.let { + try { + // RuntimeException: Font asset not found + Typeface.createFromFile(File(it)) + } catch (e: Exception) { + null + } + } ?: data.typeface?.let { + ResourcesCompat.getFont( + this, + it + ) + } + ?: Typeface.SANS_SERIF ) } @@ -106,11 +117,24 @@ class SubtitlesFragment : Fragment() { CaptionStyleCompat.EDGE_TYPE_OUTLINE, getDefColor(1), null, + null, DEF_SUBS_ELEVATION, null, ) } + private fun Context.getSavedFonts(): List { + val externalFiles = getExternalFilesDir(null) ?: return emptyList() + val fontDir = File(externalFiles.absolutePath + "/Fonts").also { + it.mkdir() + } + return fontDir.list()?.mapNotNull { + if (it.endsWith(".ttf")) { + File(fontDir.absolutePath + "/" + it) + } else null + } ?: listOf() + } + private fun Context.getCurrentStyle(): CaptionStyleCompat { return fromSaveToStyle(getCurrentSavedStyle()) } @@ -191,6 +215,10 @@ class SubtitlesFragment : Fragment() { hide = arguments?.getBoolean("hide") ?: true onColorSelectedEvent += ::onColorSelected onDialogDismissedEvent += ::onDialogDismissed + subs_import_text?.text = getString(R.string.subs_import_text).format( + context?.getExternalFilesDir(null)?.absolutePath.toString() + "/Fonts" + ) + context?.fixPaddingStatusbar(subs_root) @@ -395,22 +423,38 @@ class SubtitlesFragment : Fragment() { Pair(R.font.comic_sans, "Comic Sans"), Pair(R.font.poppins_regular, "Poppins"), ) + val savedFontTypes = textView.context.getSavedFonts() + + val currentIndex = + savedFontTypes.indexOfFirst { it.absolutePath == state.typefaceFilePath } + .let { index -> + if (index == -1) + fontTypes.indexOfFirst { it.first == state.typeface } + else index + fontTypes.size + } //showBottomDialog activity?.showDialog( - fontTypes.map { it.second }, - fontTypes.map { it.first }.indexOf(state.typeface), + fontTypes.map { it.second } + savedFontTypes.map { it.name }, + currentIndex, (textView as TextView).text.toString(), false, dismissCallback ) { index -> - state.typeface = fontTypes.map { it.first }[index] + if (index < fontTypes.size) { + state.typeface = fontTypes[index].first + state.typefaceFilePath = null + } else { + state.typefaceFilePath = savedFontTypes[index - fontTypes.size].absolutePath + state.typeface = null + } textView.context.updateState() } } subs_font.setOnLongClickListener { textView -> state.typeface = null + state.typefaceFilePath = null textView.context.updateState() showToast(activity, R.string.subs_default_reset_toast, Toast.LENGTH_SHORT) return@setOnLongClickListener true diff --git a/app/src/main/res/layout/subtitle_settings.xml b/app/src/main/res/layout/subtitle_settings.xml index 63d47cb6..e1848ed1 100644 --- a/app/src/main/res/layout/subtitle_settings.xml +++ b/app/src/main/res/layout/subtitle_settings.xml @@ -1,185 +1,195 @@ + android:id="@+id/subs_root" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:background="?attr/primaryBlackBackground"> + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:orientation="vertical"> + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_rowWeight="1" + android:layout_marginTop="20dp" + android:layout_marginBottom="10dp" + android:paddingStart="20dp" + android:paddingEnd="20dp" + android:text="@string/subtitles_settings" + android:textColor="?attr/textColor" + android:textSize="20sp" + android:textStyle="bold" /> + android:layout_width="match_parent" + android:layout_height="75sp"> + android:layout_width="match_parent" + android:layout_height="match_parent" + android:contentDescription="@string/preview_background_img_des" + android:scaleType="centerCrop" + android:src="@drawable/subtitles_preview_background" /> + android:layout_width="match_parent" + android:layout_height="match_parent" + android:layout_gravity="center" + android:foregroundGravity="center" /> + android:nextFocusRight="@id/cancel_btt" + android:nextFocusDown="@id/subs_font_size" + android:text="@string/subs_font" /> + android:nextFocusUp="@id/subs_font" + android:nextFocusDown="@id/subs_text_color" + android:text="@string/subs_font_size" /> + android:nextFocusUp="@id/subs_font_size" + android:nextFocusDown="@id/subs_outline_color" + android:text="@string/subs_text_color" /> + android:nextFocusUp="@id/subs_text_color" + android:nextFocusDown="@id/subs_background_color" + android:text="@string/subs_outline_color" /> + android:nextFocusUp="@id/subs_outline_color" + android:nextFocusDown="@id/subs_window_color" + android:text="@string/subs_background_color" /> + android:nextFocusUp="@id/subs_background_color" + android:nextFocusDown="@id/subs_edge_type" + android:text="@string/subs_window_color" /> + android:nextFocusUp="@id/subs_window_color" + android:nextFocusDown="@id/subs_subtitle_elevation" + android:text="@string/subs_edge_type" /> + android:nextFocusUp="@id/subs_edge_type" + android:nextFocusDown="@id/subs_auto_select_language" + android:text="@string/subs_subtitle_elevation" /> + android:nextFocusUp="@id/subs_subtitle_elevation" + android:nextFocusDown="@id/subs_download_languages" + android:text="@string/subs_auto_select_language" /> + android:nextFocusUp="@id/subs_auto_select_language" + android:nextFocusDown="@id/apply_btt" + android:text="@string/subs_download_languages" /> + android:gravity="center" + android:text="@string/subs_hold_to_reset_to_default" + android:textColor="?attr/textColor" + android:textSize="14sp" /> + + + android:layout_width="match_parent" + android:layout_height="60dp" + android:layout_gravity="bottom" + android:gravity="bottom|end" + android:orientation="horizontal"> + android:id="@+id/apply_btt" + style="@style/WhiteButton" + android:layout_width="wrap_content" + android:layout_gravity="center_vertical|end" + android:nextFocusRight="@id/cancel_btt" + android:nextFocusUp="@id/subs_download_languages" + android:text="@string/sort_apply" + android:visibility="visible"> + android:id="@+id/cancel_btt" + style="@style/BlackButton" + android:layout_width="wrap_content" + android:layout_gravity="center_vertical|end" + android:nextFocusLeft="@id/apply_btt" + android:nextFocusUp="@id/subs_download_languages" + android:text="@string/sort_cancel" /> \ 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 40815763..6baeefa8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -175,6 +175,7 @@ Auto-Select Language Download Languages Hold to reset to default + Import fonts by placing them in %s Continue Watching Remove