GitNex/app/src/main/java/org/mian/gitnex/fragments/profile/DetailFragment.java
qwerty287 6a84e4d897 Extend CI workflows (#1155)
Extend CI workflows (-> https://codeberg.org/gitnex/GitNex/issues/1099)
	1. pull translations and push them again on every pull
       @mmarif this needs the secrets and `BOT_TOKEN`
       the code for this comes from tea4j-autodeploy
    2. add a **template** for a check workflow which only does some static checks right now. It is disabled because it requires some investigation and discussion on how we should deal with these issues. The checks are currently (if we would enable it):
    	1. check code style and formatting according to the project file in `.idea/`
        2. make sure every Java file has an `@author` annotation

I would wait until you reviewed with enabling them because we have to reformat all the files which results in changes with a few thousand changed lines (tried this once).

Closes https://codeberg.org/gitnex/GitNex/issues/141

Co-authored-by: qwerty287 <ndev@web.de>
Co-authored-by: M M Arif <mmarif@noreply.codeberg.org>
Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1155
Reviewed-by: 6543 <6543@noreply.codeberg.org>
Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org>
Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-07-29 14:08:44 +02:00

161 lines
5 KiB
Java

package org.mian.gitnex.fragments.profile;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import org.gitnex.tea4j.v2.models.User;
import org.mian.gitnex.R;
import org.mian.gitnex.clients.PicassoService;
import org.mian.gitnex.clients.RetrofitClient;
import org.mian.gitnex.databinding.FragmentProfileDetailBinding;
import org.mian.gitnex.helpers.AlertDialogs;
import org.mian.gitnex.helpers.AppUtil;
import org.mian.gitnex.helpers.ClickListener;
import org.mian.gitnex.helpers.ColorInverter;
import org.mian.gitnex.helpers.RoundedTransformation;
import org.mian.gitnex.helpers.TimeHelper;
import org.mian.gitnex.helpers.TinyDB;
import org.mian.gitnex.helpers.Toasty;
import java.util.Locale;
import jp.wasabeef.picasso.transformations.BlurTransformation;
import retrofit2.Call;
import retrofit2.Callback;
/**
* @author M M Arif
*/
public class DetailFragment extends Fragment {
private Context context;
private FragmentProfileDetailBinding binding;
Locale locale;
TinyDB tinyDb;
private static final String usernameBundle = "";
private String username;
public DetailFragment() {}
public static DetailFragment newInstance(String username) {
DetailFragment fragment = new DetailFragment();
Bundle args = new Bundle();
args.putString(usernameBundle, username);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
username = getArguments().getString(usernameBundle);
}
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
binding = FragmentProfileDetailBinding.inflate(inflater, container, false);
context = getContext();
tinyDb = TinyDB.getInstance(context);
locale = getResources().getConfiguration().locale;
getProfileDetail(username);
return binding.getRoot();
}
public void getProfileDetail(String username) {
Call<User> call = RetrofitClient
.getApiInterface(context)
.userGet(username);
call.enqueue(new Callback<User>() {
@Override
public void onResponse(@NonNull Call<User> call, @NonNull retrofit2.Response<User> response) {
if(response.isSuccessful() && response.body() != null) {
switch(response.code()) {
case 200:
String username = !response.body().getFullName().isEmpty() ? response.body().getFullName() : response.body().getLogin();
String email = !response.body().getEmail().isEmpty() ? response.body().getEmail() : "";
int imgRadius = AppUtil.getPixelsFromDensity(context, 3);
String timeFormat = tinyDb.getString("dateFormat", "pretty");
binding.userFullName.setText(username);
binding.userLogin.setText(getString(R.string.usernameWithAt, response.body().getLogin()));
binding.userEmail.setText(email);
String[] userLanguageCodes = response.body().getLanguage().split("-");
if(userLanguageCodes.length >= 2) {
Locale locale = new Locale(userLanguageCodes[0], userLanguageCodes[1]);
binding.userLang.setText(locale.getDisplayLanguage());
}
else {
binding.userLang.setText(locale.getDisplayLanguage());
}
PicassoService.getInstance(context).get()
.load(response.body().getAvatarUrl())
.transform(new RoundedTransformation(imgRadius, 0))
.placeholder(R.drawable.loader_animated)
.resize(120, 120)
.centerCrop()
.into(binding.userAvatar);
PicassoService.getInstance(context).get()
.load(response.body().getAvatarUrl())
.transform(new BlurTransformation(context))
.into(binding.userAvatarBackground, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
int invertedColor = new ColorInverter().getImageViewContrastColor(binding.userAvatarBackground);
binding.userFullName.setTextColor(invertedColor);
binding.userLogin.setTextColor(invertedColor);
}
@Override public void onError(Exception e) {}
});
binding.userJoinedOn.setText(TimeHelper.formatTime(response.body().getCreated(), locale, timeFormat, context));
if(timeFormat.equals("pretty")) {
binding.userJoinedOn.setOnClickListener(new ClickListener(TimeHelper.customDateFormatForToastDateFormat(response.body().getCreated()), context));
}
break;
case 401:
AlertDialogs
.authorizationTokenRevokedDialog(context);
break;
case 403:
Toasty.error(context, context.getString(R.string.authorizeError));
break;
default:
Toasty.error(context, getString(R.string.genericError));
break;
}
}
binding.progressBar.setVisibility(View.GONE);
}
@Override
public void onFailure(@NonNull Call<User> call, @NonNull Throwable t) {
Toasty.error(context, context.getResources().getString(R.string.genericError));
}
});
}
}