Skip to content

Commit

Permalink
Merge pull request #16 from dfyockey/settings_improvements
Browse files Browse the repository at this point in the history
Settings improvements
  • Loading branch information
dfyockey authored Oct 28, 2024
2 parents 5343e60 + 0626a6b commit e97455d
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 12 deletions.
20 changes: 20 additions & 0 deletions app/src/main/java/net/diffengine/romandigitalclock/AppClass.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
/*
* AppClass.java
* - This file is part of the Android app RomanDigital
*
* Copyright 2024 David Yockey
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package net.diffengine.romandigitalclock;

import android.app.Application;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.SwitchPreferenceCompat;
Expand All @@ -39,8 +40,10 @@ protected void onCreate(Bundle savedInstanceState) {
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.app_settings_frame, new SettingsFragment())
.replace(R.id.screen_settings_frame, new ScreenSettingsFragment())
.setReorderingAllowed(true)
.add(R.id.app_settings_frame, new SettingsFragment())
.add(R.id.screen_settings_frame, new ScreenSettingsFragment())
.add(R.id.button_bar_2, new SettingsButtonBarFragment())
.commit();
}
ActionBar actionBar = getSupportActionBar();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* SettingsButtonBarFragment.java
* - This file is part of the Android app RomanDigital
*
* Copyright 2024 David Yockey
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package net.diffengine.romandigitalclock;

import static android.app.Activity.RESULT_OK;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.preference.PreferenceManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import java.util.Map;

public class SettingsButtonBarFragment extends Fragment implements View.OnClickListener {
Activity parentActivity;
Button btnCancel;
Button btnSave;

Map<String, ?> origprefs; // Storage for backup of original preference values
SharedPreferences prefs;

public SettingsButtonBarFragment() {
// If the layout isn't provided to the superclass, it's necessary to use a form of
// fragmentTransaction.add or .replace that takes the fragment class rather than an
// instance thereof to instantiate the fragment in an activity's onCreate method.
// Otherwise, onCreateView will never be called.
super(R.layout.fragment_settings_button_bar);
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

parentActivity = requireActivity();
prefs = PreferenceManager.getDefaultSharedPreferences(requireContext());
origprefs = prefs.getAll();
}

public void onClick (View v) {

// Get the appWidgetId if we're in a widget config activity
//
//noinspection ReassignedVariable
int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
String cls = parentActivity.getComponentName().getClassName();
if (cls.equals("net.diffengine.romandigitalclock.TimeDisplayWidgetConfigActivity")) {
TimeDisplayWidgetConfigActivity widgetConfigActivity = (TimeDisplayWidgetConfigActivity) parentActivity;
appWidgetId = widgetConfigActivity.appWidgetId;
}

if (v == btnCancel) {
// Set preferences back to original values
SharedPreferences.Editor spEditor = prefs.edit();
for (String key : origprefs.keySet()) {
spEditor.putBoolean(key, (boolean) origprefs.get(key));
}
spEditor.commit();
} else if (v == btnSave) {
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
// Provision of appwidget id in the extra data should prevent crash of some UIs
// (e.g. TouchWiz on old Samsung devices) at activity destruction.
// See https://stackoverflow.com/a/40709721
Intent result = new Intent().putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
parentActivity.setResult(RESULT_OK, result);
}
} else {
// In case of some shortsighted modification... :)
return;
}

parentActivity.finish();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_settings_button_bar, container, false);

btnCancel = v.findViewById(R.id.buttonCancel);
btnCancel.setOnClickListener(this);

btnSave = v.findViewById(R.id.buttonSave);
btnSave.setOnClickListener(this);

return v;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* TimeDisplayWidgetConfigActivity
* TimeDisplayWidgetConfigActivity.java
* - This file is part of the Android app RomanDigital
*
* Copyright 2024 David Yockey
Expand All @@ -22,6 +22,7 @@

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.preference.PreferenceFragmentCompat;

Expand Down Expand Up @@ -65,7 +66,8 @@ protected void onCreate(Bundle savedInstanceState) {
.beginTransaction()
.setReorderingAllowed(true);

fragmentTransaction.replace(R.id.widget_settings, new SettingsActivity.SettingsFragment()).commit();
fragmentTransaction.add(R.id.widget_settings, new SettingsActivity.SettingsFragment());
fragmentTransaction.add(R.id.button_bar, new SettingsButtonBarFragment()).commit();
}

ActionBar actionBar = getSupportActionBar();
Expand All @@ -92,6 +94,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
}

// This method is unused and should be removed at some point
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
Expand Down
19 changes: 15 additions & 4 deletions app/src/main/res/layout/activity_time_display_widget_config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/widget_settings_scrollview"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="@+id/button_bar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:layout_constrainedHeight="true">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
Expand All @@ -49,10 +51,19 @@
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>

<androidx.fragment.app.FragmentContainerView
android:id="@id/button_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout="@layout/fragment_settings_button_bar"/>

</androidx.constraintlayout.widget.ConstraintLayout>
57 changes: 57 additions & 0 deletions app/src/main/res/layout/fragment_settings_button_bar.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
/*
* fragment_settings_button_bar.xml
* - This file is part of the Android app RomanDigital
* and is a layout resource
*
* Copyright 2024 David Yockey
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/button_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/button_bar_bkgnd"
android:orientation="horizontal"
tools:context=".SettingsButtonBarFragment" >

<Button
android:id="@+id/buttonCancel"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel"
android:textAllCaps="false"
android:textSize="16sp"
android:textStyle="bold" />

<Button
android:id="@+id/buttonSave"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save"
android:textAllCaps="false"
android:textSize="16sp"
android:textStyle="bold" />

</LinearLayout>
21 changes: 17 additions & 4 deletions app/src/main/res/layout/settings_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
tools:context=".SettingsActivity">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/app_settings_scrollview"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="@+id/button_bar_2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:layout_constrainedHeight="true">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
Expand All @@ -60,4 +64,13 @@
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

<androidx.fragment.app.FragmentContainerView
android:id="@id/button_bar_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout="@layout/fragment_settings_button_bar"/>

</androidx.constraintlayout.widget.ConstraintLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values-night/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@
<color name="transparent_85_pct">#26000000</color>
<color name="widgetText">#FAFAFA</color>
<color name="fragment_title">@color/white</color>
<color name="button_bar_bkgnd">@color/black</color>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@
<color name="transparent_85_pct">#26FFFFFF</color>
<color name="widgetText">#FAFAFA</color>
<color name="fragment_title">@color/black</color>
<color name="button_bar_bkgnd">@color/white</color>
</resources>

0 comments on commit e97455d

Please sign in to comment.