2021-08-08 19:21:56 +02:00
|
|
|
package com.cappielloantonio.play.adapter;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
2021-08-09 17:55:30 +02:00
|
|
|
import android.os.Bundle;
|
2021-08-08 19:21:56 +02:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
2021-08-14 17:37:23 +02:00
|
|
|
import androidx.annotation.NonNull;
|
2021-08-08 19:21:56 +02:00
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
|
|
|
|
|
import com.cappielloantonio.play.App;
|
|
|
|
|
import com.cappielloantonio.play.R;
|
|
|
|
|
import com.cappielloantonio.play.interfaces.SystemCallback;
|
|
|
|
|
import com.cappielloantonio.play.model.Server;
|
|
|
|
|
import com.cappielloantonio.play.repository.SystemRepository;
|
|
|
|
|
import com.cappielloantonio.play.ui.activity.MainActivity;
|
2021-09-14 14:27:39 +02:00
|
|
|
import com.cappielloantonio.play.ui.dialog.ServerSignupDialog;
|
2022-01-11 16:25:11 +01:00
|
|
|
import com.cappielloantonio.play.util.MusicUtil;
|
2021-08-08 19:21:56 +02:00
|
|
|
import com.cappielloantonio.play.util.PreferenceUtil;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class ServerAdapter extends RecyclerView.Adapter<ServerAdapter.ViewHolder> {
|
|
|
|
|
private static final String TAG = "ServerAdapter";
|
|
|
|
|
|
|
|
|
|
private final LayoutInflater mInflater;
|
2021-08-14 17:37:23 +02:00
|
|
|
private final MainActivity mainActivity;
|
|
|
|
|
private final Context context;
|
|
|
|
|
|
|
|
|
|
private List<Server> servers;
|
2021-08-08 19:21:56 +02:00
|
|
|
|
|
|
|
|
public ServerAdapter(MainActivity mainActivity, Context context) {
|
|
|
|
|
this.mInflater = LayoutInflater.from(context);
|
|
|
|
|
this.servers = new ArrayList<>();
|
|
|
|
|
this.mainActivity = mainActivity;
|
|
|
|
|
this.context = context;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-14 17:37:23 +02:00
|
|
|
@NonNull
|
2021-08-08 19:21:56 +02:00
|
|
|
@Override
|
2021-08-14 17:37:23 +02:00
|
|
|
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
2021-08-08 19:21:56 +02:00
|
|
|
View view = mInflater.inflate(R.layout.item_login_server, parent, false);
|
|
|
|
|
return new ViewHolder(view);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onBindViewHolder(ViewHolder holder, int position) {
|
|
|
|
|
Server server = servers.get(position);
|
|
|
|
|
|
|
|
|
|
holder.serverName.setText(server.getServerName());
|
|
|
|
|
holder.serverAddress.setText(server.getAddress());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int getItemCount() {
|
|
|
|
|
return servers.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setItems(List<Server> servers) {
|
|
|
|
|
this.servers = servers;
|
|
|
|
|
notifyDataSetChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Server getItem(int id) {
|
|
|
|
|
return servers.get(id);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-10 09:20:58 +02:00
|
|
|
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
|
2021-08-08 19:21:56 +02:00
|
|
|
TextView serverName;
|
|
|
|
|
TextView serverAddress;
|
|
|
|
|
|
|
|
|
|
ViewHolder(View itemView) {
|
|
|
|
|
super(itemView);
|
|
|
|
|
|
|
|
|
|
serverName = itemView.findViewById(R.id.server_name_text_view);
|
|
|
|
|
serverAddress = itemView.findViewById(R.id.server_address_text_view);
|
|
|
|
|
|
|
|
|
|
itemView.setOnClickListener(this);
|
2021-08-10 09:20:58 +02:00
|
|
|
itemView.setOnLongClickListener(this);
|
2021-08-08 19:21:56 +02:00
|
|
|
|
|
|
|
|
serverName.setSelected(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View view) {
|
|
|
|
|
Server server = servers.get(getBindingAdapterPosition());
|
2022-01-11 15:50:22 +01:00
|
|
|
saveServerPreference(server.getServerId(), server.getAddress(), server.getUsername(), server.isLowSecurity() ? server.getPassword() : null, server.getToken(), server.getSalt());
|
2021-08-09 00:23:09 +02:00
|
|
|
refreshSubsonicClientInstance();
|
2021-08-08 19:21:56 +02:00
|
|
|
|
|
|
|
|
SystemRepository systemRepository = new SystemRepository(App.getInstance());
|
|
|
|
|
systemRepository.checkUserCredential(new SystemCallback() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onError(Exception exception) {
|
|
|
|
|
Toast.makeText(context, exception.getMessage(), Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-01-11 15:50:22 +01:00
|
|
|
public void onSuccess(String password, String token, String salt) {
|
|
|
|
|
saveServerPreference(null, null, null, password, token, salt);
|
2021-08-08 19:21:56 +02:00
|
|
|
enter();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-10 09:20:58 +02:00
|
|
|
@Override
|
|
|
|
|
public boolean onLongClick(View v) {
|
|
|
|
|
Bundle bundle = new Bundle();
|
|
|
|
|
bundle.putParcelable("server_object", servers.get(getBindingAdapterPosition()));
|
|
|
|
|
|
|
|
|
|
ServerSignupDialog dialog = new ServerSignupDialog();
|
|
|
|
|
dialog.setArguments(bundle);
|
|
|
|
|
dialog.show(mainActivity.getSupportFragmentManager(), null);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 19:21:56 +02:00
|
|
|
private void enter() {
|
|
|
|
|
mainActivity.goFromLogin();
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-11 15:50:22 +01:00
|
|
|
private void saveServerPreference(String serverId, String server, String user, String password, String token, String salt) {
|
2021-08-08 19:21:56 +02:00
|
|
|
if (user != null) PreferenceUtil.getInstance(context).setUser(user);
|
2022-01-11 15:50:22 +01:00
|
|
|
if (serverId != null) PreferenceUtil.getInstance(context).setServerId(serverId);
|
2021-08-08 19:21:56 +02:00
|
|
|
if (server != null) PreferenceUtil.getInstance(context).setServer(server);
|
2022-01-11 15:50:22 +01:00
|
|
|
if (password != null) PreferenceUtil.getInstance(context).setPassword(password);
|
2021-08-08 19:21:56 +02:00
|
|
|
|
|
|
|
|
if (token != null && salt != null) {
|
2022-01-11 15:50:22 +01:00
|
|
|
PreferenceUtil.getInstance(context).setPassword(password);
|
2021-08-08 19:21:56 +02:00
|
|
|
PreferenceUtil.getInstance(context).setToken(token);
|
|
|
|
|
PreferenceUtil.getInstance(context).setSalt(salt);
|
|
|
|
|
}
|
2021-08-09 00:23:09 +02:00
|
|
|
}
|
2021-08-08 19:21:56 +02:00
|
|
|
|
2021-08-09 00:23:09 +02:00
|
|
|
private void refreshSubsonicClientInstance() {
|
|
|
|
|
PreferenceUtil.getInstance(context).setServerId(servers.get(getBindingAdapterPosition()).getServerId());
|
2021-08-08 19:21:56 +02:00
|
|
|
App.getSubsonicClientInstance(context, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|