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;
|
|
|
|
|
|
|
|
|
|
import androidx.fragment.app.FragmentManager;
|
2021-08-09 17:55:30 +02:00
|
|
|
import androidx.navigation.Navigation;
|
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.model.Song;
|
|
|
|
|
import com.cappielloantonio.play.repository.SystemRepository;
|
|
|
|
|
import com.cappielloantonio.play.ui.activity.MainActivity;
|
2021-08-09 17:55:30 +02:00
|
|
|
import com.cappielloantonio.play.ui.fragment.dialog.ServerSignupDialog;
|
2021-08-08 19:21:56 +02:00
|
|
|
import com.cappielloantonio.play.util.PreferenceUtil;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
|
|
public class ServerAdapter extends RecyclerView.Adapter<ServerAdapter.ViewHolder> {
|
|
|
|
|
private static final String TAG = "ServerAdapter";
|
|
|
|
|
|
|
|
|
|
private List<Server> servers;
|
|
|
|
|
private final LayoutInflater mInflater;
|
|
|
|
|
private MainActivity mainActivity;
|
|
|
|
|
private Context context;
|
|
|
|
|
|
|
|
|
|
public ServerAdapter(MainActivity mainActivity, Context context) {
|
|
|
|
|
this.mInflater = LayoutInflater.from(context);
|
|
|
|
|
this.servers = new ArrayList<>();
|
|
|
|
|
this.mainActivity = mainActivity;
|
|
|
|
|
this.context = context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
|
|
|
|
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 List<Server> getItems() {
|
|
|
|
|
return this.servers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setItems(List<Server> servers) {
|
|
|
|
|
this.servers = servers;
|
|
|
|
|
notifyDataSetChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Server getItem(int id) {
|
|
|
|
|
return servers.get(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
serverName.setSelected(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View view) {
|
|
|
|
|
Server server = servers.get(getBindingAdapterPosition());
|
|
|
|
|
saveServerPreference(server.getAddress(), server.getUsername(), 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
|
|
|
|
|
public void onSuccess(String token, String salt) {
|
|
|
|
|
saveServerPreference(null, null, token, salt);
|
|
|
|
|
enter();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void enter() {
|
|
|
|
|
mainActivity.goFromLogin();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void saveServerPreference(String server, String user, String token, String salt) {
|
|
|
|
|
if (user != null) PreferenceUtil.getInstance(context).setUser(user);
|
|
|
|
|
if (server != null) PreferenceUtil.getInstance(context).setServer(server);
|
|
|
|
|
|
|
|
|
|
if (token != null && salt != null) {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|