Thorium-android-app/app/src/main/java/net/schueller/peertube/activity/VideoListActivity.java

285 lines
10 KiB
Java
Raw Normal View History

2018-03-03 01:10:13 +01:00
package net.schueller.peertube.activity;
2018-03-11 02:24:06 +01:00
import android.Manifest;
2018-03-03 01:10:13 +01:00
import android.app.Activity;
2018-03-04 01:06:32 +01:00
import android.content.Intent;
2018-03-04 01:44:34 +01:00
import android.content.SharedPreferences;
2018-03-11 02:24:06 +01:00
import android.content.pm.PackageManager;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
2018-03-11 02:24:06 +01:00
import android.support.v4.app.ActivityCompat;
import android.support.v4.widget.SwipeRefreshLayout;
2018-03-03 01:10:13 +01:00
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;
2018-03-03 01:10:13 +01:00
import android.util.Log;
import android.view.Menu;
2018-03-04 01:06:32 +01:00
import android.view.MenuInflater;
import android.view.MenuItem;
2018-03-03 01:10:13 +01:00
import android.widget.Toast;
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;
2018-03-04 01:06:32 +01:00
import com.joanzapata.iconify.IconDrawable;
import com.joanzapata.iconify.Iconify;
import com.joanzapata.iconify.fonts.FontAwesomeIcons;
import com.joanzapata.iconify.fonts.FontAwesomeModule;
2018-03-03 01:10:13 +01:00
import net.schueller.peertube.R;
import net.schueller.peertube.adapter.VideoAdapter;
import net.schueller.peertube.helper.APIUrlHelper;
import net.schueller.peertube.helper.BottomNavigationViewHelper;
2018-03-03 01:10:13 +01:00
import net.schueller.peertube.model.VideoList;
import net.schueller.peertube.network.GetVideoDataService;
import net.schueller.peertube.network.RetrofitInstance;
import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class VideoListActivity extends AppCompatActivity {
private String TAG = "VideoListActivity";
2018-03-04 01:06:32 +01:00
public static final String EXTRA_VIDEOID = "VIDEOID ";
2018-03-03 01:10:13 +01:00
private VideoAdapter videoAdapter;
private RecyclerView recyclerView;
private SwipeRefreshLayout swipeRefreshLayout;
private Toolbar toolbar;
2018-03-03 01:10:13 +01:00
2018-03-03 02:02:00 +01:00
private int currentStart = 0;
private int count = 12;
private String sort = "-createdAt";
private boolean isLoading = false;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= item -> {
switch (item.getItemId()) {
case R.id.navigation_home:
2018-03-04 01:06:32 +01:00
//Log.v(TAG, "navigation_home");
if (!isLoading) {
sort = "-createdAt";
currentStart = 0;
loadVideos(currentStart, count, sort);
}
return true;
case R.id.navigation_trending:
2018-03-04 01:06:32 +01:00
//Log.v(TAG, "navigation_trending");
if (!isLoading) {
sort = "-views";
currentStart = 0;
loadVideos(currentStart, count, sort);
}
return true;
case R.id.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");
Toast.makeText(VideoListActivity.this, "Account Not Implemented", Toast.LENGTH_SHORT).show();
return false;
}
return false;
};
2018-03-03 01:10:13 +01:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_list);
2018-03-04 01:06:32 +01:00
// Init icons
Iconify.with(new FontAwesomeModule());
// Attaching the layout to the toolbar object
toolbar = findViewById(R.id.tool_bar);
// Setting toolbar as the ActionBar with setSupportActionBar() call
setSupportActionBar(toolbar);
2018-03-03 01:10:13 +01:00
// fix android trying to use SSLv3 for handshake
updateAndroidSecurityProvider(this);
// Bottom Navigation
BottomNavigationView navigation = findViewById(R.id.navigation);
2018-03-04 01:06:32 +01:00
Menu navMenu = navigation.getMenu();
navMenu.findItem(R.id.navigation_home).setIcon(
new IconDrawable(this, FontAwesomeIcons.fa_home));
navMenu.findItem(R.id.navigation_trending).setIcon(
new IconDrawable(this, FontAwesomeIcons.fa_fire));
navMenu.findItem(R.id.navigation_subscriptions).setIcon(
new IconDrawable(this, FontAwesomeIcons.fa_folder));
navMenu.findItem(R.id.navigation_account).setIcon(
new IconDrawable(this, FontAwesomeIcons.fa_user_circle));
BottomNavigationViewHelper.removeShiftMode(navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
// load Video List
2018-03-03 02:02:00 +01:00
createList();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
2018-03-04 01:06:32 +01:00
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
// Set an icon in the ActionBar
menu.findItem(R.id.action_settings).setIcon(
new IconDrawable(this, FontAwesomeIcons.fa_cog)
2018-03-04 01:06:32 +01:00
.colorRes(R.color.cardview_light_background)
.actionBarSize());
return true;
}
2018-03-04 01:06:32 +01:00
@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.
switch (item.getItemId()) {
// action with ID action_refresh was selected
case R.id.action_settings:
2018-03-04 01:06:32 +01:00
// Toast.makeText(this, "Login Selected", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, SettingsActivity.class);
2018-03-04 01:06:32 +01:00
this.startActivity(intent);
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
2018-03-03 02:02:00 +01:00
private void createList() {
recyclerView = findViewById(R.id.recyclerView);
swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
2018-03-03 02:02:00 +01:00
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(VideoListActivity.this);
recyclerView.setLayoutManager(layoutManager);
videoAdapter = new VideoAdapter(new ArrayList<>(), VideoListActivity.this);
recyclerView.setAdapter(videoAdapter);
loadVideos(currentStart, count, sort);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(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);
}
}
}
}
});
swipeRefreshLayout.setOnRefreshListener(() -> {
// Refresh items
if (!isLoading) {
currentStart = 0;
loadVideos(currentStart, count, sort);
}
});
2018-03-03 02:02:00 +01:00
}
private void loadVideos(int start, int count, String sort) {
isLoading = true;
String apiBaseURL = APIUrlHelper.getUrl(this);
2018-03-04 01:44:34 +01:00
GetVideoDataService service = RetrofitInstance.getRetrofitInstance(apiBaseURL + "/api/v1/").create(GetVideoDataService.class);
2018-03-03 01:10:13 +01:00
Call<VideoList> call = service.getVideosData(start, count, sort);
2018-03-03 01:10:13 +01:00
/*Log the URL called*/
Log.d("URL Called", call.request().url() + "");
Toast.makeText(VideoListActivity.this, "URL Called: " + call.request().url(), Toast.LENGTH_SHORT).show();
2018-03-03 01:10:13 +01:00
call.enqueue(new Callback<VideoList>() {
@Override
public void onResponse(@NonNull Call<VideoList> call, @NonNull Response<VideoList> response) {
if (currentStart == 0) {
videoAdapter.clearData();
}
2018-03-03 02:02:00 +01:00
videoAdapter.setData(response.body().getVideoArrayList());
isLoading = false;
swipeRefreshLayout.setRefreshing(false);
2018-03-03 01:10:13 +01:00
}
@Override
public void onFailure(@NonNull Call<VideoList> call, @NonNull Throwable t) {
2018-03-03 01:10:13 +01:00
Log.wtf("err", t.fillInStackTrace());
Toast.makeText(VideoListActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
2018-03-03 02:02:00 +01:00
isLoading = false;
swipeRefreshLayout.setRefreshing(false);
2018-03-03 01:10:13 +01:00
}
});
}
/**
* Force android to not use SSLv3
*
* @param callingActivity Activity
*/
private void updateAndroidSecurityProvider(Activity callingActivity) {
try {
ProviderInstaller.installIfNeeded(this);
} catch (GooglePlayServicesRepairableException e) {
// Thrown when Google Play Services is not installed, up-to-date, or enabled
// Show dialog to allow users to install, update, or otherwise enable Google Play services.
GooglePlayServicesUtil.getErrorDialog(e.getConnectionStatusCode(), callingActivity, 0);
} catch (GooglePlayServicesNotAvailableException e) {
Log.e("SecurityException", "Google Play Services not available.");
}
}
2018-03-11 02:24:06 +01:00
@Override
protected void onResume() {
super.onResume();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
}
}
2018-03-03 01:10:13 +01:00
}