diff --git a/app/src/main/java/com/a1/nextlocation/MainActivity.java b/app/src/main/java/com/a1/nextlocation/MainActivity.java index a615490..ef12e7d 100644 --- a/app/src/main/java/com/a1/nextlocation/MainActivity.java +++ b/app/src/main/java/com/a1/nextlocation/MainActivity.java @@ -3,6 +3,9 @@ package com.a1.nextlocation; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; +import android.app.Activity; +import android.content.SharedPreferences; +import android.content.res.Configuration; import android.os.Bundle; import android.util.Log; import android.view.MenuItem; @@ -23,6 +26,7 @@ import com.google.android.material.bottomnavigation.BottomNavigationView; import java.io.File; import java.util.Arrays; +import java.util.Locale; public class MainActivity extends AppCompatActivity { private static final String TAG = MainActivity.class.getName(); @@ -52,9 +56,33 @@ public class MainActivity extends AppCompatActivity { RouteListManager.INSTANCE.setContext(this); RouteListManager.INSTANCE.load(); + // initialize saved language from sharedPreferences + setLocale(loadLocale()); + getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, new HomeFragment()).commit(); } + /** + * loads the saved language from SharedPreferences + * @return the language as string + */ + private String loadLocale(){ + SharedPreferences sharedPreferences = getSharedPreferences("Settings", Activity.MODE_PRIVATE); + return sharedPreferences.getString("Language", "nl"); + } + + /** + * sets the language of the application to the desired one + * @param language the desired language + */ + private void setLocale(String language){ + Locale locale = new Locale(language); + Locale.setDefault(locale); + Configuration configuration = new Configuration(); + configuration.setLocale(locale); + getBaseContext().getResources().updateConfiguration(configuration, getBaseContext().getResources().getDisplayMetrics()); + } + private BottomNavigationView.OnNavigationItemSelectedListener navListener = item -> { Fragment selectedFragment = null; diff --git a/app/src/main/java/com/a1/nextlocation/fragments/CouponFragment.java b/app/src/main/java/com/a1/nextlocation/fragments/CouponFragment.java index 984e38c..4279c89 100644 --- a/app/src/main/java/com/a1/nextlocation/fragments/CouponFragment.java +++ b/app/src/main/java/com/a1/nextlocation/fragments/CouponFragment.java @@ -61,22 +61,18 @@ public class CouponFragment extends Fragment { private void showPopup(Coupon coupon) { AlertDialog.Builder activateBuilder = new AlertDialog.Builder(getContext()); AlertDialog.Builder couponCodeBuilder = new AlertDialog.Builder(getContext()); - // TODO: use string resources instead of hardcoded strings - activateBuilder.setMessage("Weet je zeker dat je deze coupon wilt activeren?"); + activateBuilder.setMessage(getResources().getString(R.string.activate_question)); activateBuilder.setCancelable(true); - // TODO: use string resources instead of hardcoded strings - activateBuilder.setPositiveButton("activeren", (dialog, which) -> { - // TODO: use string resources instead of hardcoded strings + activateBuilder.setPositiveButton(R.string.activate, (dialog, which) -> { dialog.cancel(); couponCodeBuilder.setMessage("Code: " + coupon.getCode()); - couponCodeBuilder.setPositiveButton("Klaar", (dialog1, which1) -> { + couponCodeBuilder.setPositiveButton(R.string.done, (dialog1, which1) -> { dialog.cancel(); }); AlertDialog couponCodePopup = couponCodeBuilder.create(); couponCodePopup.show(); }); - // TODO: use string resources instead of hardcoded strings - activateBuilder.setNegativeButton("annuleren", (dialog, which) -> dialog.cancel()); + activateBuilder.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.cancel()); AlertDialog couponPopup = activateBuilder.create(); couponPopup.show(); diff --git a/app/src/main/java/com/a1/nextlocation/fragments/SettingsFragment.java b/app/src/main/java/com/a1/nextlocation/fragments/SettingsFragment.java index 721ba3e..9c0f42b 100644 --- a/app/src/main/java/com/a1/nextlocation/fragments/SettingsFragment.java +++ b/app/src/main/java/com/a1/nextlocation/fragments/SettingsFragment.java @@ -1,44 +1,134 @@ package com.a1.nextlocation.fragments; +import android.content.Context; +import android.content.SharedPreferences; +import android.content.res.Configuration; import android.os.Bundle; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentTransaction; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import com.a1.nextlocation.MainActivity; import com.a1.nextlocation.R; +import java.util.Locale; + public class SettingsFragment extends Fragment { + private SharedPreferences.Editor editor; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + editor = getContext().getSharedPreferences("Settings", Context.MODE_PRIVATE).edit(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - - return inflater.inflate(R.layout.fragment_settings, container, false); + + View view = inflater.inflate(R.layout.fragment_settings, container, false); + + initializeLanguageDropdown(view); + + return view; } - @Override - public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - - // Inflate the layout for this fragment - Spinner dropdown = view.findViewById(R.id.dropdown_menu_Settings); - + private void initializeLanguageDropdown(View view) { + Spinner languageDropdown = view.findViewById(R.id.dropdown_menu_Settings); String[] items = new String[]{"Nederlands", "Engels", "Chinees"}; ArrayAdapter arrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_dropdown_item, items); + languageDropdown.setAdapter(arrayAdapter); - dropdown.setAdapter(arrayAdapter); + // set the language dropdown on the currently selected language stored in the sharedPreferences + languageDropdown.setSelection(languageToDropdownPosition(getContext().getSharedPreferences("Settings", Context.MODE_PRIVATE).getString("Language", ""))); + + long previousID = languageDropdown.getSelectedItemId(); + languageDropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + setLocale(dropdownPositionToLanguage(id)); + if (id != previousID) { + Fragment currentFragment = getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_layout); + FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); + fragmentTransaction.detach(currentFragment); + fragmentTransaction.attach(currentFragment); + fragmentTransaction.commit(); + } + } + + @Override + public void onNothingSelected(AdapterView parent) { + } + }); + } + + /** + * converts the languageDropdown position to the belonging language + * + * @param id desired position to convert + * @return the language belonging to the position of the languageDropdown + */ + private String dropdownPositionToLanguage(long id) { + switch ((int) id) { + case 0: + return "nl"; + case 1: + return "en"; + default: + return ""; + } + } + + /** + * converts language to the languageDropdown position + * + * @param language desired language to convert + * @return the position of the language in the languageDropdown + */ + private int languageToDropdownPosition(String language) { + switch (language) { + case "nl": + return 0; + case "en": + return 1; + default: + return 1; + } + } + + /** + * reloads the fragment + */ + private void refresh() { + Fragment currentFragment = getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_layout); + FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); + fragmentTransaction.detach(currentFragment); + fragmentTransaction.attach(currentFragment); + fragmentTransaction.commit(); + } + + /** + * changes the current language to the desired language and saves this setting in SharedPreferences + * + * @param language the desired language to translate to + */ + private void setLocale(String language) { + Locale locale = new Locale(language); + Locale.setDefault(locale); + Configuration config = new Configuration(); + config.setLocale(locale); + getContext().getResources().updateConfiguration(config, getContext().getResources().getDisplayMetrics()); + editor.putString("Language", language); + editor.apply(); } } \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_settings.xml b/app/src/main/res/layout/fragment_settings.xml index c0cb215..a97cf2b 100644 --- a/app/src/main/res/layout/fragment_settings.xml +++ b/app/src/main/res/layout/fragment_settings.xml @@ -45,7 +45,7 @@ + + + Next Location + Locaties + Routes + Statistieken + Instellingen + Taal + Imperiaal systeem + 65+ stand + Kleurenblind + Totale afstand: + Bezochte locaties: + Totale tijd: + Coupons gespaard: + Coupons + Start Route + Weet je zeker dat je deze coupon wilt activeren? + activeren + Klaar + annuleren + \ 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 c322028..1cfcd8d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,20 +1,21 @@ - Next Location - - Hello blank fragment - Locaties + Next Location + Location Routes - Statistieken - Instellingen - Taal - Imperiaal systeem - 65+ stand - Kleurenblind - Statistieken - Totale afstand: - Bezochte locaties: - Totale tijd: - Coupons gespaard: + Statistics + Settings + Language + Imperial system + 65+ mode + Colorblind + Total distance: + Visited locations: + Total time: + Coupons collected: Coupons Start Route + Are you sure you want to activate this coupon? + activate + Done + cancel \ No newline at end of file