mergeeee
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ import com.a1.nextlocation.data.RouteHandler;
|
||||
import com.a1.nextlocation.data.StaticData;
|
||||
import com.a1.nextlocation.json.DirectionsResult;
|
||||
import com.a1.nextlocation.network.ApiHandler;
|
||||
import com.a1.nextlocation.network.DirectionsListener;
|
||||
import com.a1.nextlocation.recyclerview.LocationListManager;
|
||||
|
||||
import org.osmdroid.api.IMapController;
|
||||
|
||||
@@ -49,4 +49,8 @@ public class LocationDetailFragment extends Fragment {
|
||||
}
|
||||
return view;
|
||||
}
|
||||
|
||||
public void setLocation(Location location) {
|
||||
this.location = location;
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import androidx.fragment.app.FragmentActivity;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
@@ -15,13 +16,14 @@ import android.widget.Toast;
|
||||
import com.a1.nextlocation.R;
|
||||
import com.a1.nextlocation.data.Route;
|
||||
import com.a1.nextlocation.data.RouteHandler;
|
||||
import com.a1.nextlocation.data.StaticData;
|
||||
import com.a1.nextlocation.network.ApiHandler;
|
||||
|
||||
public class RouteDetailFragment extends Fragment {
|
||||
|
||||
private Route route;
|
||||
private TextView routeDetailText;
|
||||
private TextView routeName;
|
||||
private ImageButton imageButton;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
@@ -36,8 +38,18 @@ public class RouteDetailFragment extends Fragment {
|
||||
this.route = getArguments().getParcelable("route");
|
||||
}
|
||||
|
||||
this.routeName = view.findViewById(R.id.route_title);
|
||||
this.routeName.setText(this.route.getName());
|
||||
|
||||
this.routeDetailText = view.findViewById(R.id.reoute_detail_tekst);
|
||||
this.routeDetailText.setText(this.route.getName());
|
||||
this.routeDetailText.setText(this.route.getDescription());
|
||||
|
||||
this.imageButton = view.findViewById(R.id.route_detail_back_button);
|
||||
this.imageButton.setOnClickListener(v -> {
|
||||
RouteFragment routeFragment = new RouteFragment();
|
||||
((FragmentActivity) view.getContext()).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, routeFragment).addToBackStack(null).commit();
|
||||
});
|
||||
|
||||
Button startButton = view.findViewById(R.id.start_route_button);
|
||||
startButton.setOnClickListener(this::startRoute);
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
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;
|
||||
@@ -8,10 +10,12 @@ import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.widget.SwitchCompat;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
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.ImageView;
|
||||
import android.widget.Spinner;
|
||||
@@ -19,8 +23,12 @@ 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;
|
||||
|
||||
private ImageView imageButton;
|
||||
|
||||
SwitchCompat fontChanger;
|
||||
@@ -28,21 +36,16 @@ public class SettingsFragment extends Fragment {
|
||||
@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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
View view = inflater.inflate(R.layout.fragment_settings, container, false);
|
||||
|
||||
// Inflate the layout for this fragment
|
||||
Spinner dropdown = view.findViewById(R.id.dropdown_menu_Settings);
|
||||
initializeLanguageDropdown(view);
|
||||
|
||||
this.imageButton = view.findViewById(R.id.settings_back_button);
|
||||
this.imageButton.setOnClickListener(v -> {
|
||||
@@ -79,9 +82,95 @@ public class SettingsFragment extends Fragment {
|
||||
editor.commit();
|
||||
});
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
private void initializeLanguageDropdown(View view) {
|
||||
Spinner languageDropdown = view.findViewById(R.id.dropdown_menu_Settings);
|
||||
String[] items = new String[]{"Nederlands", "Engels", "Chinees"};
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user