Thorium-android-app/app/src/main/java/net/schueller/peertube/adapter/ServerSearchAdapter.java

175 lines
6.2 KiB
Java
Raw Normal View History

2019-02-17 16:26:17 +01:00
/*
2020-11-28 22:36:47 +01:00
* Copyright (C) 2020 Stefan Schüller <sschueller@techdroid.com>
2019-02-17 16:26:17 +01:00
*
* This program is free software: you can redistribute it and/or modify
2020-11-28 22:36:47 +01:00
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2019-02-17 16:26:17 +01:00
*
* 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
2020-11-28 22:36:47 +01:00
* GNU Affero General Public License for more details.
2019-02-17 16:26:17 +01:00
*
2020-11-28 22:36:47 +01:00
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-02-17 16:26:17 +01:00
*/
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;
import net.schueller.peertube.activity.SearchServerActivity;
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 ServerSearchAdapter extends RecyclerView.Adapter<ServerSearchAdapter.AccountViewHolder> {
2019-02-17 16:26:17 +01:00
private ArrayList<Server> serverList;
private SearchServerActivity activity;
2019-02-17 16:26:17 +01:00
private String baseUrl;
public ServerSearchAdapter(ArrayList<Server> serverList, SearchServerActivity 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());
2020-07-05 14:31:19 +02:00
View view = layoutInflater.inflate(R.layout.row_search_server, parent, false);
2019-02-17 16:26:17 +01:00
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);
2021-01-24 00:31:51 +01:00
// new Iconics.Builder().on(holder.moreButton).build();
2019-02-17 16:26:17 +01:00
//
// 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
}
}
}