Skip to content

Commit

Permalink
Added an option to toggle Arabic text
Browse files Browse the repository at this point in the history
  • Loading branch information
faisalcodes committed Sep 17, 2023
1 parent 992778f commit 5572384
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -756,14 +756,16 @@ private void initTranslationVersesFinalAsync(Chapter chapter, int fromVerse, int
toVerse
);

boolean arabicTextEnabled = SPReader.getArabicTextEnabled(this);

for (int verseNo = fromVerse, pos = 0; verseNo <= toVerse; verseNo++, pos++) {
ReaderRecyclerItemModel model = new ReaderRecyclerItemModel();
final Verse verse = chapter.getVerse(verseNo);

List<Translation> translations = listOfTranslations.get(pos);
verse.setTranslations(translations);

verse.arabicTextSpannable = prepareVerseText(verse);
verse.arabicTextSpannable = arabicTextEnabled ? prepareVerseText(verse) : null;
verse.translTextSpannable = prepareTranslSpannable(verse, translations, booksInfo);

models.add(model.setViewType(VERSE).setVerse(verse));
Expand Down Expand Up @@ -882,13 +884,16 @@ private void makeJuzTranslationVerses(
toVerse
);

boolean arabicTextEnabled = SPReader.getArabicTextEnabled(this);

for (int verseNo = fromVerse, pos = 0; verseNo <= toVerse; verseNo++, pos++) {
Verse verse = chapter.getVerse(verseNo);
ReaderRecyclerItemModel model = new ReaderRecyclerItemModel();

List<Translation> translations = listOfTranslations.get(pos);
verse.setTranslations(translations);
verse.arabicTextSpannable = prepareVerseText(verse);

verse.arabicTextSpannable = arabicTextEnabled ? prepareVerseText(verse) : null;
verse.translTextSpannable = prepareTranslSpannable(verse, translations, booksInfo);

models.add(model.setViewType(VERSE).setVerse(verse));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ private ReferenceVerseItemModel prepareVerseModel(
verseNo
);
verse.setTranslations(translations);
verse.arabicTextSpannable = prepareVerseText(verse);
verse.arabicTextSpannable = SPReader.getArabicTextEnabled(this) ? prepareVerseText(verse) : null;
verse.translTextSpannable = prepareTranslSpannable(verse, translations, booksInfo);

return new ReferenceVerseItemModel(VIEWTYPE_VERSE, verse, chapterNo, -1, -1, null, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -63,13 +64,13 @@
import com.peacedesign.android.widget.dialog.base.PeaceDialog;
import com.quranapp.android.R;
import com.quranapp.android.activities.readerSettings.ActivitySettings;
import com.quranapp.android.api.RetrofitInstance;
import com.quranapp.android.api.models.recitation.RecitationInfoModel;
import com.quranapp.android.api.models.recitation.RecitationTranslationInfoModel;
import com.quranapp.android.api.models.tafsir.TafsirInfoModel;
import com.quranapp.android.databinding.FragSettingsMainBinding;
import com.quranapp.android.databinding.LytReaderIndexTabBinding;
import com.quranapp.android.databinding.LytReaderSettingsItemBinding;
import com.quranapp.android.databinding.LytReaderSettingsItemSwitchBinding;
import com.quranapp.android.databinding.LytReaderSettingsTextDecoratorBinding;
import com.quranapp.android.databinding.LytReaderSettingsTextSizeBinding;
import com.quranapp.android.databinding.LytResDownloadSourceSheetBinding;
Expand Down Expand Up @@ -240,13 +241,14 @@ private void iniAppSettings() {
initAppLanguage(mBinding.appSettings.getRoot());
initThemes(mBinding.appSettings.getRoot());
initVOTDToggleLauncher(mBinding.appSettings.getRoot());
initArabicTextToggle(mBinding.appSettings.getRoot());
}

private void initAppLanguage(LinearLayout parent) {
LytReaderSettingsItemBinding appLangExplorerBinding = LytReaderSettingsItemBinding.inflate(mInflater, parent,
false);

setupLauncherParams(R.drawable.dr_icon_language, appLangExplorerBinding);
setupLauncherParamsAndIcon(R.drawable.dr_icon_language, appLangExplorerBinding);
setupAppLangTitle(appLangExplorerBinding);

appLangExplorerBinding.launcher.setOnClickListener(v -> launchFrag(FragSettingsLanguage.class, null));
Expand All @@ -269,12 +271,12 @@ private void setupAppLangTitle(LytReaderSettingsItemBinding binding) {
}
}

prepareTitle(binding, R.string.strTitleAppLanguage, selectedLanguageName);
prepareTitle(binding.launcher, R.string.strTitleAppLanguage, selectedLanguageName);
}

private void initThemes(LinearLayout parent) {
LytReaderSettingsItemBinding binding = LytReaderSettingsItemBinding.inflate(mInflater);
setupLauncherParams(R.drawable.dr_icon_theme, binding);
setupLauncherParamsAndIcon(R.drawable.dr_icon_theme, binding);

setupThemeTitle(binding);
binding.launcher.setOnClickListener(v -> launchThemeExplorer(v.getContext(), binding));
Expand All @@ -284,7 +286,7 @@ private void initThemes(LinearLayout parent) {

private void setupThemeTitle(LytReaderSettingsItemBinding binding) {
String selectedTheme = ThemeUtils.resolveThemeTextFromMode(binding.getRoot().getContext());
prepareTitle(binding, R.string.strTitleTheme, selectedTheme);
prepareTitle(binding.launcher, R.string.strTitleTheme, selectedTheme);
}

private void launchThemeExplorer(Context ctx, LytReaderSettingsItemBinding parentBinding) {
Expand Down Expand Up @@ -322,26 +324,21 @@ private void launchThemeExplorer(Context ctx, LytReaderSettingsItemBinding paren
sheetDialog.show(getParentFragmentManager());
}

private void initVOTDToggleLauncher(LinearLayout parent) {
LytReaderSettingsItemBinding binding = LytReaderSettingsItemBinding.inflate(mInflater, parent, false);
private void initArabicTextToggle(LinearLayout parent) {
Context context = parent.getContext();

setupLauncherParams(R.drawable.dr_icon_heart_filled, binding);
setupVOTDToggleTitle(binding);
LytReaderSettingsItemSwitchBinding binding = LytReaderSettingsItemSwitchBinding.inflate(
mInflater, parent, true
);
setupLauncherParams(binding.getRoot());

binding.launcher.setOnClickListener(v -> launchVOTDToggleExplorer(v.getContext(), binding));
binding.container.setOnClickListener(v -> binding.switcher.toggle());
binding.switcher.setChecked(SPReader.getArabicTextEnabled(context));

parent.addView(binding.getRoot());
}
binding.switcher.setOnCheckedChangeListener((buttonView, isChecked) ->
SPReader.setArabicTextEnabled(context, isChecked));

private void setupVOTDToggleTitle(LytReaderSettingsItemBinding binding) {
Context context = binding.getRoot().getContext();

String status = context.getString(
VOTDUtils.isVOTDTrulyEnabled(context)
? R.string.strLabelOn
: R.string.strLabelOff
);
prepareTitle(binding, R.string.strTitleVOTD, status);
prepareTitle(binding.text, R.string.titleArabicTextToggle, context.getString(R.string.msgArabicTextToggle));
}

private void launchVOTDToggleExplorer(Context ctx, LytReaderSettingsItemBinding parentBinding) {
Expand Down Expand Up @@ -407,6 +404,29 @@ private void requestAlarmPermission(Context ctx) {
startActivity(new Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM));
}


private void initVOTDToggleLauncher(LinearLayout parent) {
LytReaderSettingsItemBinding binding = LytReaderSettingsItemBinding.inflate(mInflater, parent, false);

setupLauncherParamsAndIcon(R.drawable.dr_icon_heart_filled, binding);
setupVOTDToggleTitle(binding);

binding.launcher.setOnClickListener(v -> launchVOTDToggleExplorer(v.getContext(), binding));

parent.addView(binding.getRoot());
}

private void setupVOTDToggleTitle(LytReaderSettingsItemBinding binding) {
Context context = binding.getRoot().getContext();

String status = context.getString(
VOTDUtils.isVOTDTrulyEnabled(context)
? R.string.strLabelOn
: R.string.strLabelOff
);
prepareTitle(binding.launcher, R.string.strTitleVOTD, status);
}

private void initReaderSettings(Context ctx) {
LytSettingsReaderBinding readerSettings = mBinding.readerSettings;

Expand All @@ -425,7 +445,7 @@ private void initReaderSettings(Context ctx) {
private void initTranslExplorer(LinearLayout parent) {
mTranslExplorerBinding = LytReaderSettingsItemBinding.inflate(mInflater);

setupLauncherParams(R.drawable.dr_icon_translations, mTranslExplorerBinding);
setupLauncherParamsAndIcon(R.drawable.dr_icon_translations, mTranslExplorerBinding);
setupTranslTitle();

mTranslExplorerBinding.launcher.setOnClickListener(v -> {
Expand Down Expand Up @@ -467,13 +487,13 @@ private void setupTranslTitle() {

final int size = slugs.size();
String subtext = size == 0 ? null : getString(R.string.strLabelSelectedCount, size);
prepareTitle(mTranslExplorerBinding, R.string.strTitleTranslations, subtext);
prepareTitle(mTranslExplorerBinding.launcher, R.string.strTitleTranslations, subtext);
}

private void initTafsirExplorer(LinearLayout parent) {
mTafsirExplorerBinding = LytReaderSettingsItemBinding.inflate(mInflater);

setupLauncherParams(R.drawable.dr_icon_tafsir, mTafsirExplorerBinding);
setupLauncherParamsAndIcon(R.drawable.dr_icon_tafsir, mTafsirExplorerBinding);
setupTafsirTitle();

mTafsirExplorerBinding.launcher.setOnClickListener(v -> launchFrag(FragSettingsTafsirs.class, null));
Expand All @@ -484,7 +504,7 @@ private void initTafsirExplorer(LinearLayout parent) {
private void initScriptExplorer(LinearLayout parent) {
mScriptExplorerBinding = LytReaderSettingsItemBinding.inflate(mInflater);

setupLauncherParams(R.drawable.dr_icon_quran_script, mScriptExplorerBinding);
setupLauncherParamsAndIcon(R.drawable.dr_icon_quran_script, mScriptExplorerBinding);
setupScriptTitle();

mScriptExplorerBinding.launcher.setOnClickListener(v -> launchFrag(FragSettingsScripts.class, null));
Expand All @@ -499,18 +519,18 @@ private void setupScriptTitle() {
String subtitle = QuranScriptUtilsKt.getQuranScriptName(
SPReader.getSavedScript(mScriptExplorerBinding.getRoot().getContext())
);
prepareTitle(mScriptExplorerBinding, R.string.strTitleSelectScripts, subtitle);
prepareTitle(mScriptExplorerBinding.launcher, R.string.strTitleSelectScripts, subtitle);
}

private void setupTafsirTitle() {
if (mTafsirExplorerBinding == null) return;
Context ctx = mTafsirExplorerBinding.getRoot().getContext();

prepareTitle(mTafsirExplorerBinding, R.string.strTitleSelectTafsir, null);
prepareTitle(mTafsirExplorerBinding.launcher, R.string.strTitleSelectTafsir, null);

TafsirManager.prepare(ctx, false, () -> {
prepareTitle(
mTafsirExplorerBinding,
mTafsirExplorerBinding.launcher,
R.string.strTitleSelectTafsir,
TafsirUtils.getTafsirName(SPReader.getSavedTafsirKey(ctx))
);
Expand All @@ -522,7 +542,7 @@ private void setupTafsirTitle() {
private void initRecitationExplorer(LinearLayout parent) {
mRecitationExplorerBinding = LytReaderSettingsItemBinding.inflate(mInflater);

setupLauncherParams(R.drawable.dr_icon_recitation, mRecitationExplorerBinding);
setupLauncherParamsAndIcon(R.drawable.dr_icon_recitation, mRecitationExplorerBinding);
setupRecitationTitle();

mRecitationExplorerBinding.launcher.setOnClickListener(v -> launchFrag(FragSettingsRecitations.class, null));
Expand All @@ -534,11 +554,11 @@ private void setupRecitationTitle() {
if (mRecitationExplorerBinding == null) return;
Context ctx = mRecitationExplorerBinding.getRoot().getContext();

prepareTitle(mRecitationExplorerBinding, R.string.strTitleSelectReciter, null);
prepareTitle(mRecitationExplorerBinding.launcher, R.string.strTitleSelectReciter, null);
RecitationManager.prepare(ctx, false, () -> {
RecitationManager.prepareTranslations(ctx, false, () -> {
prepareTitle(
mRecitationExplorerBinding,
mRecitationExplorerBinding.launcher,
R.string.strTitleSelectReciter,
RecitationManager.getCurrentReciterNameForAudioOption(ctx)
);
Expand All @@ -553,9 +573,10 @@ private void initManageAudioExplorer(LinearLayout parent) {
com.quranapp.android.databinding.LytReaderSettingsItemBinding mManageAudioExplorerBinding = LytReaderSettingsItemBinding.inflate(
mInflater);

setupLauncherParams(R.drawable.dr_icon_download, mManageAudioExplorerBinding);
setupLauncherParamsAndIcon(R.drawable.dr_icon_download, mManageAudioExplorerBinding);

prepareTitle(mManageAudioExplorerBinding, R.string.titleManageAudio, getString(R.string.downloadRecitations));
prepareTitle(mManageAudioExplorerBinding.launcher, R.string.titleManageAudio,
getString(R.string.downloadRecitations));

mManageAudioExplorerBinding.launcher.setOnClickListener(v -> launchFrag(FragSettingsManageAudio.class, null));

Expand All @@ -572,8 +593,8 @@ private void setupLauncherIcon(int startIconRes, IconedTextView textView) {
}

@SuppressLint("RtlHardcoded")
private void prepareTitle(LytReaderSettingsItemBinding binding, int titleRes, String subtitle) {
Context ctx = binding.getRoot().getContext();
private void prepareTitle(TextView txtView, int titleRes, String subtitle) {
Context ctx = txtView.getContext();

SpannableStringBuilder ssb = new SpannableStringBuilder();
int flag = SPAN_EXCLUSIVE_EXCLUSIVE;
Expand All @@ -592,20 +613,22 @@ private void prepareTitle(LytReaderSettingsItemBinding binding, int titleRes, St
ssb.append("\n").append(subtitleSS);
}

binding.launcher.setText(ssb);
binding.launcher.setGravity(WindowUtils.isRTL(ctx) ? Gravity.RIGHT : Gravity.LEFT);
txtView.setText(ssb);
txtView.setGravity(WindowUtils.isRTL(ctx) ? Gravity.RIGHT : Gravity.LEFT);
}

private void setupLauncherParams(int startIconRes, LytReaderSettingsItemBinding launcherBinding) {
View launcherRoot = launcherBinding.getRoot();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
LayoutParamsKt.updateMarginHorizontal(params, ContextKt.dp2px(launcherRoot.getContext(), 10));
LayoutParamsKt.updateMarginVertical(params, ContextKt.dp2px(launcherRoot.getContext(), 5));
launcherRoot.setLayoutParams(params);

private void setupLauncherParamsAndIcon(int startIconRes, LytReaderSettingsItemBinding launcherBinding) {
setupLauncherParams(launcherBinding.getRoot());
setupLauncherIcon(startIconRes, launcherBinding.launcher);
}

private void setupLauncherParams(View layout) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
LayoutParamsKt.updateMarginHorizontal(params, ContextKt.dp2px(layout.getContext(), 10));
LayoutParamsKt.updateMarginVertical(params, ContextKt.dp2px(layout.getContext(), 5));
layout.setLayoutParams(params);
}

private void initLayoutStyle(Context ctx) {
CardView readerStyleContainer = mBinding.readerSettings.readerStyleContainer;

Expand Down Expand Up @@ -809,7 +832,7 @@ private void iniOtherSettings() {

private void initDownloadSource(LinearLayout parent) {
LytReaderSettingsItemBinding binding = LytReaderSettingsItemBinding.inflate(mInflater);
setupLauncherParams(R.drawable.dr_icon_download, binding);
setupLauncherParamsAndIcon(R.drawable.dr_icon_download, binding);

setupDownloadSrcTitle(binding);
binding.launcher.setOnClickListener(v -> launchDownloadSourceSelector(v.getContext(), binding));
Expand Down Expand Up @@ -859,7 +882,7 @@ private void setupDownloadSrcTitle(LytReaderSettingsItemBinding binding) {
final String selectedSource = DownloadSourceUtils.getCurrentSourceName(
binding.getRoot().getContext()
);
prepareTitle(binding, R.string.titleResourceDownloadSource, selectedSource);
prepareTitle(binding.launcher, R.string.titleResourceDownloadSource, selectedSource);
}

private void initLogLauncher(LinearLayout parent) {
Expand All @@ -868,9 +891,9 @@ private void initLogLauncher(LinearLayout parent) {
LytReaderSettingsItemBinding logExplorerBinding = LytReaderSettingsItemBinding.inflate(mInflater, parent,
false);

setupLauncherParams(R.drawable.icon_log, logExplorerBinding);
setupLauncherParamsAndIcon(R.drawable.icon_log, logExplorerBinding);
prepareTitle(
logExplorerBinding,
logExplorerBinding.launcher,
R.string.appLogs,
TextUtils.concat(ctx.getString(R.string.crashLogs), ", ", ctx.getString(R.string.suppressedLogs)).toString()
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.quranapp.android.utils.sharedPrefs;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import static com.quranapp.android.reader_managers.ReaderParams.READER_STYLE_DEFAULT;
Expand Down Expand Up @@ -32,13 +33,28 @@
* SharedPreferences utility class for Reader
*/
public abstract class SPReader {
public static final String SP_READER = "sp_reader";

public static final String SP_TEXT_STYLE = "sp_reader_text";
public static final String SP_TRANSL = "sp_reader_translations";
public static final String SP_RECITATION_OPTIONS = "sp_reader_recitation_options";
public static final String SP_TAFSIR = "sp_reader_tafsir";
public static final String SP_SCRIPT = "sp_reader_script";
public static final String SP_READER_STYLE = "sp_reader_style";

public static boolean getArabicTextEnabled(Context context) {
SharedPreferences sp = context.getSharedPreferences(SP_READER, Context.MODE_PRIVATE);
return sp.getBoolean(Keys.READER_KEY_ARABIC_TEXT_ENABLED, true);
}

@SuppressLint("ApplySharedPref")
public static void setArabicTextEnabled(Context context, boolean enabled) {
SharedPreferences sp = context.getSharedPreferences(SP_READER, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean(Keys.READER_KEY_ARABIC_TEXT_ENABLED, enabled);
editor.commit();
}

public static float getSavedTextSizeMultArabic(Context context) {
SharedPreferences sp = context.getSharedPreferences(SP_TEXT_STYLE, Context.MODE_PRIVATE);

Expand Down
Loading

0 comments on commit 5572384

Please sign in to comment.