2018-03-03 01:10:13 +01:00
|
|
|
package net.schueller.peertube.activity;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
2018-03-03 02:46:01 +01:00
|
|
|
import android.support.annotation.NonNull;
|
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;
|
2018-03-03 02:46:01 +01:00
|
|
|
import android.support.v7.widget.Toolbar;
|
2018-03-03 01:10:13 +01:00
|
|
|
import android.util.Log;
|
2018-03-03 02:46:01 +01:00
|
|
|
import android.view.Menu;
|
|
|
|
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;
|
|
|
|
|
|
|
|
import net.schueller.peertube.R;
|
|
|
|
import net.schueller.peertube.adapter.VideoAdapter;
|
|
|
|
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 VideoAdapter videoAdapter;
|
|
|
|
private RecyclerView recyclerView;
|
2018-03-03 02:46:01 +01:00
|
|
|
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;
|
|
|
|
|
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-03 02:46:01 +01:00
|
|
|
// 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);
|
|
|
|
|
2018-03-03 02:02:00 +01:00
|
|
|
createList();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-03 02:46:01 +01:00
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
// Inflate the menu; this adds items to the action bar if it is present.
|
|
|
|
getMenuInflater().inflate(R.menu.menu_main, menu);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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_user:
|
|
|
|
Toast.makeText(this, "Login Selected", Toast.LENGTH_SHORT)
|
|
|
|
.show();
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
2018-03-03 02:02:00 +01:00
|
|
|
|
|
|
|
private void createList() {
|
|
|
|
recyclerView = findViewById(R.id.recyclerView);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void loadVideos(int start, int count, String sort) {
|
|
|
|
|
|
|
|
isLoading = true;
|
|
|
|
|
2018-03-03 01:10:13 +01:00
|
|
|
GetVideoDataService service = RetrofitInstance.getRetrofitInstance().create(GetVideoDataService.class);
|
|
|
|
|
2018-03-03 02:02:00 +01:00
|
|
|
Call<VideoList> call = service.getVideoData(start, count, sort);
|
2018-03-03 01:10:13 +01:00
|
|
|
|
|
|
|
/*Log the URL called*/
|
|
|
|
Log.wtf("URL Called", call.request().url() + "");
|
|
|
|
|
|
|
|
call.enqueue(new Callback<VideoList>() {
|
|
|
|
@Override
|
2018-03-03 02:46:01 +01:00
|
|
|
public void onResponse(@NonNull Call<VideoList> call, @NonNull Response<VideoList> response) {
|
2018-03-03 02:02:00 +01:00
|
|
|
videoAdapter.setData(response.body().getVideoArrayList());
|
|
|
|
isLoading = false;
|
2018-03-03 01:10:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-03 02:46:01 +01:00
|
|
|
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;
|
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.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|