Skip to content

Commit 7e25617

Browse files
committed
Move theme checkbox to settings activity
1 parent c7909b4 commit 7e25617

23 files changed

+118
-121
lines changed

app/src/main/java/fr/free/nrw/commons/AboutActivity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import android.text.method.LinkMovementMethod;
66
import android.widget.TextView;
77

8+
import fr.free.nrw.commons.theme.BaseActivity;
9+
810
public class AboutActivity extends BaseActivity {
911
private TextView versionText;
1012
private TextView licenseText;

app/src/main/java/fr/free/nrw/commons/BaseActivity.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

app/src/main/java/fr/free/nrw/commons/BaseAppCompatActivity.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

app/src/main/java/fr/free/nrw/commons/SettingsActivity.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package fr.free.nrw.commons;
22

33
import android.content.Context;
4+
import android.content.Intent;
45
import android.content.SharedPreferences;
56
import android.content.res.Configuration;
67
import android.os.Bundle;
78
import android.preference.ListPreference;
89
import android.preference.Preference;
910
import android.preference.PreferenceActivity;
11+
import android.preference.PreferenceManager;
1012
import android.support.annotation.LayoutRes;
13+
import android.support.v4.content.IntentCompat;
1114
import android.support.v7.app.AppCompatDelegate;
1215
import android.view.MenuInflater;
1316
import android.view.View;
@@ -23,10 +26,10 @@ protected void onCreate(Bundle savedInstanceState) {
2326
getDelegate().installViewFactory();
2427
getDelegate().onCreate(savedInstanceState);
2528
//Check prefs on every activity starts
26-
if (getSharedPreferences("prefs", Context.MODE_PRIVATE).getBoolean("theme", false)) {
27-
setTheme(R.style.LightAppTheme);
29+
if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("theme",false)) {
30+
setTheme(R.style.DarkAppTheme);
2831
}else {
29-
setTheme(R.style.DarkAppTheme); //default
32+
setTheme(R.style.LightAppTheme); //default
3033
}
3134
super.onCreate(savedInstanceState);
3235
super.onCreate(savedInstanceState);
@@ -76,7 +79,12 @@ protected void onPause() {
7679

7780
@Override
7881
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
79-
82+
if(key.equals("theme")){
83+
//Finish current activity and tart new one with selected theme
84+
Intent intent = getIntent();
85+
finish();
86+
startActivity(intent);
87+
}
8088
}
8189

8290
// All the stuff below is just to get a actionbar that says settings...

app/src/main/java/fr/free/nrw/commons/WelcomeActivity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import com.viewpagerindicator.CirclePageIndicator;
1111

12+
import fr.free.nrw.commons.theme.BaseActivity;
13+
1214
public class WelcomeActivity extends BaseActivity {
1315
static final int PAGE_WIKIPEDIA = 0,
1416
PAGE_DO_UPLOAD = 1,

app/src/main/java/fr/free/nrw/commons/auth/AuthenticatedActivity.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
import android.os.Bundle;
1212
import android.support.annotation.Nullable;
1313
import android.support.v4.app.ActivityCompat;
14-
import android.support.v7.app.AppCompatActivity;
1514

1615
import java.io.IOException;
1716

18-
import fr.free.nrw.commons.BaseAppCompatActivity;
17+
import fr.free.nrw.commons.theme.BaseAppCompatActivity;
1918
import fr.free.nrw.commons.CommonsApplication;
2019
import fr.free.nrw.commons.Utils;
2120

app/src/main/java/fr/free/nrw/commons/auth/SignupActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import android.webkit.WebViewClient;
99
import android.widget.Toast;
1010

11-
import fr.free.nrw.commons.BaseActivity;
11+
import fr.free.nrw.commons.theme.BaseActivity;
1212

1313
public class SignupActivity extends BaseActivity {
1414

app/src/main/java/fr/free/nrw/commons/contributions/ContributionsActivity.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ protected void onCreate(Bundle savedInstanceState) {
125125
if (mediaDetails != null && savedInstanceState.getBoolean("mediaDetailsVisible")) {
126126
// Feels awful that we have to reset this manually!
127127
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
128-
getSupportActionBar().setDisplayShowCustomEnabled(true);
129128
}
130129
}
131130
requestAuthToken();

app/src/main/java/fr/free/nrw/commons/contributions/ContributionsListFragment.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import android.support.v4.app.Fragment;
1212
import android.support.v4.content.ContextCompat;
1313
import android.support.v4.content.IntentCompat;
14+
import android.support.v7.app.ActionBar;
1415
import android.util.Log;
1516
import android.view.LayoutInflater;
1617
import android.view.Menu;
@@ -166,27 +167,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
166167
startActivity(nearbyIntent);
167168
return true;
168169
}
169-
case R.id.menu_theme_toggle:
170-
final SharedPreferences prefs = getActivity().getSharedPreferences("prefs", Context.MODE_PRIVATE);
171-
prefs.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() {
172-
@Override
173-
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
174-
if(s.equals("theme")){
175-
//http://stackoverflow.com/questions/5659742/onsharedpreferencechanged-called-multiple-times-why
176-
prefs.unregisterOnSharedPreferenceChangeListener(this);
177-
//Finish current activity and tart new one with selected theme
178-
Intent intent = getActivity().getIntent();
179-
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
180-
getActivity().finish();
181-
startActivity(intent);
182-
}
183-
}
184-
});
185-
SharedPreferences.Editor editor = prefs.edit();
186-
//put inverse of selected boolean
187-
editor.putBoolean("theme", !prefs.getBoolean("theme", false));
188-
editor.commit();
189-
return true;
190170
case R.id.menu_refresh:
191171
((SourceRefresher)getActivity()).refreshSource();
192172
return true;
@@ -229,7 +209,6 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
229209
}
230210

