Skip to content

Commit 6697b72

Browse files
committed
refactor: move doNotTranslate to translator settings
1 parent 6d607b0 commit 6697b72

File tree

2 files changed

+69
-42
lines changed

2 files changed

+69
-42
lines changed

TMessagesProj/src/main/java/org/telegram/ui/LanguageSelectActivity.java

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
package org.telegram.ui;
1010

11+
import static org.telegram.messenger.LocaleController.getString;
12+
1113
import android.content.Context;
1214
import android.content.DialogInterface;
1315
import android.graphics.Canvas;
@@ -42,6 +44,7 @@
4244
import org.telegram.ui.Cells.HeaderCell;
4345
import org.telegram.ui.Cells.LanguageCell;
4446
import org.telegram.ui.Cells.ShadowSectionCell;
47+
import org.telegram.ui.Cells.TextCell;
4548
import org.telegram.ui.Cells.TextCheckCell;
4649
import org.telegram.ui.Cells.TextInfoPrivacyCell;
4750
import org.telegram.ui.Cells.TextRadioCell;
@@ -60,6 +63,8 @@
6063
import java.util.Iterator;
6164
import java.util.Timer;
6265

66+
import tw.nekomimi.nekogram.settings.NekoTranslatorSettingsActivity;
67+
6368
public class LanguageSelectActivity extends BaseFragment implements NotificationCenter.NotificationCenterDelegate {
6469

6570
private ListAdapter listAdapter;
@@ -196,21 +201,19 @@ protected void onMoveAnimationUpdate(RecyclerView.ViewHolder holder) {
196201
if (view instanceof TextCheckCell) {
197202
final boolean prevFullValue = getContextValue() || getChatValue();
198203
if (position == 1) {
199-
// boolean value = !getContextValue();
200-
// getMessagesController().getTranslateController().setContextTranslateEnabled(value);
201-
// ((TextCheckCell) view).setChecked(value);
202-
// NotificationCenter.getInstance(currentAccount).postNotificationName(NotificationCenter.updateSearchSettings);
203-
return;
204+
boolean value = !getContextValue();
205+
getMessagesController().getTranslateController().setContextTranslateEnabled(value);
206+
((TextCheckCell) view).setChecked(value);
207+
NotificationCenter.getInstance(currentAccount).postNotificationName(NotificationCenter.updateSearchSettings);
204208
} else if (position == 2) {
205-
// boolean value = !getChatValue();
206-
// if (value && !getUserConfig().isPremium()) {
207-
// showDialog(new PremiumFeatureBottomSheet(LanguageSelectActivity.this, PremiumPreviewFragment.PREMIUM_FEATURE_TRANSLATIONS, false));
208-
// return;
209-
// }
210-
// getMessagesController().getTranslateController().setChatTranslateEnabled(value);
211-
// NotificationCenter.getInstance(currentAccount).postNotificationName(NotificationCenter.updateSearchSettings);
212-
// ((TextCheckCell) view).setChecked(value);
213-
return;
209+
boolean value = !getChatValue();
210+
if (value && !getUserConfig().isPremium()) {
211+
showDialog(new PremiumFeatureBottomSheet(LanguageSelectActivity.this, PremiumPreviewFragment.PREMIUM_FEATURE_TRANSLATIONS, false));
212+
return;
213+
}
214+
getMessagesController().getTranslateController().setChatTranslateEnabled(value);
215+
NotificationCenter.getInstance(currentAccount).postNotificationName(NotificationCenter.updateSearchSettings);
216+
((TextCheckCell) view).setChecked(value);
214217
}
215218
final boolean currentFullValue = getContextValue() || getChatValue();
216219
if (currentFullValue != prevFullValue) {
@@ -232,6 +235,9 @@ protected void onMoveAnimationUpdate(RecyclerView.ViewHolder holder) {
232235
}
233236
}
234237
return;
238+
} else if (view instanceof TextCell) {
239+
presentFragment(new NekoTranslatorSettingsActivity());
240+
return;
235241
} else if (view instanceof TextSettingsCell) {
236242
presentFragment(new RestrictedLanguagesSelectActivity());
237243
return;
@@ -241,7 +247,7 @@ protected void onMoveAnimationUpdate(RecyclerView.ViewHolder holder) {
241247
}
242248
boolean search = listView.getAdapter() == searchListViewAdapter;
243249
if (!search) {
244-
position -= (7 - (!(getChatValue() || getContextValue()) ? 1 : 0) - (getMessagesController().premiumFeaturesBlocked() ? 1 : 0));
250+
position -= 4;/*(7 - (!(getChatValue() || getContextValue()) ? 1 : 0) - (getMessagesController().premiumFeaturesBlocked() ? 1 : 0));*/
245251
}
246252
LocaleController.LocaleInfo localeInfo;
247253
if (search) {
@@ -306,7 +312,7 @@ protected void onMoveAnimationUpdate(RecyclerView.ViewHolder holder) {
306312
}
307313
boolean search = listView.getAdapter() == searchListViewAdapter;
308314
if (!search) {
309-
position -= (7 - (!(getChatValue() || getContextValue()) ? 1 : 0) - (getMessagesController().premiumFeaturesBlocked() ? 1 : 0));
315+
position -= 4;/*(7 - (!(getChatValue() || getContextValue()) ? 1 : 0) - (getMessagesController().premiumFeaturesBlocked() ? 1 : 0));*/
310316
}
311317
LocaleController.LocaleInfo localeInfo;
312318
if (search) {
@@ -543,7 +549,7 @@ public int getItemCount() {
543549
if (!unofficialLanguages.isEmpty()) {
544550
count += unofficialLanguages.size() + 1;
545551
}
546-
return 4 + (getMessagesController().premiumFeaturesBlocked() ? 0 : 1) + (getChatValue() || getContextValue() ? 1 : 0) + 1 + count;
552+
return 4 + /*(getMessagesController().premiumFeaturesBlocked() ? 0 : 1) + (getChatValue() || getContextValue() ? 1 : 0) + 1 + */count;
547553
}
548554
}
549555

@@ -562,7 +568,9 @@ public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType
562568
view = switchCell;
563569
break;
564570
case VIEW_TYPE_SETTINGS:
565-
TextSettingsCell settingsCell = new TextSettingsCell(mContext);
571+
/*TextSettingsCell settingsCell = new TextSettingsCell(mContext);
572+
settingsCell.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite));*/
573+
TextCell settingsCell = new TextCell(mContext);
566574
settingsCell.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite));
567575
view = settingsCell;
568576
break;
@@ -596,7 +604,7 @@ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
596604
switch (holder.getItemViewType()) {
597605
case VIEW_TYPE_LANGUAGE: {
598606
if (!search) {
599-
position -= (7 - (!(getChatValue() || getContextValue()) ? 1 : 0) - (getMessagesController().premiumFeaturesBlocked() ? 1 : 0));
607+
position -= 4;/*(7 - (!(getChatValue() || getContextValue()) ? 1 : 0) - (getMessagesController().premiumFeaturesBlocked() ? 1 : 0));*/
600608
}
601609
TextRadioCell textSettingsCell = (TextRadioCell) holder.itemView;
602610
textSettingsCell.updateRTL();
@@ -641,8 +649,9 @@ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
641649
break;
642650
}
643651
case VIEW_TYPE_SETTINGS: {
644-
TextSettingsCell settingsCell = (TextSettingsCell) holder.itemView;
645-
settingsCell.updateRTL();
652+
TextCell settingsCell = (TextCell) holder.itemView;
653+
settingsCell.setTextAndIcon(getString(R.string.TranslatorSettings), R.drawable.ic_translate, false);
654+
/*settingsCell.updateRTL();
646655
HashSet<String> langCodes = RestrictedLanguagesSelectActivity.getRestrictedLanguages();
647656
final String doNotTranslateCellName = LocaleController.getString(R.string.DoNotTranslate);
648657
String doNotTranslateCellValue = null;
@@ -676,19 +685,17 @@ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
676685
if (doNotTranslateCellValue == null) {
677686
doNotTranslateCellValue = String.format(LocaleController.getPluralString("Languages", langCodes.size()), langCodes.size());
678687
}
679-
settingsCell.setTextAndValue(doNotTranslateCellName, doNotTranslateCellValue, true, false);
688+
settingsCell.setTextAndValue(doNotTranslateCellName, doNotTranslateCellValue, true, false);*/
680689
break;
681690
}
682691
case VIEW_TYPE_SWITCH: {
683692
TextCheckCell cell = (TextCheckCell) holder.itemView;
684693
cell.updateRTL();
685694
if (position == 1) {
686-
cell.setTextAndCheck(LocaleController.getString(R.string.ShowTranslateButton), true, true);
687-
cell.setEnabled(false, null);
695+
cell.setTextAndCheck(LocaleController.getString(R.string.ShowTranslateButton), getContextValue(), true);
688696
cell.setCheckBoxIcon(0);
689697
} else if (position == 2) {
690698
cell.setTextAndCheck(LocaleController.getString(R.string.ShowTranslateChatButton), getChatValue(), getContextValue() || getChatValue());
691-
cell.setEnabled(false, null);
692699
cell.setCheckBoxIcon(!getUserConfig().isPremium() ? R.drawable.permission_locked : 0);
693700
}
694701
break;
@@ -704,7 +711,7 @@ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
704711
} else {
705712
infoCell.setTopPadding(0);
706713
infoCell.setBottomPadding(16);
707-
infoCell.setText(LocaleController.getString(R.string.TranslateMessagesInfo3));
714+
infoCell.setText(LocaleController.getString(R.string.TranslateMessagesInfo2));
708715
infoCell.setBackground(Theme.getThemedDrawableByKey(mContext, R.drawable.greydivider_top, Theme.key_windowBackgroundGrayShadow));
709716
}
710717
break;
@@ -723,15 +730,17 @@ public int getItemViewType(int i) {
723730
return VIEW_TYPE_LANGUAGE;
724731
} else {
725732
if (i-- == 0) return VIEW_TYPE_HEADER;
726-
if (i-- == 0) return VIEW_TYPE_SWITCH;
733+
if (i-- == 0) return VIEW_TYPE_SETTINGS;
734+
if (i-- == 0) return VIEW_TYPE_SHADOW;
735+
/*if (i-- == 0) return VIEW_TYPE_SWITCH;
727736
if (!getMessagesController().premiumFeaturesBlocked()) {
728737
if (i-- == 0) return VIEW_TYPE_SWITCH;
729738
}
730739
if (getChatValue() || getContextValue()) {
731740
if (i-- == 0) return VIEW_TYPE_SETTINGS;
732741
}
733742
if (i-- == 0) return VIEW_TYPE_INFO;
734-
if (i-- == 0) return VIEW_TYPE_INFO;
743+
if (i-- == 0) return VIEW_TYPE_INFO;*/
735744
if (i-- == 0) return VIEW_TYPE_HEADER;
736745
if (!unofficialLanguages.isEmpty() && (i == unofficialLanguages.size() || i == unofficialLanguages.size() + sortedLanguages.size() + 1) || unofficialLanguages.isEmpty() && i == sortedLanguages.size()) {
737746
return VIEW_TYPE_SHADOW;

TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoTranslatorSettingsActivity.java

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tw.nekomimi.nekogram.settings;
22

33
import static org.telegram.messenger.AndroidUtilities.dp;
4+
import static org.telegram.messenger.LocaleController.getPluralString;
45
import static org.telegram.messenger.LocaleController.getString;
56

67
import android.annotation.SuppressLint;
@@ -46,9 +47,12 @@
4647
import org.telegram.ui.Components.Premium.PremiumGradient;
4748
import org.telegram.ui.Components.RecyclerListView;
4849
import org.telegram.ui.Components.SeekBarView;
50+
import org.telegram.ui.Components.TranslateAlert2;
51+
import org.telegram.ui.RestrictedLanguagesSelectActivity;
4952

5053
import java.util.ArrayList;
5154
import java.util.HashMap;
55+
import java.util.HashSet;
5256
import java.util.List;
5357
import java.util.Map;
5458

@@ -90,6 +94,7 @@ public class NekoTranslatorSettingsActivity extends BaseNekoXSettingsActivity {
9094
getString(R.string.TranslatorModePopup),
9195
}, null));
9296
private final AbstractConfigCell translateToLangRow = cellGroup.appendCell(new ConfigCellCustom("TranslateToLang", CellGroup.ITEM_TYPE_TEXT_SETTINGS_CELL, true));
97+
private final AbstractConfigCell doNotTranslateRow = cellGroup.appendCell(new ConfigCellCustom("DoNotTranslate", CellGroup.ITEM_TYPE_TEXT_SETTINGS_CELL, true));
9398
private final AbstractConfigCell preferredTranslateTargetLangRow = cellGroup.appendCell(
9499
new ConfigCellTextInput(
95100
getString(R.string.PreferredTranslateTargetLangName),
@@ -165,20 +170,6 @@ public NekoTranslatorSettingsActivity() {
165170
addRowsToMap(cellGroup);
166171
}
167172

168-
protected void onItemClick(View view, int position, float ignoredX, float ignoredY) {
169-
if (position == cellGroup.rows.indexOf(translateToLangRow)) {
170-
Translator.showTargetLangSelect(view, false, (locale) -> {
171-
if (position == cellGroup.rows.indexOf(translateToLangRow)) {
172-
NekoConfig.translateToLang.setConfigString(TranslatorKt.getLocale2code(locale));
173-
} else {
174-
NekoConfig.translateInputLang.setConfigString(TranslatorKt.getLocale2code(locale));
175-
}
176-
listAdapter.notifyItemChanged(position);
177-
return Unit.INSTANCE;
178-
});
179-
}
180-
}
181-
182173
@SuppressLint("NotifyDataSetChanged")
183174
@Override
184175
protected void updateRows() {
@@ -352,6 +343,8 @@ public void onItemClick(int id) {
352343
listAdapter.notifyItemChanged(position);
353344
return Unit.INSTANCE;
354345
});
346+
} else if (position == cellGroup.rows.indexOf(doNotTranslateRow)) {
347+
presentFragment(new RestrictedLanguagesSelectActivity());
355348
} else if (position == cellGroup.rows.indexOf(articleTranslationProviderRow)) {
356349
showProviderSelectionPopup(view, NaConfig.INSTANCE.getArticleTranslationProvider(), () -> listAdapter.notifyItemChanged(position));
357350
}
@@ -486,6 +479,8 @@ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int positi
486479
}
487480
} else if (position == cellGroup.rows.indexOf(translateToLangRow)) {
488481
textCell.setTextAndValue(getString(R.string.TransToLang), NekoXConfig.formatLang(NekoConfig.translateToLang.String()), true);
482+
} else if (position == cellGroup.rows.indexOf(doNotTranslateRow)) {
483+
textCell.setTextAndValue(getString(R.string.DoNotTranslate), getRestrictedLanguages(), true, true);
489484
} else if (position == cellGroup.rows.indexOf(articleTranslationProviderRow)) {
490485
textCell.setTextAndValue(getString(R.string.ArticleTranslationProvider), getProviderName(NaConfig.INSTANCE.getArticleTranslationProvider().Int()), true);
491486
}
@@ -615,6 +610,7 @@ private void rebuildRowsForLlmProvider(int currentLlmProvider) {
615610
cellGroup.appendCell(translationProviderRow);
616611
cellGroup.appendCell(translatorModeRow);
617612
cellGroup.appendCell(translateToLangRow);
613+
cellGroup.appendCell(doNotTranslateRow);
618614
cellGroup.appendCell(preferredTranslateTargetLangRow);
619615
if (!NaConfig.INSTANCE.getGoogleTranslateExp().Bool()) {
620616
cellGroup.appendCell(googleCloudTranslateKeyRow);
@@ -656,4 +652,26 @@ private CharSequence addPremiumStar(String text) {
656652
}
657653
return new SpannableStringBuilder(text).append("  ").append(premiumStar);
658654
}
655+
656+
private String getRestrictedLanguages() {
657+
HashSet<String> langCodes = RestrictedLanguagesSelectActivity.getRestrictedLanguages();
658+
if (langCodes.isEmpty()) return "";
659+
String doNotTranslateCellValue = null;
660+
try {
661+
if (langCodes.size() < 3) {
662+
List<String> names = new ArrayList<>();
663+
for (String lang : langCodes) {
664+
String name = TranslateAlert2.languageName(lang, null);
665+
if (name != null) {
666+
names.add(TranslateAlert2.capitalFirst(name));
667+
}
668+
}
669+
doNotTranslateCellValue = TextUtils.join(", ", names);
670+
}
671+
} catch (Exception ignore) {}
672+
if (TextUtils.isEmpty(doNotTranslateCellValue)) {
673+
doNotTranslateCellValue = String.format(getPluralString("Languages", langCodes.size()), langCodes.size());
674+
}
675+
return doNotTranslateCellValue;
676+
}
659677
}

0 commit comments

Comments
 (0)