1+
2+ package com .ahmedjazzar .rosetta ;
3+
4+ import android .app .Dialog ;
5+ import android .os .Bundle ;
6+ import android .widget .Button ;
7+ import android .widget .TextView ;
8+
9+ import androidx .annotation .NonNull ;
10+ import androidx .appcompat .app .AlertDialog ;
11+ import androidx .fragment .app .DialogFragment ;
12+ import androidx .fragment .app .FragmentActivity ;
13+
14+ import java .util .ArrayList ;
15+ import java .util .Locale ;
16+ import java .util .Objects ;
17+
18+ import com .fox2code .mmm .R ;
19+ import com .google .android .material .dialog .MaterialAlertDialogBuilder ;
20+
21+ /**
22+ * This fragment is responsible for displaying the supported locales and performing any necessary
23+ * action that allows user to select, cancel, and commit changes.
24+ *
25+ * Created by ahmedjazzar on 1/19/16.
26+ */
27+
28+ public class LanguagesListDialogFragment extends DialogFragment {
29+
30+ private final int DIALOG_TITLE_ID = R .string .language ;
31+ private final int DIALOG_POSITIVE_ID = R .string .ok ;
32+ private final int DIALOG_NEGATIVE_ID = R .string .cancel ;
33+
34+ private int mSelectedLanguage = -1 ;
35+ private final Logger mLogger ;
36+
37+ public LanguagesListDialogFragment () {
38+ String TAG = LanguagesListDialogFragment .class .getName ();
39+ this .mLogger = new Logger (TAG );
40+ }
41+
42+ /**
43+ * @return a Dialog fragment
44+ */
45+ @ NonNull
46+ @ Override
47+ public Dialog onCreateDialog (Bundle savedInstanceState ) {
48+ final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder (requireActivity ());
49+ mLogger .debug ("Building DialogFragment." );
50+
51+ builder .setTitle (getString (DIALOG_TITLE_ID ))
52+ .setSingleChoiceItems (
53+ getLanguages (),
54+ getCurrentLocaleIndex (),
55+ (dialogInterface , which ) -> onLanguageSelectedLocalized (which ))
56+ .setPositiveButton (
57+ getString (DIALOG_POSITIVE_ID ).toUpperCase (),
58+ (dialogInterface , which ) -> onPositiveClick ())
59+ .setNegativeButton (
60+ getString (DIALOG_NEGATIVE_ID ).toUpperCase (),
61+ (dialogInterface , which ) -> onNegativeClick ());
62+
63+ mLogger .verbose ("DialogFragment built." );
64+ return builder .create ();
65+ }
66+
67+ /**
68+ * @param which the position of the selected locale
69+ */
70+ protected void onLanguageSelected (int which ) {
71+ // just update the selected locale
72+ mSelectedLanguage = which ;
73+ }
74+
75+ /**
76+ * Localizing the dialog buttons and title
77+ * @param which the position of the selected locale
78+ */
79+ protected void onLanguageSelectedLocalized (int which ) {
80+
81+ // update the selected locale
82+ mSelectedLanguage = which ;
83+ AlertDialog dialog = (AlertDialog ) getDialog ();
84+
85+ mLogger .debug ("Displaying dialog main strings in the selected " +
86+ "locale" );
87+
88+ onLanguageSelectedLocalized (
89+ which ,
90+ null ,
91+ dialog .getButton (AlertDialog .BUTTON_POSITIVE ),
92+ dialog .getButton (AlertDialog .BUTTON_NEGATIVE ));
93+ }
94+
95+ /**
96+ * the position of the selected locale given the ids
97+ * @param which the position of the selected locale
98+ * @param titleView dialog's title text view
99+ * @param positiveButton positive button
100+ * @param negativeButton negative button
101+ */
102+ protected void onLanguageSelectedLocalized (int which , TextView titleView , Button positiveButton ,
103+ Button negativeButton ) {
104+
105+ // update the selected locale
106+ mSelectedLanguage = which ;
107+ Locale locale = LocalesUtils .getLocaleFromIndex (mSelectedLanguage );
108+ AlertDialog dialog = (AlertDialog ) getDialog ();
109+ FragmentActivity activity = getActivity ();
110+
111+ mLogger .debug ("Displaying dialog main strings in the selected " +
112+ "locale" );
113+
114+ assert activity != null ;
115+ String LocalizedTitle = LocalesUtils .getInSpecificLocale (activity , locale , DIALOG_TITLE_ID );
116+ if (titleView == null ) {
117+ // Display dialog title in the selected locale
118+ assert dialog != null ;
119+ dialog .setTitle (LocalizedTitle );
120+ } else {
121+ titleView .setText (LocalizedTitle );
122+ }
123+
124+ // Display positive button text in the selected locale
125+ positiveButton .setText (LocalesUtils .getInSpecificLocale (
126+ activity , locale , DIALOG_POSITIVE_ID ));
127+
128+ // Display negative button text in the selected locale
129+ negativeButton .setText (LocalesUtils .getInSpecificLocale (
130+ activity , locale , DIALOG_NEGATIVE_ID ));
131+ }
132+
133+ /**
134+ * called when the user approved changing locale
135+ */
136+ protected void onPositiveClick () {
137+
138+ // if the user did not select the same locale go ahead, else ignore
139+ if (mSelectedLanguage != -1 &&
140+ mSelectedLanguage != LocalesUtils .getCurrentLocaleIndex ()) {
141+
142+ // Try changing the locale
143+ if (LocalesUtils .setAppLocale (
144+ getActivity (), mSelectedLanguage )) {
145+
146+ mLogger .info ("App locale changed successfully." );
147+ LocalesUtils .refreshApplication (requireActivity ());
148+ } else {
149+ mLogger .error ("Unsuccessful trial to change the App locale." );
150+ // TODO: notify the user that his request not placed
151+ }
152+ } else {
153+ dismiss ();
154+ }
155+ }
156+
157+ /**
158+ * called when the user discarded changing locale
159+ */
160+ protected void onNegativeClick () {
161+ mLogger .verbose ("User discarded changing language." );
162+ mLogger .debug ("Return to the original locale." );
163+ this .onLanguageSelectedLocalized (this .getCurrentLocaleIndex ());
164+ }
165+
166+ /**
167+ *
168+ * @return available languages
169+ */
170+ protected String [] getLanguages () {
171+ ArrayList <String > languages = LocalesUtils .getLocalesWithDisplayName ();
172+ return languages .toArray (new String [languages .size ()]);
173+ }
174+
175+ /**
176+ *
177+ * @return the index of the locale that app is using now
178+ */
179+ protected int getCurrentLocaleIndex () {
180+ return LocalesUtils .getCurrentLocaleIndex ();
181+ }
182+
183+ }
0 commit comments