- Implemented basic search

This commit is contained in:
Stefan Schueller 2018-11-10 22:40:27 +01:00
parent 2552d13382
commit 6e7d85c74a
14 changed files with 360 additions and 93 deletions

View File

@ -45,6 +45,10 @@ dependencies {
// implementation 'org.webrtc:google-webrtc:1.0.+'
implementation 'com.android.support:design:28.0.0'
// BottomNavigationViewEx -> https://github.com/ittianyu/BottomNavigationViewEx
implementation 'com.github.ittianyu:BottomNavigationViewEx:2.0.2'
implementation 'com.blackboardtheory:android-iconify-fontawesome:3.0.1-SNAPSHOT'
implementation 'com.github.TorrentStream:TorrentStream-Android:2.5.0'

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="net.schueller.peertube">
<uses-permission android:name="android.permission.INTERNET" />
@ -11,30 +12,59 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
tools:ignore="GoogleAppIndexingWarning"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".activity.VideoListActivity">
<meta-data android:name="android.app.default_searchable"
android:value=".activity.SearchActivity" />
<activity
android:name=".activity.VideoListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.SearchActivity"
android:label="@string/title_activity_search"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<activity
android:name=".activity.LoginActivity"
android:label="@string/title_activity_login" />
<activity
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode"
android:launchMode="singleTop"
android:name=".activity.VideoPlayActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode"
android:label="@string/title_activity_video_play"
android:launchMode="singleTop"
android:theme="@style/AppTheme" />
<activity
android:name=".activity.SettingsActivity"
android:label="@string/title_activity_settings" />
<!-- Content provider for search suggestions -->
<provider android:name=".provider.SearchSuggestionsProvider"
android:enabled="true"
android:authorities="net.schueller.peertube.provider.SearchSuggestionsProvider"
android:exported="false" />
</application>
</manifest>

View File

@ -0,0 +1,177 @@
package net.schueller.peertube.activity;
import android.app.SearchManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.provider.SearchRecentSuggestions;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
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.widget.Toast;
import net.schueller.peertube.R;
import net.schueller.peertube.adapter.VideoAdapter;
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;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
// TODO: cleanup, this code partially is duplicated from VideoList Activity and should be seperated out
public class SearchActivity extends AppCompatActivity {
private VideoAdapter videoAdapter;
private SwipeRefreshLayout swipeRefreshLayout;
private int currentStart = 0;
private int count = 12;
private String sort = "-match";
private String filter = "";
private boolean isLoading = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
Intent intent = getIntent();
// do search
handleIntent(intent);
// handle search suggestions
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);
}
}
private void createList(String query) {
RecyclerView recyclerView = findViewById(R.id.recyclerView);
swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(SearchActivity.this);
recyclerView.setLayoutManager(layoutManager);
videoAdapter = new VideoAdapter(new ArrayList<>(), 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", true) ? "both" : "false";
String apiBaseURL = APIUrlHelper.getUrl(this);
GetVideoDataService service = RetrofitInstance.getRetrofitInstance(apiBaseURL + "/api/v1/").create(GetVideoDataService.class);
Call<VideoList> 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<VideoList>() {
@Override
public void onResponse(@NonNull Call<VideoList> call, @NonNull Response<VideoList> response) {
if (currentStart == 0) {
videoAdapter.clearData();
}
if (response.body() != null) {
videoAdapter.setData(response.body().getVideoArrayList());
}
isLoading = false;
swipeRefreshLayout.setRefreshing(false);
}
@Override
public void onFailure(@NonNull Call<VideoList> 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;
}
}

View File

@ -7,8 +7,8 @@ import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
@ -26,6 +26,7 @@ import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.security.ProviderInstaller;
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;
import com.joanzapata.iconify.IconDrawable;
import com.joanzapata.iconify.Iconify;
import com.joanzapata.iconify.fonts.FontAwesomeIcons;
@ -34,7 +35,6 @@ import com.joanzapata.iconify.fonts.FontAwesomeModule;
import net.schueller.peertube.R;
import net.schueller.peertube.adapter.VideoAdapter;
import net.schueller.peertube.helper.APIUrlHelper;
import net.schueller.peertube.helper.BottomNavigationViewHelper;
import net.schueller.peertube.model.VideoList;
import net.schueller.peertube.network.GetVideoDataService;
import net.schueller.peertube.network.RetrofitInstance;
@ -53,19 +53,16 @@ public class VideoListActivity extends AppCompatActivity {
public static final String EXTRA_VIDEOID = "VIDEOID ";
private VideoAdapter videoAdapter;
private RecyclerView recyclerView;
private SwipeRefreshLayout swipeRefreshLayout;
private Toolbar toolbar;
private int currentStart = 0;
private int count = 12;
private String sort = "-createdAt";
private String filter = "";
private String nsfw = "false";
private boolean isLoading = false;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
private BottomNavigationViewEx.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= item -> {
switch (item.getItemId()) {
case R.id.navigation_home:
@ -82,20 +79,20 @@ public class VideoListActivity extends AppCompatActivity {
//Log.v(TAG, "navigation_trending");
if (!isLoading) {
sort = "-views";
sort = "-trending";
currentStart = 0;
loadVideos(currentStart, count, sort, filter);
}
return true;
case R.id.navigation_subscriptions:
Log.v(TAG, "navigation_subscriptions");
//Log.v(TAG, "navigation_subscriptions");
Toast.makeText(VideoListActivity.this, "Subscriptions Not Implemented", Toast.LENGTH_SHORT).show();
return false;
case R.id.navigation_account:
Log.v(TAG, "navigation_account");
//Log.v(TAG, "navigation_account");
Toast.makeText(VideoListActivity.this, "Account Not Implemented", Toast.LENGTH_SHORT).show();
return false;
@ -114,7 +111,7 @@ public class VideoListActivity extends AppCompatActivity {
Iconify.with(new FontAwesomeModule());
// Attaching the layout to the toolbar object
toolbar = findViewById(R.id.tool_bar);
Toolbar toolbar = findViewById(R.id.tool_bar);
// Setting toolbar as the ActionBar with setSupportActionBar() call
setSupportActionBar(toolbar);
@ -122,7 +119,12 @@ public class VideoListActivity extends AppCompatActivity {
updateAndroidSecurityProvider(this);
// Bottom Navigation
BottomNavigationView navigation = findViewById(R.id.navigation);
BottomNavigationViewEx navigation = findViewById(R.id.navigation);
navigation.enableAnimation(false);
navigation.enableShiftingMode(false);
navigation.enableItemShiftingMode(false);
Menu navMenu = navigation.getMenu();
navMenu.findItem(R.id.navigation_home).setIcon(
new IconDrawable(this, FontAwesomeIcons.fa_home));
@ -133,8 +135,6 @@ public class VideoListActivity extends AppCompatActivity {
navMenu.findItem(R.id.navigation_account).setIcon(
new IconDrawable(this, FontAwesomeIcons.fa_user_circle));
BottomNavigationViewHelper.removeShiftMode(navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
@ -172,8 +172,8 @@ public class VideoListActivity extends AppCompatActivity {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case R.id.action_search:
// TODO: implement
Toast.makeText(this, "Search Selected", Toast.LENGTH_SHORT).show();
//Toast.makeText(this, "Search Selected", Toast.LENGTH_SHORT).show();
onSearchRequested();
return false;
case R.id.action_settings:
@ -190,7 +190,7 @@ public class VideoListActivity extends AppCompatActivity {
}
private void createList() {
recyclerView = findViewById(R.id.recyclerView);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(VideoListActivity.this);
@ -203,12 +203,12 @@ public class VideoListActivity extends AppCompatActivity {
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
if (dy > 0) {
// is at end of list?
@ -238,7 +238,7 @@ public class VideoListActivity extends AppCompatActivity {
isLoading = true;
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
nsfw = sharedPref.getBoolean("pref_show_nsfw", true) ? "both" : "false";
String nsfw = sharedPref.getBoolean("pref_show_nsfw", true) ? "both" : "false";
String apiBaseURL = APIUrlHelper.getUrl(this);

View File

@ -1,34 +0,0 @@
package net.schueller.peertube.helper;
import android.annotation.SuppressLint;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.internal.BottomNavigationMenuView;
import android.support.design.widget.BottomNavigationView;
import android.util.Log;
import java.lang.reflect.Field;
public class BottomNavigationViewHelper {
@SuppressLint("RestrictedApi")
public static void removeShiftMode(BottomNavigationView view) {
BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
try {
Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
shiftingMode.setAccessible(true);
shiftingMode.setBoolean(menuView, false);
shiftingMode.setAccessible(false);
for (int i = 0; i < menuView.getChildCount(); i++) {
BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
//noinspection RestrictedApi
//item.setShiftingMode(false);
// set once again checked value, so view will be updated
//noinspection RestrictedApi
item.setChecked(item.getItemData().isChecked());
}
} catch (NoSuchFieldException e) {
Log.e("BottomNav", "Unable to get shift mode field", e);
} catch (IllegalAccessException e) {
Log.e("BottomNav", "Unable to change value of shift mode", e);
}
}
}

View File

@ -23,12 +23,13 @@ public interface GetVideoDataService {
@Path(value = "id", encoded = true) String id
);
@GET("videos/search/")
@GET("search/videos/")
Call<VideoList> searchVideosData(
@Query("start") int start,
@Query("count") int count,
@Query("sort") String sort,
@Query("filter") String filter,
@Query("nsfw") String nsfw,
@Query("search") String search
// @Query("filter") String filter
);
}

View File

@ -0,0 +1,17 @@
package net.schueller.peertube.provider;
import android.content.SearchRecentSuggestionsProvider;
public class SearchSuggestionsProvider extends SearchRecentSuggestionsProvider {
public final static String AUTHORITY = SearchSuggestionsProvider.class.getName();
public static final int MODE = SearchRecentSuggestionsProvider.DATABASE_MODE_QUERIES;
public SearchSuggestionsProvider()
{
setupSuggestions(AUTHORITY, MODE);
}
// TODO: add search suggestions once they become available in peertube server
}

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:color="@color/colorAccent"
android:state_checked="true"
android:state_pressed="true"
android:state_focused="true"
android:state_selected="true"
android:state_checkable="true"
android:state_enabled="true"
/>
<item
android:color="@color/viewBg"
android:state_checked="false"
android:state_pressed="false"
android:state_focused="false"
android:state_selected="false"
android:state_checkable="false"
android:state_enabled="false"
/>
</selector>

View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="net.schueller.peertube.activity.SearchActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/appbar"
android:layout_weight="1"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.constraint.ConstraintLayout>

View File

@ -37,17 +37,19 @@
</android.support.v4.widget.SwipeRefreshLayout>
<android.support.design.widget.BottomNavigationView
<com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_gravity="bottom"
app:menu="@menu/menu_bottom"
android:layout_gravity="bottom" />
app:itemIconTint="@color/bottom_bar"
android:background="?android:attr/windowBackground"
/>
</android.support.design.widget.CoordinatorLayout>

View File

@ -19,8 +19,10 @@
android:id="@+id/thumb"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxHeight="300dp"
android:contentDescription="@string/video_row_video_thumbnail"
android:scaleType="fitXY"
android:adjustViewBounds="true"
/>
<de.hdodenhof.circleimageview.CircleImageView

View File

@ -1,44 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Unused? delete? -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
<!--<android.support.v7.widget.CardView-->
android:layout_margin="16dp"
android:layout_width="220dp"
android:layout_gravity="center"
android:layout_height="wrap_content">
<!--android:layout_margin="16dp"-->
<!--android:layout_width="220dp"-->
<!--android:layout_gravity="center"-->
<!--android:layout_height="wrap_content">-->
<LinearLayout
android:id="@+id/linearLayout"
android:padding="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--<LinearLayout-->
<!--android:id="@+id/linearLayout"-->
<!--android:padding="8dp"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:orientation="vertical">-->
<ImageView
android:id="@+id/thumb"
android:layout_margin="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/name"
android:layout_margin="5dp"
android:gravity="center"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="username"/>
<!--<ImageView-->
<!--android:id="@+id/thumb"-->
<!--android:layout_margin="5dp"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content" />-->
<!--<TextView-->
<!--android:id="@+id/name"-->
<!--android:layout_margin="5dp"-->
<!--android:gravity="center"-->
<!--android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="username"/>-->
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!--<TextView-->
<!--android:id="@+id/description"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--/>-->
<!--</LinearLayout>-->
<!--</android.support.v7.widget.CardView>-->
</LinearLayout>

View File

@ -42,7 +42,6 @@
<string name="meta_data_owner_seperator">\@</string>
<string name="video_row_video_thumbnail">Video Thumbnail</string>
<string name="video_row_account_avatar">Account Avatar</string>
@ -54,5 +53,7 @@
<string name="pref_title_license">License</string>
<string name="pref_description_license">\n<b>GNU Affero General Public License v3.0</b>\n\nPermissions of this strongest copyleft license are conditioned on making available complete source code of licensed works and modifications, which include larger works using a licensed work, under the same license. Copyright and license notices must be preserved. Contributors provide an express grant of patent rights. When a modified version is used to provide a service over a network, the complete source code of the modified version must be made available.</string>
<string name="pref_title_version">Version</string>
<string name="search_hint">Search PeerTube</string>
<string name="title_activity_search">Search</string>
</resources>

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint"
android:searchSuggestAuthority="net.schueller.peertube.provider.SearchSuggestionsProvider"
android:searchSuggestSelection=" ? ">
</searchable>