From 914c247463506394d6e66103054ec393d0ae32d0 Mon Sep 17 00:00:00 2001 From: Stefan Schueller Date: Sun, 2 Dec 2018 14:27:00 +0100 Subject: [PATCH 1/4] - loing WIP --- app/src/main/AndroidManifest.xml | 3 +- .../peertube/activity/SearchActivity.java | 33 ++++++++++++++++--- .../peertube/activity/VideoListActivity.java | 3 +- app/src/main/res/layout/activity_search.xml | 19 ++++++++++- app/src/main/res/values/strings.xml | 1 + 5 files changed, 51 insertions(+), 8 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 80cd260..5ef9e6b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -30,7 +30,8 @@ + android:launchMode="singleTop" + android:parentActivityName=".activity.VideoListActivity"> diff --git a/app/src/main/java/net/schueller/peertube/activity/SearchActivity.java b/app/src/main/java/net/schueller/peertube/activity/SearchActivity.java index 976d00f..2456599 100644 --- a/app/src/main/java/net/schueller/peertube/activity/SearchActivity.java +++ b/app/src/main/java/net/schueller/peertube/activity/SearchActivity.java @@ -11,7 +11,10 @@ import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.Toolbar; import android.util.Log; +import android.view.View; +import android.widget.TextView; import android.widget.Toast; @@ -43,6 +46,9 @@ public class SearchActivity extends AppCompatActivity { private boolean isLoading = false; + private TextView emptyView; + private RecyclerView recyclerView; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -53,6 +59,11 @@ public class SearchActivity extends AppCompatActivity { // do search handleIntent(intent); + // toolbar + Toolbar toolbar = findViewById(R.id.tool_bar); + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + // handle search suggestions if (Intent.ACTION_SEARCH.equals(intent.getAction())) { String query = intent.getStringExtra(SearchManager.QUERY); @@ -66,9 +77,11 @@ public class SearchActivity extends AppCompatActivity { } private void createList(String query) { - RecyclerView recyclerView = findViewById(R.id.recyclerView); + recyclerView = findViewById(R.id.recyclerView); swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout); + emptyView = findViewById(R.id.empty_view); + RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(SearchActivity.this); recyclerView.setLayoutManager(layoutManager); @@ -123,10 +136,6 @@ public class SearchActivity extends AppCompatActivity { Call call = service.searchVideosData(start, count, sort, nsfw, search); - /*Log the URL called*/ - Log.d("URL Called", call.request().url() + ""); -// Toast.makeText(VideoListActivity.this, "URL Called: " + call.request().url(), Toast.LENGTH_SHORT).show(); - call.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { @@ -138,6 +147,20 @@ public class SearchActivity extends AppCompatActivity { if (response.body() != null) { videoAdapter.setData(response.body().getVideoArrayList()); } + + Log.d("SearchActivity", "getItemCount: " + videoAdapter.getItemCount()); + Log.d("SearchActivity", "response: " + response.body()); + + // no results show no results message + if (response.body() == null && videoAdapter.getItemCount() == 0) { + emptyView.setVisibility(View.VISIBLE); + recyclerView.setVisibility(View.GONE); + + } else { + emptyView.setVisibility(View.GONE); + recyclerView.setVisibility(View.VISIBLE); + } + isLoading = false; swipeRefreshLayout.setRefreshing(false); } diff --git a/app/src/main/java/net/schueller/peertube/activity/VideoListActivity.java b/app/src/main/java/net/schueller/peertube/activity/VideoListActivity.java index 31a09c1..8135f6e 100644 --- a/app/src/main/java/net/schueller/peertube/activity/VideoListActivity.java +++ b/app/src/main/java/net/schueller/peertube/activity/VideoListActivity.java @@ -7,6 +7,7 @@ import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.preference.PreferenceManager; import android.support.annotation.NonNull; +import android.support.design.bottomnavigation.LabelVisibilityMode; import android.support.v4.app.ActivityCompat; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; @@ -124,7 +125,7 @@ public class VideoListActivity extends AppCompatActivity { BottomNavigationViewEx navigation = findViewById(R.id.navigation); navigation.enableAnimation(false); - navigation.setLabelVisibilityMode(1); // enableShiftingMode + navigation.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED); // enableShiftingMode navigation.setItemHorizontalTranslationEnabled(false); // enableItemShiftingMode Menu navMenu = navigation.getMenu(); diff --git a/app/src/main/res/layout/activity_search.xml b/app/src/main/res/layout/activity_search.xml index 66a4a72..19a336b 100644 --- a/app/src/main/res/layout/activity_search.xml +++ b/app/src/main/res/layout/activity_search.xml @@ -13,6 +13,15 @@ android:orientation="vertical" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> + + - + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 92814a4..c4ee486 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -55,5 +55,6 @@ Version Search PeerTube Search + No Results From 57c3079c6837fdb78391963999b6e39cc959ef8b Mon Sep 17 00:00:00 2001 From: Stefan Schueller Date: Sun, 2 Dec 2018 14:59:14 +0100 Subject: [PATCH 2/4] - Prevent fatal when server URL is invalid --- .../java/net/schueller/peertube/helper/APIUrlHelper.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/net/schueller/peertube/helper/APIUrlHelper.java b/app/src/main/java/net/schueller/peertube/helper/APIUrlHelper.java index 6e60f4f..cfed405 100644 --- a/app/src/main/java/net/schueller/peertube/helper/APIUrlHelper.java +++ b/app/src/main/java/net/schueller/peertube/helper/APIUrlHelper.java @@ -3,6 +3,7 @@ package net.schueller.peertube.helper; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; +import android.webkit.URLUtil; import net.schueller.peertube.R; @@ -10,7 +11,13 @@ public class APIUrlHelper{ public static String getUrl(Context context) { SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); - return sharedPref.getString("pref_api_base", context.getResources().getString(R.string.pref_default_api_base_url)); + + // validate URL is valid + String URL = sharedPref.getString("pref_api_base", context.getResources().getString(R.string.pref_default_api_base_url)); + if (!URLUtil.isValidUrl(URL)) { + return "http://invalid"; + } + return URL; } public static String getUrlWithVersion(Context context) { From 43b10c680dbf95bbfc299ab5fc5a8654860ca0de Mon Sep 17 00:00:00 2001 From: Stefan Schueller Date: Sun, 2 Dec 2018 17:28:18 +0100 Subject: [PATCH 3/4] - Better search --- app/src/main/AndroidManifest.xml | 21 +- .../peertube/activity/SearchActivity.java | 200 ------------------ .../peertube/activity/VideoListActivity.java | 111 +++++++++- app/src/main/res/layout/activity_search.xml | 55 ----- .../main/res/layout/activity_video_list.xml | 11 + app/src/main/res/menu/menu_main.xml | 4 +- 6 files changed, 120 insertions(+), 282 deletions(-) delete mode 100644 app/src/main/java/net/schueller/peertube/activity/SearchActivity.java delete mode 100644 app/src/main/res/layout/activity_search.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 5ef9e6b..44be243 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -16,29 +16,18 @@ android:supportsRtl="true" android:theme="@style/AppTheme" tools:ignore="GoogleAppIndexingWarning"> - - + - + - - - - - - + android:resource="@xml/searchable"> + (), SearchActivity.this); - recyclerView.setAdapter(videoAdapter); - - loadVideos(currentStart, count, sort, query, filter); - - recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { - @Override - public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { - super.onScrollStateChanged(recyclerView, newState); - } - - @Override - public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { - - if (dy > 0) { - // is at end of list? - if(!recyclerView.canScrollVertically(RecyclerView.FOCUS_DOWN)){ - if (!isLoading) { - currentStart = currentStart + count; - loadVideos(currentStart, count, sort, query, filter); - } - } - } - - } - }); - - swipeRefreshLayout.setOnRefreshListener(() -> { - // Refresh items - if (!isLoading) { - currentStart = 0; - loadVideos(currentStart, count, sort, query, filter); - } - }); - - } - - - private void loadVideos(int start, int count, String sort, String search, String filter) { - - isLoading = true; - - SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); - String nsfw = sharedPref.getBoolean("pref_show_nsfw", false) ? "both" : "false"; - - String apiBaseURL = APIUrlHelper.getUrlWithVersion(this); - - GetVideoDataService service = RetrofitInstance.getRetrofitInstance(apiBaseURL).create(GetVideoDataService.class); - - Call call = service.searchVideosData(start, count, sort, nsfw, search); - - call.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { - - if (currentStart == 0) { - videoAdapter.clearData(); - } - - if (response.body() != null) { - videoAdapter.setData(response.body().getVideoArrayList()); - } - - Log.d("SearchActivity", "getItemCount: " + videoAdapter.getItemCount()); - Log.d("SearchActivity", "response: " + response.body()); - - // no results show no results message - if (response.body() == null && videoAdapter.getItemCount() == 0) { - emptyView.setVisibility(View.VISIBLE); - recyclerView.setVisibility(View.GONE); - - } else { - emptyView.setVisibility(View.GONE); - recyclerView.setVisibility(View.VISIBLE); - } - - isLoading = false; - swipeRefreshLayout.setRefreshing(false); - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - Log.wtf("err", t.fillInStackTrace()); - Toast.makeText(SearchActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show(); - isLoading = false; - swipeRefreshLayout.setRefreshing(false); - } - }); - } - - - - @Override - protected void onNewIntent(Intent intent) { - setIntent(intent); - handleIntent(intent); - } - - private void handleIntent(Intent intent) { - if (Intent.ACTION_SEARCH.equals(intent.getAction())) { - String query = intent.getStringExtra(SearchManager.QUERY); - Log.v("Search Activity", query); - createList(query); - } - } - - @Override - public boolean onSearchRequested() { - Bundle appData = new Bundle(); - startSearch(null, false, appData, false); - return true; - } -} diff --git a/app/src/main/java/net/schueller/peertube/activity/VideoListActivity.java b/app/src/main/java/net/schueller/peertube/activity/VideoListActivity.java index e517840..5ef84c4 100644 --- a/app/src/main/java/net/schueller/peertube/activity/VideoListActivity.java +++ b/app/src/main/java/net/schueller/peertube/activity/VideoListActivity.java @@ -1,11 +1,13 @@ package net.schueller.peertube.activity; import android.Manifest; -import android.app.Activity; +import android.app.SearchManager; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.preference.PreferenceManager; +import android.provider.SearchRecentSuggestions; import android.support.annotation.NonNull; import android.support.design.bottomnavigation.LabelVisibilityMode; import android.support.v4.app.ActivityCompat; @@ -15,11 +17,14 @@ import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.SearchView; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; +import android.view.View; +import android.widget.TextView; import android.widget.Toast; //import com.google.android.gms.common.GooglePlayServicesNotAvailableException; @@ -38,6 +43,7 @@ import net.schueller.peertube.helper.APIUrlHelper; import net.schueller.peertube.model.VideoList; import net.schueller.peertube.network.GetVideoDataService; import net.schueller.peertube.network.RetrofitInstance; +import net.schueller.peertube.provider.SearchSuggestionsProvider; import java.util.ArrayList; @@ -59,6 +65,10 @@ public class VideoListActivity extends AppCompatActivity { private int count = 12; private String sort = "-createdAt"; private String filter = ""; + private String searchQuery = ""; + + private TextView emptyView; + private RecyclerView recyclerView; private boolean isLoading = false; @@ -152,16 +162,49 @@ public class VideoListActivity extends AppCompatActivity { inflater.inflate(R.menu.menu_main, menu); // Set an icon in the ActionBar - menu.findItem(R.id.action_search).setIcon( - new IconDrawable(this, FontAwesomeIcons.fa_search) - .colorRes(R.color.cardview_light_background) - .actionBarSize()); - menu.findItem(R.id.action_settings).setIcon( new IconDrawable(this, FontAwesomeIcons.fa_cog) .colorRes(R.color.cardview_light_background) .actionBarSize()); + + MenuItem searchMenuItem = menu.findItem(R.id.action_search); + searchMenuItem.setIcon( + new IconDrawable(this, FontAwesomeIcons.fa_search) + .colorRes(R.color.cardview_light_background) + .actionBarSize()); + + // Get the SearchView and set the searchable configuration + SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); + SearchView searchView = (SearchView) searchMenuItem.getActionView(); + + // Assumes current activity is the searchable activity + searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); + searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default + searchView.setQueryRefinementEnabled(true); + + searchMenuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() { + @Override + public boolean onMenuItemActionExpand(MenuItem menuItem) { + return true; + } + + @Override + public boolean onMenuItemActionCollapse(MenuItem menuItem) { + searchQuery = ""; + Log.d(TAG, "onMenuItemActionCollapse: "); + loadVideos(0, count, sort, filter); + return true; + } + }); + + // TODO, this doesn't work + searchManager.setOnDismissListener(() -> { + searchQuery = ""; + Log.d(TAG, "onDismiss: "); + loadVideos(0, count, sort, filter); + }); + return true; } @@ -176,7 +219,6 @@ public class VideoListActivity extends AppCompatActivity { // action with ID action_refresh was selected case R.id.action_search: //Toast.makeText(this, "Search Selected", Toast.LENGTH_SHORT).show(); - onSearchRequested(); return false; case R.id.action_settings: @@ -193,9 +235,11 @@ public class VideoListActivity extends AppCompatActivity { } private void createList() { - RecyclerView recyclerView = findViewById(R.id.recyclerView); + recyclerView = findViewById(R.id.recyclerView); swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout); + emptyView = findViewById(R.id.empty_view); + RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(VideoListActivity.this); recyclerView.setLayoutManager(layoutManager); @@ -247,7 +291,12 @@ public class VideoListActivity extends AppCompatActivity { GetVideoDataService service = RetrofitInstance.getRetrofitInstance(apiBaseURL).create(GetVideoDataService.class); - Call call = service.getVideosData(start, count, sort, nsfw); + Call call; + if (!searchQuery.equals("")) { + call = service.searchVideosData(start, count, sort, nsfw, searchQuery); + } else { + call = service.getVideosData(start, count, sort, nsfw); + } /*Log the URL called*/ Log.d("URL Called", call.request().url() + ""); @@ -264,6 +313,17 @@ public class VideoListActivity extends AppCompatActivity { if (response.body() != null) { videoAdapter.setData(response.body().getVideoArrayList()); } + + // no results show no results message + if (currentStart == 0 && videoAdapter.getItemCount() == 0) { + emptyView.setVisibility(View.VISIBLE); + recyclerView.setVisibility(View.GONE); + + } else { + emptyView.setVisibility(View.GONE); + recyclerView.setVisibility(View.VISIBLE); + } + isLoading = false; swipeRefreshLayout.setRefreshing(false); } @@ -304,4 +364,37 @@ public class VideoListActivity extends AppCompatActivity { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0); } } + + @Override + protected void onNewIntent(Intent intent) { + setIntent(intent); + handleIntent(intent); + } + + private void handleIntent(Intent intent) { + if (Intent.ACTION_SEARCH.equals(intent.getAction())) { + String query = intent.getStringExtra(SearchManager.QUERY); + + SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this, + SearchSuggestionsProvider.AUTHORITY, + SearchSuggestionsProvider.MODE); + + // Save recent searches + suggestions.saveRecentQuery(query, null); + + searchQuery = query; + + loadVideos(0, count, sort, filter); + + } + } + + @Override + public boolean onSearchRequested() { + Bundle appData = new Bundle(); + startSearch(null, false, appData, false); + return true; + } + + } diff --git a/app/src/main/res/layout/activity_search.xml b/app/src/main/res/layout/activity_search.xml deleted file mode 100644 index 19a336b..0000000 --- a/app/src/main/res/layout/activity_search.xml +++ /dev/null @@ -1,55 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - diff --git a/app/src/main/res/layout/activity_video_list.xml b/app/src/main/res/layout/activity_video_list.xml index 787af75..354e31b 100644 --- a/app/src/main/res/layout/activity_video_list.xml +++ b/app/src/main/res/layout/activity_video_list.xml @@ -18,6 +18,15 @@ layout="@layout/tool_bar" /> + + + + - + app:showAsAction="always|collapseActionView" + app:actionViewClass="android.support.v7.widget.SearchView" /> Date: Sun, 2 Dec 2018 17:29:13 +0100 Subject: [PATCH 4/4] - Version Bump --- app/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 8a0ce83..8b4c10d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,8 +6,8 @@ android { applicationId "net.schueller.peertube" minSdkVersion 23 targetSdkVersion 28 - versionCode 104 - versionName "1.0.4" + versionCode 105 + versionName "1.0.5" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" dependencies { implementation fileTree(dir: 'libs', include: ['*.jar'])