Skip to content

Commit

Permalink
try to solve a google pre-check-bug ClassCastException
Browse files Browse the repository at this point in the history
  • Loading branch information
amarradi committed Dec 10, 2024
1 parent e577f0e commit 6a4e031
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 42 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ android {
applicationId "com.git.amarradi.leafpad"
minSdkVersion 25
targetSdkVersion 34
versionCode 20
versionCode 21
versionName "1.16"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
Expand Down
23 changes: 6 additions & 17 deletions app/src/main/java/com/git/amarradi/leafpad/Leaf.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,19 @@ public class Leaf {
private final static boolean HIDE = false;

public static ArrayList<Note> loadAll(Context context, boolean includeHidden) {

SharedPreferences sharedPreferences = context.getSharedPreferences(STORE_PREF, Context.MODE_PRIVATE);
ArrayList<Note> notes = new ArrayList<>();
Set<String> noteIds = sharedPreferences.getStringSet(ID_KEY, null);
Log.d("LoadAll", "Note IDs loaded: " + noteIds);



/** if (noteIds != null) {
if (noteIds != null) {
for (String noteId : noteIds) {
notes.add(load(context, noteId));
}
}**/

for (String noteId : noteIds) {
Note note = load(context, noteId);
if (!note.isHide() || includeHidden) { // Parameter `includeHidden` hinzufügen
notes.add(note);
Note note = load(context, noteId);
if (!note.isHide() || includeHidden) {
notes.add(note);
}
}
}


DateTimeFormatter d;
DateTimeFormatter t;

Expand All @@ -73,7 +65,6 @@ public static Note load(Context context, String noteId) {
SharedPreferences sharedPreferences = context.getSharedPreferences(STORE_PREF, Context.MODE_PRIVATE);
boolean noteHide;
if (sharedPreferences.contains(HIDE + noteId)) {
// Migrate old data if found
noteHide = sharedPreferences.getBoolean(HIDE + noteId, false);
sharedPreferences.edit().remove(HIDE + noteId).putBoolean(HIDE + "_" + noteId, noteHide).apply();
} else {
Expand Down Expand Up @@ -126,15 +117,13 @@ public static void set(Context context, Note note) {
} else {
editor.putBoolean(HIDE + "_" + note.getId(), note.isHide());
}

editor.apply();
}

@SuppressLint("MutatingSharedPrefs")
public static void remove(Context context, Note note) {
SharedPreferences sharedPreferences = context.getSharedPreferences(STORE_PREF, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();

Set<String> ids = sharedPreferences.getStringSet(ID_KEY, null);

if (ids == null) {
Expand Down
16 changes: 2 additions & 14 deletions app/src/main/java/com/git/amarradi/leafpad/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
Expand All @@ -29,7 +28,6 @@

public class MainActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {


public final static String EXTRA_NOTE_ID = "com.git.amarradi.leafpad";
public static final String SHARED_PREFS = "sharedPrefs";
public static final String DESIGN_MODE = "system";
Expand Down Expand Up @@ -61,8 +59,6 @@ protected void onCreate(Bundle savedInstanceState) {

Objects.requireNonNull(getSupportActionBar()).setDefaultDisplayHomeAsUpEnabled(true);

// SplashScreen splashScreen = SplashScreen.installSplashScreen(this);

adapter = new SimpleAdapter(this, data,
R.layout.note_list_item,
new String[]{"title", "date", "time", "state"},
Expand Down Expand Up @@ -104,7 +100,6 @@ private void loadThemeFromPreference(SharedPreferences sharedPreferences) {
}

private void changeTheme(String themeValue) {
Log.d("theme", "changeTheme: " + themeValue);
AppCompatDelegate.setDefaultNightMode(toNightMode(themeValue));
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Expand All @@ -121,8 +116,6 @@ private int toNightMode(String themeValue) {
}
return AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
}


@Override
protected void onPause() {
super.onPause();
Expand All @@ -134,13 +127,13 @@ protected void onResume() {
updateListView();
}

@SuppressLint("UseCompatLoadingForDrawables")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.item_show_hidden);
if (showHidden) {

item.setIcon(getDrawable(R.drawable.action_eye_closed));
item.setTitle(getString(R.string.hide_hidden));
} else {
Expand All @@ -151,10 +144,9 @@ public boolean onCreateOptionsMenu(Menu menu) {
return true;
}

@SuppressLint({"NonConstantResourceId", "UseCompatLoadingForDrawables"})
@SuppressLint({"NonConstantResourceId"})
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {

switch (item.getItemId()) {
case R.id.item_settings:
Intent settingsIntent = new Intent(this, SettingsActivity.class);
Expand All @@ -171,11 +163,9 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
private void toggleShowHidden(MenuItem item) {
showHidden = !showHidden;
if (!showHidden) {
Log.d("MenuAction", "Setting icon to action_eye_closed");
item.setIcon(getDrawable(R.drawable.action_eye_closed));
item.setTitle(getString(R.string.hide_hidden));
} else {
Log.d("MenuAction", "Setting icon to action_eye_open");
item.setIcon(getDrawable(R.drawable.action_eye_open));
item.setTitle(getString(R.string.show_hidden));
}
Expand All @@ -195,15 +185,13 @@ public void updateDataset() {
data.clear();
for (Note note : notes) {
if (showHidden && note.isHide()) {
Log.d("Note", "Title: " + note.getTitle() + ", Hidden: " + note.isHide());
Map<String, String> datum = new HashMap<>();
datum.put("title", note.getTitle());
datum.put("body", note.getBody());
datum.put("date", note.getDate());
datum.put("time", note.getTime());
data.add(datum);
} else if(!showHidden && !note.isHide()){
Log.d("Note", "Title: " + note.getTitle() + ", Hidden: " + note.isHide());
Map<String, String> datum = new HashMap<>();
datum.put("title", note.getTitle());
datum.put("body", note.getBody());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ protected void onCreate(Bundle savedInstanceState) {
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);


Intent intent = getIntent();
String noteId = intent.getStringExtra(MainActivity.EXTRA_NOTE_ID);
if (Objects.equals(getIntent().getAction(), "android.intent.action.VIEW")) {
Expand All @@ -52,14 +51,10 @@ protected void onCreate(Bundle savedInstanceState) {
bodyEdit = findViewById(R.id.body_edit);
visibleSwitch = findViewById(R.id.visible_switch);




note = Leaf.load(this, noteId);

toggleView();


if (isNewEntry(note, intent)) {
note = Leaf.load(this, Note.makeId());
note.setHide(false);
Expand Down Expand Up @@ -149,9 +144,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
@SuppressLint("NonConstantResourceId")
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();
switch (id) {
case R.id.action_share_note:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ private void setPreferenceSummary(Preference preference, String value) {
}
}


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -114,7 +113,6 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, @Null
}

public static void launchAppStore(Activity activity, String packageName) {

Intent intent;
try {
intent = new Intent(Intent.ACTION_VIEW);
Expand Down

0 comments on commit 6a4e031

Please sign in to comment.