2020-07-07 16:20:43 +02:00
|
|
|
package org.mian.gitnex.adapters;
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
2020-08-04 22:50:04 +02:00
|
|
|
import android.app.Activity;
|
2020-07-07 16:20:43 +02:00
|
|
|
import android.content.Context;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import androidx.annotation.NonNull;
|
2020-08-04 22:50:04 +02:00
|
|
|
import androidx.appcompat.app.AlertDialog;
|
2021-02-25 00:22:11 +01:00
|
|
|
import androidx.appcompat.content.res.AppCompatResources;
|
2020-07-07 16:20:43 +02:00
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import org.mian.gitnex.R;
|
2020-07-19 12:13:37 +02:00
|
|
|
import org.mian.gitnex.clients.PicassoService;
|
2020-08-04 22:50:04 +02:00
|
|
|
import org.mian.gitnex.database.api.UserAccountsApi;
|
2020-07-07 16:20:43 +02:00
|
|
|
import org.mian.gitnex.database.models.UserAccount;
|
2020-07-19 12:13:37 +02:00
|
|
|
import org.mian.gitnex.helpers.RoundedTransformation;
|
2020-07-07 19:15:01 +02:00
|
|
|
import org.mian.gitnex.helpers.TinyDB;
|
2020-07-07 16:20:43 +02:00
|
|
|
import org.mian.gitnex.helpers.Toasty;
|
|
|
|
import java.util.List;
|
|
|
|
import io.mikael.urlbuilder.UrlBuilder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Author M M Arif
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class UserAccountsAdapter extends RecyclerView.Adapter<UserAccountsAdapter.UserAccountsViewHolder> {
|
|
|
|
|
2021-02-25 00:22:11 +01:00
|
|
|
private final List<UserAccount> userAccountsList;
|
|
|
|
private final Context mCtx;
|
2020-07-07 16:20:43 +02:00
|
|
|
private TinyDB tinyDB;
|
|
|
|
|
2020-08-04 22:50:04 +02:00
|
|
|
class UserAccountsViewHolder extends RecyclerView.ViewHolder {
|
2020-07-07 16:20:43 +02:00
|
|
|
|
2021-02-25 00:22:11 +01:00
|
|
|
private int accountId;
|
|
|
|
private String accountName;
|
|
|
|
|
|
|
|
private final TextView accountUrl;
|
|
|
|
private final TextView userId;
|
|
|
|
private final ImageView activeAccount;
|
|
|
|
private final ImageView deleteAccount;
|
|
|
|
private final ImageView repoAvatar;
|
2020-07-07 16:20:43 +02:00
|
|
|
|
|
|
|
private UserAccountsViewHolder(View itemView) {
|
|
|
|
|
|
|
|
super(itemView);
|
|
|
|
|
|
|
|
accountUrl = itemView.findViewById(R.id.accountUrl);
|
|
|
|
userId = itemView.findViewById(R.id.userId);
|
|
|
|
activeAccount = itemView.findViewById(R.id.activeAccount);
|
|
|
|
deleteAccount = itemView.findViewById(R.id.deleteAccount);
|
2020-07-19 12:13:37 +02:00
|
|
|
repoAvatar = itemView.findViewById(R.id.repoAvatar);
|
2020-07-07 16:20:43 +02:00
|
|
|
|
|
|
|
deleteAccount.setOnClickListener(itemDelete -> {
|
2020-08-04 22:50:04 +02:00
|
|
|
|
|
|
|
new AlertDialog.Builder(mCtx)
|
2021-02-25 00:22:11 +01:00
|
|
|
.setIcon(AppCompatResources.getDrawable(mCtx, R.drawable.ic_delete))
|
2020-08-04 22:50:04 +02:00
|
|
|
.setTitle(mCtx.getResources().getString(R.string.removeAccountPopupTitle))
|
|
|
|
.setMessage(mCtx.getResources().getString(R.string.removeAccountPopupMessage))
|
|
|
|
.setPositiveButton(mCtx.getResources().getString(R.string.removeButton), (dialog, which) -> {
|
|
|
|
|
|
|
|
updateLayoutByPosition(getAdapterPosition());
|
|
|
|
UserAccountsApi userAccountsApi = new UserAccountsApi(mCtx);
|
2021-02-25 00:22:11 +01:00
|
|
|
userAccountsApi.deleteAccount(Integer.parseInt(String.valueOf(accountId)));
|
2020-08-04 22:50:04 +02:00
|
|
|
}).setNeutralButton(mCtx.getResources().getString(R.string.cancelButton), null)
|
|
|
|
.show();
|
2020-07-07 16:20:43 +02:00
|
|
|
});
|
|
|
|
|
2021-02-25 00:22:11 +01:00
|
|
|
itemView.setOnClickListener(switchAccount -> {
|
2020-08-04 22:50:04 +02:00
|
|
|
|
|
|
|
UserAccountsApi userAccountsApi = new UserAccountsApi(mCtx);
|
2021-02-25 00:22:11 +01:00
|
|
|
UserAccount userAccount = userAccountsApi.getAccountData(accountName);
|
2020-08-04 22:50:04 +02:00
|
|
|
|
|
|
|
if(tinyDB.getInt("currentActiveAccountId") != userAccount.getAccountId()) {
|
|
|
|
|
|
|
|
String url = UrlBuilder.fromString(userAccount.getInstanceUrl())
|
|
|
|
.withPath("/")
|
|
|
|
.toString();
|
|
|
|
|
|
|
|
tinyDB.putString("loginUid", userAccount.getUserName());
|
|
|
|
tinyDB.putString("userLogin", userAccount.getUserName());
|
|
|
|
tinyDB.putString(userAccount.getUserName() + "-token", userAccount.getToken());
|
|
|
|
tinyDB.putString("instanceUrl", userAccount.getInstanceUrl());
|
|
|
|
tinyDB.putInt("currentActiveAccountId", userAccount.getAccountId());
|
|
|
|
|
|
|
|
Toasty.success(mCtx, mCtx.getResources().getString(R.string.switchAccountSuccess, userAccount.getUserName(), url));
|
|
|
|
((Activity) mCtx).recreate();
|
|
|
|
}
|
2020-07-07 16:20:43 +02:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public UserAccountsAdapter(Context mCtx, List<UserAccount> userAccountsListMain) {
|
|
|
|
|
|
|
|
this.mCtx = mCtx;
|
|
|
|
this.userAccountsList = userAccountsListMain;
|
|
|
|
}
|
|
|
|
|
2020-08-04 22:50:04 +02:00
|
|
|
private void updateLayoutByPosition(int position) {
|
2020-07-07 16:20:43 +02:00
|
|
|
|
|
|
|
userAccountsList.remove(position);
|
|
|
|
notifyItemRemoved(position);
|
|
|
|
notifyItemRangeChanged(position, userAccountsList.size());
|
2020-08-06 17:16:51 +02:00
|
|
|
Toasty.success(mCtx, mCtx.getResources().getString(R.string.accountDeletedMessage));
|
2020-07-07 16:20:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public UserAccountsAdapter.UserAccountsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
|
|
|
|
|
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_user_accounts, parent, false);
|
|
|
|
return new UserAccountsViewHolder(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressLint("DefaultLocale")
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(@NonNull UserAccountsAdapter.UserAccountsViewHolder holder, int position) {
|
|
|
|
|
|
|
|
UserAccount currentItem = userAccountsList.get(position);
|
2020-11-02 16:17:00 +01:00
|
|
|
tinyDB = TinyDB.getInstance(mCtx);
|
2020-07-07 16:20:43 +02:00
|
|
|
|
|
|
|
String url = UrlBuilder.fromString(currentItem.getInstanceUrl())
|
|
|
|
.withPath("/")
|
|
|
|
.toString();
|
|
|
|
|
2021-02-25 00:22:11 +01:00
|
|
|
holder.accountId = currentItem.getAccountId();
|
|
|
|
holder.accountName = currentItem.getAccountName();
|
|
|
|
|
2020-07-07 16:20:43 +02:00
|
|
|
holder.userId.setText(String.format("@%s", currentItem.getUserName()));
|
|
|
|
holder.accountUrl.setText(url);
|
|
|
|
|
2020-07-19 12:13:37 +02:00
|
|
|
PicassoService.getInstance(mCtx).get().load(url + "img/favicon.png").placeholder(R.drawable.loader_animated).transform(new RoundedTransformation(8, 0)).resize(120, 120).centerCrop().into(holder.repoAvatar);
|
|
|
|
|
2020-07-07 16:20:43 +02:00
|
|
|
if(tinyDB.getInt("currentActiveAccountId") == currentItem.getAccountId()) {
|
2021-02-25 00:22:11 +01:00
|
|
|
|
2020-07-07 16:20:43 +02:00
|
|
|
holder.activeAccount.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
else {
|
2021-02-25 00:22:11 +01:00
|
|
|
|
2020-08-04 22:50:04 +02:00
|
|
|
holder.deleteAccount.setVisibility(View.VISIBLE);
|
2020-07-07 16:20:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return userAccountsList.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|