Add history for search

Gets deleted with all events from settings
This commit is contained in:
akaessens 2021-08-10 17:17:51 +02:00
parent 43458af11c
commit 2ef6a35862
4 changed files with 69 additions and 7 deletions

View File

@ -12,6 +12,8 @@ import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.menu.MenuBuilder;
@ -23,13 +25,13 @@ import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.ref.WeakReference;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@ -40,7 +42,7 @@ import static com.akdev.nofbeventscraper.FbEvent.createEventList;
public class MainActivity extends AppCompatActivity {
protected ExtendedFloatingActionButton paste_button;
protected TextInputEditText edit_text_uri_input;
protected AutoCompleteTextView edit_text_uri_input;
protected TextInputLayout layout_uri_input;
@ -49,6 +51,28 @@ public class MainActivity extends AppCompatActivity {
EventAdapter adapter;
LinearLayoutManager linear_layout_manager;
List<String> history;
ArrayAdapter<String> history_adapter;
private List<String> getHistory() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Gson gson = new Gson();
String json = prefs.getString("history", "");
Type history_type = new TypeToken<List<String>>() {
}.getType();
List<String> list = gson.fromJson(json, history_type);
if (list == null) {
list = new ArrayList<>();
}
return list;
}
private List<FbEvent> getSavedEvents() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
@ -81,6 +105,12 @@ public class MainActivity extends AppCompatActivity {
adapter.notifyDataSetChanged();
}
if (getHistory().isEmpty()) {
history.clear();
history_adapter.clear();
adapter.notifyDataSetChanged();
}
/*
* Intent from IntentReceiver - read only once
*/
@ -106,6 +136,9 @@ public class MainActivity extends AppCompatActivity {
Gson gson = new Gson();
String json = gson.toJson(events);
prefs_edit.putString("events", json);
json = gson.toJson(history);
prefs_edit.putString("history", json);
prefs_edit.apply();
}
@ -132,6 +165,10 @@ public class MainActivity extends AppCompatActivity {
linear_layout_manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recycler_view.setLayoutManager(linear_layout_manager);
// restore history
this.history = getHistory();
history_adapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, history);
recycler_view.setItemAnimator(new FadeInAnimator());
@ -197,6 +234,14 @@ public class MainActivity extends AppCompatActivity {
};
layout_uri_input.setErrorIconOnClickListener(listener);
layout_uri_input.setEndIconOnClickListener(listener);
edit_text_uri_input.setAdapter(history_adapter);
layout_uri_input.setStartIconOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
edit_text_uri_input.showDropDown();
}
});
/*
@ -235,6 +280,9 @@ public class MainActivity extends AppCompatActivity {
scraper = new FbScraper(new WeakReference<>(this), url);
scraper.run();
history_adapter.insert(url, 0);
history.add(0, url);
}
/**

View File

@ -43,16 +43,20 @@ public class SettingsActivity extends AppCompatActivity {
final SharedPreferences prefs = preference.getSharedPreferences();
final String undo = prefs.getString("events", "");
final String events = prefs.getString("events", "");
prefs.edit().remove("events").apply();
final String history = prefs.getString("history", "");
prefs.edit().remove("history").apply();
Snackbar.make(getActivity().findViewById(android.R.id.content),
getString(R.string.preferences_event_snackbar), Snackbar.LENGTH_SHORT)
.setAction(R.string.undo, new View.OnClickListener() {
@Override
public void onClick(View v) {
prefs.edit().putString("events", undo).apply();
prefs.edit().putString("events", events).apply();
prefs.edit().putString("history", history).apply();
}
}).show();

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M13,3c-4.97,0 -9,4.03 -9,9L1,12l3.89,3.89 0.07,0.14L9,12L6,12c0,-3.87 3.13,-7 7,-7s7,3.13 7,7 -3.13,7 -7,7c-1.93,0 -3.68,-0.79 -4.94,-2.06l-1.42,1.42C8.27,19.99 10.51,21 13,21c4.97,0 9,-4.03 9,-9s-4.03,-9 -9,-9zM12,8v5l4.25,2.52 0.77,-1.28 -3.52,-2.09L13.5,8z"
android:fillColor="#000000"/>
</vector>

View File

@ -17,7 +17,7 @@
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/layout_uri_input"
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
@ -26,9 +26,10 @@
app:endIconMode="clear_text"
app:errorIconDrawable="@drawable/ic_backspace_black"
app:helperText="@string/helper_add_link"
app:helperTextEnabled="true">
app:helperTextEnabled="true"
app:startIconDrawable="@drawable/ic_history_black">
<com.google.android.material.textfield.TextInputEditText
<com.google.android.material.textfield.MaterialAutoCompleteTextView
android:id="@+id/edit_text_uri_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"