2019-06-03 23:25:05 +02:00
|
|
|
package org.mian.gitnex.fragments;
|
|
|
|
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Bundle;
|
2020-07-07 19:15:01 +02:00
|
|
|
import android.os.Handler;
|
2020-10-12 20:11:23 +02:00
|
|
|
import android.os.Looper;
|
2020-07-07 19:15:01 +02:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ProgressBar;
|
|
|
|
import android.widget.TextView;
|
2019-06-03 23:25:05 +02:00
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
import androidx.lifecycle.Observer;
|
2019-09-12 10:30:07 +02:00
|
|
|
import androidx.lifecycle.ViewModelProvider;
|
2019-06-03 23:25:05 +02:00
|
|
|
import androidx.recyclerview.widget.DividerItemDecoration;
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
|
|
|
import org.mian.gitnex.adapters.ProfileEmailsAdapter;
|
2021-01-26 16:10:31 +01:00
|
|
|
import org.mian.gitnex.databinding.FragmentProfileEmailsBinding;
|
2019-06-03 23:25:05 +02:00
|
|
|
import org.mian.gitnex.helpers.Authorization;
|
|
|
|
import org.mian.gitnex.models.Emails;
|
|
|
|
import org.mian.gitnex.viewmodels.ProfileEmailsViewModel;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Author M M Arif
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class ProfileEmailsFragment extends Fragment {
|
|
|
|
|
|
|
|
private ProgressBar mProgressBar;
|
|
|
|
private ProfileEmailsAdapter adapter;
|
|
|
|
private RecyclerView mRecyclerView;
|
|
|
|
private TextView noDataEmails;
|
|
|
|
private static String repoNameF = "param2";
|
|
|
|
private static String repoOwnerF = "param1";
|
|
|
|
|
|
|
|
private String repoName;
|
|
|
|
private String repoOwner;
|
|
|
|
|
|
|
|
private OnFragmentInteractionListener mListener;
|
|
|
|
|
|
|
|
public ProfileEmailsFragment() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ProfileEmailsFragment newInstance(String param1, String param2) {
|
|
|
|
ProfileEmailsFragment fragment = new ProfileEmailsFragment();
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putString(repoOwnerF, param1);
|
|
|
|
args.putString(repoNameF, param2);
|
|
|
|
fragment.setArguments(args);
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
if (getArguments() != null) {
|
|
|
|
repoName = getArguments().getString(repoNameF);
|
|
|
|
repoOwner = getArguments().getString(repoOwnerF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
|
|
Bundle savedInstanceState) {
|
|
|
|
|
2021-01-26 16:10:31 +01:00
|
|
|
FragmentProfileEmailsBinding fragmentProfileEmailsBinding = FragmentProfileEmailsBinding.inflate(inflater, container, false);
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2021-01-26 16:10:31 +01:00
|
|
|
final SwipeRefreshLayout swipeRefresh = fragmentProfileEmailsBinding.pullToRefresh;
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2021-01-26 16:10:31 +01:00
|
|
|
noDataEmails = fragmentProfileEmailsBinding.noDataEmails;
|
|
|
|
mRecyclerView = fragmentProfileEmailsBinding.recyclerView;
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
mRecyclerView.setHasFixedSize(true);
|
|
|
|
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
|
|
|
|
|
|
|
|
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mRecyclerView.getContext(),
|
|
|
|
DividerItemDecoration.VERTICAL);
|
|
|
|
mRecyclerView.addItemDecoration(dividerItemDecoration);
|
|
|
|
|
2021-01-26 16:10:31 +01:00
|
|
|
mProgressBar = fragmentProfileEmailsBinding.progressBar;
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-10-12 20:11:23 +02:00
|
|
|
swipeRefresh.setOnRefreshListener(() -> new Handler(Looper.getMainLooper()).postDelayed(() -> {
|
|
|
|
|
|
|
|
swipeRefresh.setRefreshing(false);
|
2020-11-02 16:17:00 +01:00
|
|
|
ProfileEmailsViewModel.loadEmailsList(Authorization.get(getContext()), getContext());
|
2020-10-12 20:11:23 +02:00
|
|
|
|
|
|
|
}, 200));
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
fetchDataAsync(Authorization.get(getContext()));
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2021-01-26 16:10:31 +01:00
|
|
|
return fragmentProfileEmailsBinding.getRoot();
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
private void fetchDataAsync(String instanceToken) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2019-09-12 10:30:07 +02:00
|
|
|
ProfileEmailsViewModel profileEmailModel = new ViewModelProvider(this).get(ProfileEmailsViewModel.class);
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
profileEmailModel.getEmailsList(instanceToken, getContext()).observe(getViewLifecycleOwner(), new Observer<List<Emails>>() {
|
2019-06-03 23:25:05 +02:00
|
|
|
@Override
|
|
|
|
public void onChanged(@Nullable List<Emails> emailsListMain) {
|
|
|
|
adapter = new ProfileEmailsAdapter(getContext(), emailsListMain);
|
|
|
|
if(adapter.getItemCount() > 0) {
|
|
|
|
mRecyclerView.setAdapter(adapter);
|
|
|
|
noDataEmails.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
adapter.notifyDataSetChanged();
|
|
|
|
mRecyclerView.setAdapter(adapter);
|
|
|
|
noDataEmails.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
mProgressBar.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onButtonPressed(Uri uri) {
|
|
|
|
if (mListener != null) {
|
|
|
|
mListener.onFragmentInteraction(uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDetach() {
|
|
|
|
super.onDetach();
|
|
|
|
mListener = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface OnFragmentInteractionListener {
|
|
|
|
void onFragmentInteraction(Uri uri);
|
|
|
|
}
|
|
|
|
}
|