Add SharedPreferences to handle global behaviour. First use is "Hiding umbrella ImageView when new city is added into the DB"
This commit is contained in:
parent
9afb1cb8dd
commit
eb61b3fdb7
6
.idea/render.experimental.xml
Normal file
6
.idea/render.experimental.xml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="RenderSettings">
|
||||||
|
<option name="showDecorations" value="true" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
@ -2,6 +2,9 @@ package fr.romanet.vj.apps.myweather;
|
|||||||
|
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.appcompat.widget.Toolbar;
|
import androidx.appcompat.widget.Toolbar;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
@ -13,6 +16,10 @@ public class AddCity extends AppCompatActivity {
|
|||||||
|
|
||||||
private EditText cityName;
|
private EditText cityName;
|
||||||
|
|
||||||
|
// - Define shared preferences var -
|
||||||
|
SharedPreferences sharedPreferences;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState)
|
protected void onCreate(Bundle savedInstanceState)
|
||||||
{
|
{
|
||||||
@ -37,8 +44,21 @@ public class AddCity extends AppCompatActivity {
|
|||||||
cityName.setError("Please, enter a city name");
|
cityName.setError("Please, enter a city name");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
||||||
|
// - Load Shared Preferences (named 'prefs') -
|
||||||
|
sharedPreferences = getSharedPreferences("prefs", Context.MODE_PRIVATE);
|
||||||
|
|
||||||
|
// - Now the ImageView has to be hidden, so write it into the shared preferences -
|
||||||
|
// - Put value '0' into the preference 'umbrella_visible' of the shared preference book called 'prefs'
|
||||||
|
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||||
|
editor.putInt("umbrella_visible", 0);
|
||||||
|
editor.apply();
|
||||||
|
|
||||||
saveCity(newCityString);
|
saveCity(newCityString);
|
||||||
resetForm();
|
resetForm();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,11 +3,24 @@ package fr.romanet.vj.apps.myweather;
|
|||||||
import android.content.*;
|
import android.content.*;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.widget.ImageView;
|
||||||
|
|
||||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
|
// - Umbrella ImageView
|
||||||
|
public static ImageView imgPara;
|
||||||
|
|
||||||
|
// - Is used to store the shared preference value of umbrella visibility
|
||||||
|
public int umbrella_visible;
|
||||||
|
|
||||||
|
// - Define shared preferences var -
|
||||||
|
SharedPreferences sharedPreferences;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState)
|
protected void onCreate(Bundle savedInstanceState)
|
||||||
{
|
{
|
||||||
@ -21,6 +34,46 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
openAddCityPage();
|
openAddCityPage();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// - Assign Activity ImageView to var -
|
||||||
|
imgPara = (ImageView) findViewById(R.id.imageView);
|
||||||
|
|
||||||
|
// - Load Shared Preferences (named 'prefs') -
|
||||||
|
sharedPreferences = getSharedPreferences("prefs", Context.MODE_PRIVATE);
|
||||||
|
|
||||||
|
// - Try to retrieve 'umbrella_visible', if not set return 3 -
|
||||||
|
umbrella_visible = sharedPreferences.getInt("umbrella_visible", 3);
|
||||||
|
|
||||||
|
// - If preference not yet defined, set value to 1 (visible) -
|
||||||
|
if (umbrella_visible == 3){
|
||||||
|
|
||||||
|
// - Put value '1' into the preference 'umbrella_visible' of the shared preference book called 'prefs'
|
||||||
|
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||||
|
editor.putInt("umbrella_visible", 1);
|
||||||
|
editor.apply();
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.d("DEBUG1 -> create", Integer.toString(umbrella_visible));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
|
||||||
|
// - Load Shared Preferences (named 'prefs') -
|
||||||
|
sharedPreferences = getSharedPreferences("prefs", Context.MODE_PRIVATE);
|
||||||
|
|
||||||
|
// - Try to retrieve 'umbrella_visible', if not set return 3 -
|
||||||
|
umbrella_visible = sharedPreferences.getInt("umbrella_visible", 3);
|
||||||
|
Log.d("DEBUG1 -> resume", Integer.toString(umbrella_visible));
|
||||||
|
|
||||||
|
// - If 'umbrella_visible' is equal to '0', we have to hide the ImageView -
|
||||||
|
if(umbrella_visible == 0){
|
||||||
|
|
||||||
|
// - Hide ImageView -
|
||||||
|
imgPara.setVisibility(View.GONE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void openAddCityPage()
|
public void openAddCityPage()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user