From 943445807d9ecc4cf875acf45f5c606b4176a9b7 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 28 Jun 2020 12:14:14 +0200 Subject: [PATCH] Search --- app/src/main/AndroidManifest.xml | 6 +- .../app/fedilab/fedilabtube/MainActivity.java | 24 +++++++ .../fedilab/fedilabtube/SearchActivity.java | 65 +++++++++++++++++++ .../RetrievePeertubeSearchAsyncTask.java | 8 +-- .../fedilabtube/client/PeertubeAPI.java | 4 +- .../res/layout/activity_search_result.xml | 6 ++ app/src/main/res/menu/main_menu.xml | 9 +++ app/src/main/res/values/strings.xml | 3 + 8 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/app/fedilab/fedilabtube/SearchActivity.java create mode 100644 app/src/main/res/layout/activity_search_result.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3e73c03..c141e61 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -53,7 +53,11 @@ android:label="@string/app_name" android:theme="@style/AppThemeNoActionBar" android:windowSoftInputMode="stateAlwaysHidden" /> - + diff --git a/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java b/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java index 399b28a..8b2d7ab 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java @@ -1,6 +1,7 @@ package app.fedilab.fedilabtube; import android.content.Context; +import android.content.Intent; import android.content.SharedPreferences; import android.os.AsyncTask; import android.os.Bundle; @@ -9,6 +10,7 @@ import android.view.MenuItem; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.SearchView; import androidx.navigation.NavController; import androidx.navigation.Navigation; import androidx.navigation.ui.AppBarConfiguration; @@ -49,6 +51,27 @@ public class MainActivity extends AppCompatActivity { @Override public boolean onCreateOptionsMenu(@NotNull Menu menu) { getMenuInflater().inflate(R.menu.main_menu, menu); + MenuItem myActionMenuItem = menu.findItem( R.id.action_search); + SearchView searchView = (SearchView) myActionMenuItem.getActionView(); + searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { + @Override + public boolean onQueryTextSubmit(String query) { + Intent intent = new Intent(MainActivity.this, SearchActivity.class); + Bundle b = new Bundle(); + b.putString("search", query.trim()); + intent.putExtras(b); + startActivity(intent); + if( ! searchView.isIconified()) { + searchView.setIconified(true); + } + myActionMenuItem.collapseActionView(); + return false; + } + @Override + public boolean onQueryTextChange(String s) { + return false; + } + }); return true; } @@ -58,6 +81,7 @@ public class MainActivity extends AppCompatActivity { showRadioButtonDialog(); return true; } + return super.onOptionsItemSelected(item); } diff --git a/app/src/main/java/app/fedilab/fedilabtube/SearchActivity.java b/app/src/main/java/app/fedilab/fedilabtube/SearchActivity.java new file mode 100644 index 0000000..c1d5977 --- /dev/null +++ b/app/src/main/java/app/fedilab/fedilabtube/SearchActivity.java @@ -0,0 +1,65 @@ + +package app.fedilab.fedilabtube; + +import android.os.Bundle; +import android.view.MenuItem; +import android.widget.Toast; + +import androidx.appcompat.app.AppCompatActivity; +import androidx.fragment.app.FragmentTransaction; + +import app.fedilab.fedilabtube.client.APIResponse; +import app.fedilab.fedilabtube.fragment.DisplayStatusFragment; +import app.fedilab.fedilabtube.interfaces.OnRetrieveFeedsInterface; +import es.dmoral.toasty.Toasty; + + + +public class SearchActivity extends AppCompatActivity implements OnRetrieveFeedsInterface { + + + private String search; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.activity_search_result); + Bundle b = getIntent().getExtras(); + if (b != null) { + search = b.getString("search"); + } else { + Toasty.error(SearchActivity.this, getString(R.string.toast_error_search), Toast.LENGTH_LONG).show(); + } + + if (getSupportActionBar() != null) + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + setTitle(search); + + + if (savedInstanceState == null) { + DisplayStatusFragment displayStatusFragment = new DisplayStatusFragment(); + Bundle bundle = new Bundle(); + bundle.putString("search_peertube", search); + displayStatusFragment.setArguments(bundle); + FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); + ft.add(R.id.container, displayStatusFragment).commit(); + } + } + + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == android.R.id.home) { + finish(); + return true; + } + return super.onOptionsItemSelected(item); + } + + + @Override + public void onRetrieveFeeds(APIResponse apiResponse) { + + } +} diff --git a/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrievePeertubeSearchAsyncTask.java b/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrievePeertubeSearchAsyncTask.java index 97190ac..32fc73a 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrievePeertubeSearchAsyncTask.java +++ b/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrievePeertubeSearchAsyncTask.java @@ -12,23 +12,23 @@ import app.fedilab.fedilabtube.interfaces.OnRetrieveFeedsInterface; public class RetrievePeertubeSearchAsyncTask extends AsyncTask { - private String query, instance; + private String query, max_id; private APIResponse apiResponse; private OnRetrieveFeedsInterface listener; private WeakReference contextReference; - public RetrievePeertubeSearchAsyncTask(Context context, String instance, String query, OnRetrieveFeedsInterface onRetrieveFeedsInterface) { + public RetrievePeertubeSearchAsyncTask(Context context, String max_id, String query, OnRetrieveFeedsInterface onRetrieveFeedsInterface) { this.contextReference = new WeakReference<>(context); this.query = query; this.listener = onRetrieveFeedsInterface; - this.instance = instance; + this.max_id = max_id; } @Override protected Void doInBackground(Void... params) { PeertubeAPI api = new PeertubeAPI(this.contextReference.get()); - apiResponse = api.searchPeertube(instance, query); + apiResponse = api.searchPeertube(query, max_id); return null; } diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java index 17aa534..6878213 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java @@ -1747,9 +1747,11 @@ public class PeertubeAPI { * @param query String search * @return APIResponse */ - public APIResponse searchPeertube(String instance, String query) { + public APIResponse searchPeertube(String query, String max_id) { HashMap params = new HashMap<>(); params.put("count", "50"); + if (max_id != null) + params.put("start", max_id); if (query == null) return null; try { diff --git a/app/src/main/res/layout/activity_search_result.xml b/app/src/main/res/layout/activity_search_result.xml new file mode 100644 index 0000000..ce1002b --- /dev/null +++ b/app/src/main/res/layout/activity_search_result.xml @@ -0,0 +1,6 @@ + + + \ No newline at end of file diff --git a/app/src/main/res/menu/main_menu.xml b/app/src/main/res/menu/main_menu.xml index 9a67115..8c52da6 100644 --- a/app/src/main/res/menu/main_menu.xml +++ b/app/src/main/res/menu/main_menu.xml @@ -1,6 +1,15 @@ + + + + Voulez-vous vous désabonner de ce compte ? Se désabonner Suivre + Chercher + Une erreur s’est produite lors de la recherche ! + Aucun résultat ! \ No newline at end of file