231211
menu.findItem(R.id.menu_refresh).setVisible(false);
232-
menu.findItem(R.id.menu_theme_toggle).setActionView(R.layout.theme_toggle_layout);
233212
}
234213

235214
/*http://stackoverflow.com/questions/30076392/how-does-this-strange-condition-happens-when-show-menu-item-icon-in-toolbar-over/30337653#30337653

app/src/main/java/fr/free/nrw/commons/nearby/NearbyActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import android.view.MenuInflater;
1313
import android.view.MenuItem;
1414

15-
import fr.free.nrw.commons.BaseAppCompatActivity;
15+
import fr.free.nrw.commons.theme.BaseAppCompatActivity;
1616
import fr.free.nrw.commons.R;
1717

1818
public class NearbyActivity extends BaseAppCompatActivity {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package fr.free.nrw.commons.theme;
2+
3+
import android.app.Activity;
4+
import android.content.Intent;
5+
import android.os.Bundle;
6+
import android.preference.PreferenceManager;
7+
import android.util.Log;
8+
9+
import fr.free.nrw.commons.R;
10+
11+
12+
public class BaseActivity extends Activity {
13+
boolean currentTheme;
14+
15+
@Override
16+
protected void onCreate(Bundle savedInstanceState) {
17+
if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("theme",false)) {
18+
currentTheme = true;
19+
setTheme(R.style.DarkAppTheme);
20+
}else {
21+
currentTheme = false;
22+
setTheme(R.style.LightAppTheme); //default
23+
}
24+
super.onCreate(savedInstanceState);
25+
}
26+
27+
@Override
28+
protected void onResume() {
29+
//Restart activity if theme is changed
30+
Log.d("deneme","onResume Base");
31+
boolean newTheme = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("theme",false);
32+
if(currentTheme!=newTheme){ //is activity theme changed
33+
Intent intent = getIntent();
34+
finish();
35+
startActivity(intent);
36+
}
37+
super.onResume();
38+
}
39+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package fr.free.nrw.commons.theme;
2+
3+
import android.content.Intent;
4+
import android.os.Bundle;
5+
import android.preference.PreferenceManager;
6+
import android.support.v7.app.AppCompatActivity;
7+
import android.util.Log;
8+
9+
import fr.free.nrw.commons.R;
10+
11+
public class BaseAppCompatActivity extends AppCompatActivity {
12+
boolean currentTheme;
13+
14+
@Override
15+
protected void onCreate(Bundle savedInstanceState) {
16+
if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("theme",false)) {
17+
currentTheme = true;g
18+
setTheme(R.style.DarkAppTheme);
19+
}else {
20+
currentTheme = false;
21+
setTheme(R.style.LightAppTheme); //default
22+
}
23+
super.onCreate(savedInstanceState);
24+
}
25+
26+
@Override
27+
protected void onResume() {
28+
//Restart activity if theme is changed
29+
Log.d("deneme","onResume Base");
30+
boolean newTheme = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("theme",false);
31+
if(currentTheme!=newTheme){ //is activity theme changed
32+
Intent intent = getIntent();
33+
finish();
34+
startActivity(intent);
35+
}
36+
super.onResume();
37+
}
38+
}

app/src/main/java/fr/free/nrw/commons/upload/SingleUploadFragment.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.content.Intent;
66
import android.content.SharedPreferences;
77
import android.graphics.Color;
8+
import android.graphics.PorterDuff;
89
import android.net.Uri;
910
import android.os.Bundle;
1011
import android.preference.ListPreference;
@@ -116,7 +117,7 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
116117

117118
//Set selected color to white because it should be readable on random images.
118119
TextView selectedText = (TextView) licenseSpinner.getChildAt(0);
119-
if (selectedText != null) {
120+
if (selectedText != null ) {
120121
selectedText.setTextColor(Color.WHITE);
121122
}
122123

app/src/main/res/drawable/toggle.png

-585 Bytes
Binary file not shown.
-641 Bytes
Binary file not shown.

app/src/main/res/drawable/toggle_selector.xml

Lines changed: 0 additions & 9 deletions
This file was deleted.

app/src/main/res/layout/fragment_single_upload.xml

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,12 @@
3333
android:imeOptions="flagNoExtractUi"
3434
/>
3535

36-
<LinearLayout
36+
<Spinner
37+
android:id="@+id/licenseSpinner"
3738
android:layout_width="match_parent"
3839
android:layout_height="wrap_content"
39-
android:theme="@style/LightAppTheme"
40-
>
41-
42-
<Spinner
43-
android:id="@+id/licenseSpinner"
44-
android:layout_width="match_parent"
45-
android:layout_height="wrap_content"
46-
android:theme="@style/SpinnerTheme"
47-
/>
48-
49-
</LinearLayout>
50-
40+
android:theme="?attr/spinnerTheme"
41+
/>
5142

5243
<Button
5344
android:text="@string/use_previous"

app/src/main/res/layout/theme_toggle_layout.xml

Lines changed: 0 additions & 18 deletions
This file was deleted.

app/src/main/res/menu/fragment_contributions_list.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@
2828
android:title="@string/menu_nearby"
2929
app:showAsAction="never"
3030
/>
31-
<item android:id="@+id/menu_theme_toggle"
32-
android:title="Night mode"
33-
android:icon="?attr/toggleButtonIcon"
34-
app:actionLayout="@layout/theme_toggle_layout"
35-
app:showAsAction="never"
36-
/>
3731
<item android:id="@+id/menu_refresh"
3832
android:title="@string/menu_refresh"
3933
app:showAsAction="never"

app/src/main/res/values/attrs.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<attr name="fragmentCategorisationBackground" format="reference"/>
88
<attr name="buttonBackground" format="reference"/>
99
<attr name="uploadOverlayBackground" format="reference"/>
10-
<attr name="toggleButtonIcon" format="reference"/>
10+
<attr name="spinnerTheme" format="reference"/>
1111
<attr name="iconSend" format="reference"/>
1212
<attr name="iconSave" format="reference"/>
1313
<attr name="iconCamera" format="reference"/>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Tap this message (or hit back) to skip this step.</string>
9393
<string name="allow_gps">Automatically get current location</string>
9494
<string name="allow_gps_summary">Retrieve current location to offer category suggestions if image is not geotagged</string>
9595
<string name="preference_theme">Select theme</string>
96-
<string name="preference_theme_summary">Select application theme as light/dark</string>
96+
<string name="preference_theme_summary">Select if you prefer night mode</string>
9797
<string name="license_name_cc_by_sa_four"> Attribution-ShareAlike 4.0</string>
9898
<string name="license_name_cc_by_four"> Attribution 4.0</string>
9999
<string name="license_name_cc_by_sa"> Attribution-ShareAlike 3.0</string>

0 commit comments

Comments
 (0)