2019-02-17 16:26:17 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Stefan Schüller <sschueller@techdroid.com>
|
|
|
|
*
|
|
|
|
* License: GPL-3.0+
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package net.schueller.peertube.adapter;
|
|
|
|
|
2020-06-21 17:01:15 +02:00
|
|
|
import android.content.Intent;
|
2019-02-17 16:26:17 +01:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2020-07-04 20:46:32 +02:00
|
|
|
import android.widget.ImageView;
|
2019-02-17 16:26:17 +01:00
|
|
|
import android.widget.TextView;
|
2019-03-03 00:48:33 +01:00
|
|
|
import android.widget.Toast;
|
2019-02-17 16:26:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
import net.schueller.peertube.R;
|
2019-03-02 20:13:02 +01:00
|
|
|
import net.schueller.peertube.activity.SelectServerActivity;
|
2019-02-17 16:26:17 +01:00
|
|
|
import net.schueller.peertube.helper.APIUrlHelper;
|
|
|
|
import net.schueller.peertube.model.Server;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
2020-07-04 20:46:32 +02:00
|
|
|
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
|
|
|
|
|
2020-06-21 17:01:15 +02:00
|
|
|
import static android.app.Activity.RESULT_OK;
|
|
|
|
|
2019-02-17 16:26:17 +01:00
|
|
|
|
|
|
|
public class ServerAdapter extends RecyclerView.Adapter<ServerAdapter.AccountViewHolder> {
|
|
|
|
|
|
|
|
|
|
|
|
private ArrayList<Server> serverList;
|
2019-03-02 20:13:02 +01:00
|
|
|
private SelectServerActivity activity;
|
2019-02-17 16:26:17 +01:00
|
|
|
private String baseUrl;
|
|
|
|
|
2019-03-02 20:13:02 +01:00
|
|
|
public ServerAdapter(ArrayList<Server> serverList, SelectServerActivity activity) {
|
2019-02-17 16:26:17 +01:00
|
|
|
this.serverList = serverList;
|
2019-03-02 20:13:02 +01:00
|
|
|
this.activity = activity;
|
2019-02-17 16:26:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public AccountViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
|
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
|
|
|
|
View view = layoutInflater.inflate(R.layout.row_server, parent, false);
|
|
|
|
|
2019-03-02 20:13:02 +01:00
|
|
|
baseUrl = APIUrlHelper.getUrl(activity);
|
2019-02-17 16:26:17 +01:00
|
|
|
|
|
|
|
return new AccountViewHolder(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(@NonNull AccountViewHolder holder, int position) {
|
|
|
|
|
|
|
|
holder.name.setText(serverList.get(position).getName());
|
|
|
|
holder.host.setText(serverList.get(position).getHost());
|
2019-03-02 20:13:02 +01:00
|
|
|
holder.signupAllowed.setText(activity.getString(R.string.server_selection_signup_allowed, activity.getString(
|
|
|
|
serverList.get(position).getSignupAllowed() ?
|
|
|
|
R.string.server_selection_signup_allowed_yes :
|
|
|
|
R.string.server_selection_signup_allowed_no
|
|
|
|
)));
|
|
|
|
|
2020-07-04 20:46:32 +02:00
|
|
|
holder.videoTotals.setText(
|
|
|
|
activity.getString(R.string.server_selection_video_totals,
|
|
|
|
serverList.get(position).getTotalVideos().toString(),
|
|
|
|
serverList.get(position).getTotalLocalVideos().toString()
|
|
|
|
));
|
|
|
|
|
|
|
|
// don't show description if it hasn't been changes from the default
|
|
|
|
if (!activity.getString(R.string.peertube_instance_search_default_description).equals(serverList.get(position).getShortDescription())) {
|
|
|
|
holder.shortDescription.setText(serverList.get(position).getShortDescription());
|
|
|
|
holder.shortDescription.setVisibility(View.VISIBLE);
|
|
|
|
} else {
|
|
|
|
holder.shortDescription.setVisibility(View.GONE);
|
|
|
|
}
|
2019-03-02 20:13:02 +01:00
|
|
|
|
2020-07-04 20:46:32 +02:00
|
|
|
DefaultArtifactVersion serverVersion = new DefaultArtifactVersion(serverList.get(position).getVersion());
|
2019-03-02 20:13:02 +01:00
|
|
|
|
2020-07-04 20:46:32 +02:00
|
|
|
// at least version 2.2
|
|
|
|
DefaultArtifactVersion minVersion22 = new DefaultArtifactVersion("2.2.0");
|
|
|
|
if (serverVersion.compareTo(minVersion22) >= 0) {
|
|
|
|
// show NSFW Icon
|
|
|
|
if (serverList.get(position).getNSFW()) {
|
|
|
|
holder.isNSFW.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
}
|
2019-03-03 00:48:33 +01:00
|
|
|
|
2020-07-04 20:46:32 +02:00
|
|
|
|
|
|
|
// select server
|
|
|
|
holder.itemView.setOnClickListener(v -> {
|
|
|
|
|
|
|
|
String serverUrl = APIUrlHelper.cleanServerUrl(serverList.get(position).getHost());
|
2019-03-03 00:48:33 +01:00
|
|
|
|
|
|
|
Toast.makeText(activity, activity.getString(R.string.server_selection_set_server, serverUrl), Toast.LENGTH_LONG).show();
|
|
|
|
|
2020-06-21 17:01:15 +02:00
|
|
|
Intent intent = new Intent();
|
|
|
|
intent.putExtra("serverUrl", serverUrl);
|
|
|
|
intent.putExtra("serverName", serverList.get(position).getName());
|
|
|
|
activity.setResult(RESULT_OK, intent);
|
|
|
|
|
|
|
|
activity.finish();
|
|
|
|
|
2019-03-02 20:13:02 +01:00
|
|
|
});
|
|
|
|
|
2019-02-17 16:26:17 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// holder.moreButton.setText(R.string.video_more_icon);
|
|
|
|
// new Iconics.IconicsBuilder().ctx(context).on(holder.moreButton).build();
|
|
|
|
//
|
|
|
|
// holder.moreButton.setOnClickListener(v -> {
|
|
|
|
//
|
|
|
|
// PopupMenu popup = new PopupMenu(context, v);
|
|
|
|
// popup.setOnMenuItemClickListener(menuItem -> {
|
|
|
|
// switch (menuItem.getItemId()) {
|
|
|
|
// case R.id.menu_share:
|
|
|
|
// Intents.Share(context, serverList.get(position));
|
|
|
|
// return true;
|
|
|
|
// default:
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// popup.inflate(R.menu.menu_video_row_mode);
|
|
|
|
// popup.show();
|
|
|
|
//
|
|
|
|
// });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setData(ArrayList<Server> data) {
|
|
|
|
serverList.addAll(data);
|
|
|
|
this.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clearData() {
|
|
|
|
serverList.clear();
|
|
|
|
this.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return serverList.size();
|
|
|
|
}
|
|
|
|
|
2020-07-04 20:46:32 +02:00
|
|
|
static class AccountViewHolder extends RecyclerView.ViewHolder {
|
2019-02-17 16:26:17 +01:00
|
|
|
|
2020-07-04 20:46:32 +02:00
|
|
|
TextView name, host, signupAllowed, shortDescription, videoTotals;
|
|
|
|
ImageView isNSFW;
|
2019-02-17 16:26:17 +01:00
|
|
|
|
|
|
|
AccountViewHolder(View itemView) {
|
|
|
|
super(itemView);
|
2020-07-04 20:46:32 +02:00
|
|
|
name = itemView.findViewById(R.id.sl_row_name);
|
|
|
|
host = itemView.findViewById(R.id.sl_row_host);
|
|
|
|
signupAllowed = itemView.findViewById(R.id.sl_row_signup_allowed);
|
|
|
|
shortDescription = itemView.findViewById(R.id.sl_row_short_description);
|
|
|
|
isNSFW = itemView.findViewById(R.id.sl_row_is_nsfw);
|
|
|
|
videoTotals = itemView.findViewById(R.id.sl_row_video_totals);
|
2019-02-17 16:26:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|