Merge branch 'languages' into develop

This commit is contained in:
Bart
2021-01-06 10:49:35 +01:00
7 changed files with 180 additions and 42 deletions

View File

@@ -3,6 +3,9 @@ package com.a1.nextlocation;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.MenuItem; import android.view.MenuItem;
@@ -23,6 +26,7 @@ import com.google.android.material.bottomnavigation.BottomNavigationView;
import java.io.File; import java.io.File;
import java.util.Arrays; import java.util.Arrays;
import java.util.Locale;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getName(); private static final String TAG = MainActivity.class.getName();
@@ -52,9 +56,33 @@ public class MainActivity extends AppCompatActivity {
RouteListManager.INSTANCE.setContext(this); RouteListManager.INSTANCE.setContext(this);
RouteListManager.INSTANCE.load(); RouteListManager.INSTANCE.load();
// initialize saved language from sharedPreferences
setLocale(loadLocale());
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, new HomeFragment()).commit(); 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 -> { private BottomNavigationView.OnNavigationItemSelectedListener navListener = item -> {
Fragment selectedFragment = null; Fragment selectedFragment = null;

View File

@@ -61,22 +61,18 @@ public class CouponFragment extends Fragment {
private void showPopup(Coupon coupon) { private void showPopup(Coupon coupon) {
AlertDialog.Builder activateBuilder = new AlertDialog.Builder(getContext()); AlertDialog.Builder activateBuilder = new AlertDialog.Builder(getContext());
AlertDialog.Builder couponCodeBuilder = new AlertDialog.Builder(getContext()); AlertDialog.Builder couponCodeBuilder = new AlertDialog.Builder(getContext());
// TODO: use string resources instead of hardcoded strings activateBuilder.setMessage(getResources().getString(R.string.activate_question));
activateBuilder.setMessage("Weet je zeker dat je deze coupon wilt activeren?");
activateBuilder.setCancelable(true); activateBuilder.setCancelable(true);
// TODO: use string resources instead of hardcoded strings activateBuilder.setPositiveButton(R.string.activate, (dialog, which) -> {
activateBuilder.setPositiveButton("activeren", (dialog, which) -> {
// TODO: use string resources instead of hardcoded strings
dialog.cancel(); dialog.cancel();
couponCodeBuilder.setMessage("Code: " + coupon.getCode()); couponCodeBuilder.setMessage("Code: " + coupon.getCode());
couponCodeBuilder.setPositiveButton("Klaar", (dialog1, which1) -> { couponCodeBuilder.setPositiveButton(R.string.done, (dialog1, which1) -> {
dialog.cancel(); dialog.cancel();
}); });
AlertDialog couponCodePopup = couponCodeBuilder.create(); AlertDialog couponCodePopup = couponCodeBuilder.create();
couponCodePopup.show(); couponCodePopup.show();
}); });
// TODO: use string resources instead of hardcoded strings activateBuilder.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.cancel());
activateBuilder.setNegativeButton("annuleren", (dialog, which) -> dialog.cancel());
AlertDialog couponPopup = activateBuilder.create(); AlertDialog couponPopup = activateBuilder.create();
couponPopup.show(); couponPopup.show();

View File

@@ -1,44 +1,134 @@
package com.a1.nextlocation.fragments; package com.a1.nextlocation.fragments;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Spinner; import android.widget.Spinner;
import com.a1.nextlocation.MainActivity; import com.a1.nextlocation.MainActivity;
import com.a1.nextlocation.R; import com.a1.nextlocation.R;
import java.util.Locale;
public class SettingsFragment extends Fragment { public class SettingsFragment extends Fragment {
private SharedPreferences.Editor editor;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
editor = getContext().getSharedPreferences("Settings", Context.MODE_PRIVATE).edit();
} }
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { 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 private void initializeLanguageDropdown(View view) {
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { Spinner languageDropdown = view.findViewById(R.id.dropdown_menu_Settings);
super.onViewCreated(view, savedInstanceState);
// Inflate the layout for this fragment
Spinner dropdown = view.findViewById(R.id.dropdown_menu_Settings);
String[] items = new String[]{"Nederlands", "Engels", "Chinees"}; String[] items = new String[]{"Nederlands", "Engels", "Chinees"};
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_dropdown_item, items); ArrayAdapter<String> 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();
} }
} }

View File

@@ -45,7 +45,7 @@
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/taal" android:text="@string/language"
android:textColor="@color/black" android:textColor="@color/black"
android:textSize="20sp" android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
@@ -93,7 +93,7 @@
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/imperiaal_systeem" android:text="@string/imperial_system"
android:textColor="@color/black" android:textColor="@color/black"
android:textSize="20sp" android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
@@ -138,7 +138,7 @@
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/_65_stand" android:text="@string/_65_mode"
android:textColor="@color/black" android:textColor="@color/black"
android:textSize="20sp" android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
@@ -183,7 +183,7 @@
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/kleurenblind" android:text="@string/colorblind"
android:textColor="@color/black" android:textColor="@color/black"
android:textSize="20sp" android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"

View File

@@ -12,7 +12,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="10dp" android:layout_marginTop="10dp"
android:text="@string/statistieken" android:text="@string/statistics"
android:textColor="@color/white" android:textColor="@color/white"
android:textSize="30sp" android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
@@ -35,7 +35,7 @@
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/totale_afstand" android:text="@string/total_distance"
android:textColor="@color/black" android:textColor="@color/black"
android:textSize="20sp" android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
@@ -85,7 +85,7 @@
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/bezochte_locaties" android:text="@string/visited_locations"
android:textColor="@color/black" android:textColor="@color/black"
android:textSize="20sp" android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
@@ -133,7 +133,7 @@
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/totale_tijd" android:text="@string/total_time"
android:textColor="@color/black" android:textColor="@color/black"
android:textSize="20sp" android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
@@ -181,7 +181,7 @@
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/coupons_gespaard" android:text="@string/coupons_collected"
android:textColor="@color/black" android:textColor="@color/black"
android:textSize="20sp" android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name" translatable="false">Next Location</string>
<string name="locations">Locaties</string>
<string name="routes">Routes</string>
<string name="statistics">Statistieken</string>
<string name="settings">Instellingen</string>
<string name="language">Taal</string>
<string name="imperial_system">Imperiaal systeem</string>
<string name="_65_mode">65+ stand</string>
<string name="colorblind">Kleurenblind</string>
<string name="total_distance">Totale afstand:</string>
<string name="visited_locations">Bezochte locaties:</string>
<string name="total_time">Totale tijd:</string>
<string name="coupons_collected">Coupons gespaard:</string>
<string name="coupons">Coupons</string>
<string name="start_route">Start Route</string>
<string name="activate_question">Weet je zeker dat je deze coupon wilt activeren?</string>
<string name="activate">activeren</string>
<string name="done">Klaar</string>
<string name="cancel">annuleren</string>
</resources>

View File

@@ -1,20 +1,21 @@
<resources> <resources>
<string name="app_name">Next Location</string> <string name="app_name" translatable="false">Next Location</string>
<!-- TODO: Remove or change this placeholder text --> <string name="locations">Location</string>
<string name="hello_blank_fragment">Hello blank fragment</string>
<string name="locations">Locaties</string>
<string name="routes">Routes</string> <string name="routes">Routes</string>
<string name="statistics">Statistieken</string> <string name="statistics">Statistics</string>
<string name="settings">Instellingen</string> <string name="settings">Settings</string>
<string name="taal">Taal</string> <string name="language">Language</string>
<string name="imperiaal_systeem">Imperiaal systeem</string> <string name="imperial_system">Imperial system</string>
<string name="_65_stand">65+ stand</string> <string name="_65_mode">65+ mode</string>
<string name="kleurenblind">Kleurenblind</string> <string name="colorblind">Colorblind</string>
<string name="statistieken">Statistieken</string> <string name="total_distance">Total distance:</string>
<string name="totale_afstand">Totale afstand:</string> <string name="visited_locations">Visited locations:</string>
<string name="bezochte_locaties">Bezochte locaties:</string> <string name="total_time">Total time:</string>
<string name="totale_tijd">Totale tijd:</string> <string name="coupons_collected">Coupons collected:</string>
<string name="coupons_gespaard">Coupons gespaard:</string>
<string name="coupons">Coupons</string> <string name="coupons">Coupons</string>
<string name="start_route">Start Route</string> <string name="start_route">Start Route</string>
<string name="activate_question">Are you sure you want to activate this coupon?</string>
<string name="activate">activate</string>
<string name="done">Done</string>
<string name="cancel">cancel</string>
</resources> </resources>