1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-26 20:06:52 +01:00

Add theme selector to first screen

This commit is contained in:
Arjan Schrijver 2024-09-05 14:14:51 +02:00
parent 19ef63073d
commit 256d3938a5
2 changed files with 50 additions and 1 deletions

View File

@ -16,7 +16,10 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities.welcome;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -24,11 +27,18 @@ import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import com.google.android.material.textfield.MaterialAutoCompleteTextView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
public class WelcomeFragmentIntro extends Fragment {
private static final Logger LOG = LoggerFactory.getLogger(WelcomeFragmentIntro.class);
@ -37,6 +47,28 @@ public class WelcomeFragmentIntro extends Fragment {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.fragment_welcome_intro, container, false);
final View view = inflater.inflate(R.layout.fragment_welcome_intro, container, false);
final String[] themes = getResources().getStringArray(R.array.pref_theme_values);
final Prefs prefs = GBApplication.getPrefs();
final String currentTheme = prefs.getString("pref_key_theme", getString(R.string.pref_theme_value_system));
final int currentThemeIndex = Arrays.asList(themes).indexOf(currentTheme);
final MaterialAutoCompleteTextView themeMenu = view.findViewById(R.id.app_theme_dropdown_menu);
themeMenu.setSaveEnabled(false); // https://github.com/material-components/material-components-android/issues/1464#issuecomment-1258051448
themeMenu.setText(getResources().getStringArray(R.array.pref_theme_options)[currentThemeIndex], false);
themeMenu.setOnItemClickListener((adapterView, view1, i, l) -> {
final SharedPreferences.Editor editor = prefs.getPreferences().edit();
editor.putString("pref_key_theme", themes[i]).apply();
final Handler handler = new Handler();
handler.postDelayed(() -> {
// Delay recreation of the Activity to give the dropdown some time to settle.
// If we recreate it immediately, the theme popup will reopen, which is not what the user expects.
Intent intent = new Intent();
intent.setAction(GBApplication.ACTION_THEME_CHANGE);
LocalBroadcastManager.getInstance(requireContext()).sendBroadcast(intent);
}, 500);
});
return view;
}
}

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
@ -40,5 +41,21 @@
android:textAlignment="center"
android:text="@string/first_start_intro_tag_line" />
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.Material3.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:id="@+id/app_theme_dropdown_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:hint="@string/pref_title_theme">
<AutoCompleteTextView
android:id="@+id/app_theme_dropdown_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
app:simpleItems="@array/pref_theme_options" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
</RelativeLayout>