Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ public class UpdateApplication extends Application {
protected void attachBaseContext(Context base) {
super.attachBaseContext(LocaleHelper.onAttach(base, "en"));
}
}

@Override
public void onCreate() {
super.onCreate();
LocaleHelper.applyPersistedLocales(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.example.updateapp.R;
Expand All @@ -17,9 +18,9 @@

public class LanguageAdapter extends RecyclerView.Adapter<LanguageAdapter.LanguageViewHolder> {

private Context context;
private List<LanguageModel> languageList;
private OnLanguageSelectedListener listener;
private final Context context;
private final List<LanguageModel> languageList;
private final OnLanguageSelectedListener listener;

public interface OnLanguageSelectedListener {
void onLanguageSelected(LanguageModel language, int position);
Expand All @@ -41,18 +42,24 @@ public LanguageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int view
@Override
public void onBindViewHolder(@NonNull LanguageViewHolder holder, int position) {
LanguageModel language = languageList.get(position);

holder.txtLanguageName.setText(language.getLanguageName());

if (language.isSelected()) {
holder.imgSelected.setVisibility(View.VISIBLE);
} else {
holder.imgSelected.setVisibility(View.GONE);
}

boolean selected = language.isSelected();
holder.itemView.setBackgroundResource(
selected ? R.drawable.bg_language_item_selected : R.drawable.bg_language_item
);
holder.imgSelected.setVisibility(selected ? View.VISIBLE : View.INVISIBLE);
holder.imgSelected.setContentDescription(
selected ? context.getString(R.string.selected_language) : null
);
holder.txtLanguageName.setTextColor(
ContextCompat.getColor(context, selected ? R.color.themeColor : R.color.black)
);
holder.itemView.setSelected(selected);

holder.itemView.setOnClickListener(v -> {
if (listener != null) {
listener.onLanguageSelected(language, position);
listener.onLanguageSelected(language, holder.getBindingAdapterPosition());
}
});
}
Expand All @@ -79,4 +86,4 @@ public LanguageViewHolder(@NonNull View itemView) {
imgSelected = itemView.findViewById(R.id.img_selected);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,70 +8,65 @@
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.viewpager.widget.PagerAdapter;

import com.example.updateapp.R;

public class OnboardingAdapter extends PagerAdapter {

Context context;
LayoutInflater layoutInflater;
private final Context context;

public OnboardingAdapter(Context context) {
this.context = context;
}

String titles[] = {
"Welcome to FinTrack",
"Track • Analyze • Improve",
"Build Better Money Habits"
private final int[] titles = {
R.string.onboarding_title_1,
R.string.onboarding_title_2,
R.string.onboarding_title_3
};

String subtitles[] ={
"Your all-in-one AI companion for smarter spending,\nbetter savings, and complete financial clarity." ,
"Understand your expenses with clean visuals, smart insights, and real-time spending patterns\nthat keep you in control.",
"Set budgets, stay organized, and make confident \nfinancial decisions with powerful AI-Chatbot tools."
private final int[] subtitles = {
R.string.onboarding_subtitle_1,
R.string.onboarding_subtitle_2,
R.string.onboarding_subtitle_3
};

int images[] = {
private final int[] images = {
R.drawable.bg_1,
R.drawable.bg_2,
R.drawable.bg_3
};

public OnboardingAdapter(Context context) {
this.context = context;
}

@Override
public int getCount() {
return titles.length;
}

@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == (ConstraintLayout) object;
return view == object;
}

@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View v = layoutInflater.inflate(R.layout.slide, container, false);
View v = LayoutInflater.from(context).inflate(R.layout.slide, container, false);

ImageView image = v.findViewById(R.id.slideImg);
TextView title = v.findViewById(R.id.sliderTitle);
TextView subtitle = v.findViewById(R.id.sliderSubTitle);

image.setImageResource(images[position]);
title.setText(titles[position]);
subtitle.setText(subtitles[position]);
title.setText(context.getString(titles[position]));
subtitle.setText(context.getString(subtitles[position]));

container.addView(v);

return v;

}

@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((ConstraintLayout) object);
container.removeView((View) object);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ public boolean isSelected() {
public void setSelected(boolean selected) {
isSelected = selected;
}
}
}
75 changes: 32 additions & 43 deletions app/src/main/java/com/example/updateapp/utils/LocaleHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;

import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.os.LocaleListCompat;

import java.util.Locale;

Expand All @@ -14,29 +16,44 @@ public class LocaleHelper {
private static final String PREFS_NAME = "app_prefs";

public static Context onAttach(Context context) {
String lang = getPersistedData(context, "en");
String country = getPersistedCountry(context, "");
return setLocale(context, lang, country);
return onAttach(context, "en");
}

public static Context onAttach(Context context, String defaultLanguage) {
String lang = getPersistedData(context, defaultLanguage);
String country = getPersistedCountry(context, "");
return setLocale(context, lang, country);
return wrap(context, lang, country);
}

public static String getLanguage(Context context) {
return getPersistedData(context, "en");
}

public static Context setLocale(Context context, String language, String country) {
persist(context, language, country);
public static String getCountry(Context context) {
return getPersistedCountry(context, "");
}

public static void applyPersistedLocales(Context context) {
applyAppLocales(getLanguage(context), getCountry(context));
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return updateResources(context, language, country);
public static Context setLocale(Context context, String language, String country) {
if (language == null || language.isEmpty()) {
language = "en";
}
if (country == null || "en".equals(language)) {
country = "";
}
persist(context, language, country);
applyAppLocales(language, country);
return wrap(context, language, country);
}

return updateResourcesLegacy(context, language, country);
private static void applyAppLocales(String language, String country) {
String tag = (country == null || country.isEmpty())
? language
: language + "-" + country;
AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags(tag));
}

private static String getPersistedData(Context context, String defaultLanguage) {
Expand All @@ -53,11 +70,11 @@ private static void persist(Context context, String language, String country) {
SharedPreferences preferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(SELECTED_LANGUAGE, language);
editor.putString(SELECTED_COUNTRY, country);
editor.putString(SELECTED_COUNTRY, country == null ? "" : country);
editor.apply();
}

private static Context updateResources(Context context, String language, String country) {
private static Context wrap(Context context, String language, String country) {
Locale locale;
if (country != null && !country.isEmpty()) {
locale = new Locale(language, country);
Expand All @@ -66,38 +83,10 @@ private static Context updateResources(Context context, String language, String
}
Locale.setDefault(locale);

Configuration configuration = context.getResources().getConfiguration();
Resources resources = context.getResources();
Configuration configuration = new Configuration(resources.getConfiguration());
configuration.setLocale(locale);
configuration.setLayoutDirection(locale);

return context.createConfigurationContext(configuration);
}

private static Context updateResourcesLegacy(Context context, String language, String country) {
Locale locale;
if (country != null && !country.isEmpty()) {
locale = new Locale(language, country);
} else {
locale = new Locale(language);
}
Locale.setDefault(locale);

Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLayoutDirection(locale);
}

resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return context;
}

public static void clearLanguagePreference(Context context) {
SharedPreferences preferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.remove(SELECTED_LANGUAGE);
editor.remove(SELECTED_COUNTRY);
editor.apply();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private void setupLanguageList() {
languageList = new ArrayList<>();

// Add all 10 supported languages
languageList.add(new LanguageModel(getString(R.string.lang_english), "en", "US"));
languageList.add(new LanguageModel(getString(R.string.lang_english), "en", ""));
languageList.add(new LanguageModel(getString(R.string.lang_chinese), "zh", "CN"));
languageList.add(new LanguageModel(getString(R.string.lang_hindi), "hi", "IN"));
languageList.add(new LanguageModel(getString(R.string.lang_spanish), "es", "ES"));
Expand All @@ -77,18 +77,17 @@ private void setupLanguageList() {
languageList.add(new LanguageModel(getString(R.string.lang_russian), "ru", "RU"));
languageList.add(new LanguageModel(getString(R.string.lang_urdu), "ur", "PK"));

// Set current language as selected
String currentLanguage = LocaleHelper.getLanguage(this);
boolean found = false;
for (int i = 0; i < languageList.size(); i++) {
if (languageList.get(i).getLanguageCode().equals(currentLanguage)) {
languageList.get(i).setSelected(true);
selectedPosition = i;
found = true;
break;
}
}

// If no match found, default to English (position 0)
if (selectedPosition == 0 && !currentLanguage.equals("en")) {
if (!found) {
languageList.get(0).setSelected(true);
selectedPosition = 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public void onComplete(@NonNull Task<AuthResult> task) {
this,
getString(R.string.google_signin_error, e.getStatusCode(), e.getMessage()),
Toast.LENGTH_LONG
).show();
) .show();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,15 @@ private void verifyCredential(PhoneAuthCredential credential) {
dialog.dismiss();

Exception e = linkTask.getException();
String msg = e != null ? e.getMessage() : "Unknown linking error";
String msg = e != null ? e.getMessage() : getString(R.string.unknown_linking_error);

if (e instanceof FirebaseAuthUserCollisionException) {
Toast.makeText(OTPActivity.this,
"This email is already registered with a different account. Please login with that email or use another email.",
getString(R.string.email_already_registered_different),
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(OTPActivity.this,
"Failed to link email: " + msg,
getString(R.string.failed_to_link_email, msg),
Toast.LENGTH_LONG).show();
}
}
Expand Down Expand Up @@ -224,7 +224,7 @@ private void saveUserToFirestoreAndContinue(String uid, String name, String emai
})
.addOnFailureListener(e -> {
if (dialog.isShowing()) dialog.dismiss();
Toast.makeText(OTPActivity.this, "Failed to save user data: " + e.getMessage(), Toast.LENGTH_LONG).show();
Toast.makeText(OTPActivity.this, getString(R.string.failed_to_save_user, e.getMessage()), Toast.LENGTH_LONG).show();
});
}

Expand Down
Loading