Commit 57ebf6e1 authored by Daniel Cesanelli's avatar Daniel Cesanelli
Browse files

Cambios en News y Producers para cargar sólo una vez

parent eff6598d
......@@ -23,8 +23,8 @@ public class ApiAdapter {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
// String baseUrl = "http://ec2-3-235-40-183.compute-1.amazonaws.com/api/";
String baseUrl = "http://ec2-3-236-129-11.compute-1.amazonaws.com:8080/api/";
String baseUrl = "http://ec2-3-235-40-183.compute-1.amazonaws.com/api/";
// String baseUrl = "http://ec2-3-236-129-11.compute-1.amazonaws.com:8080/api/";
if (API_SERVICE == null) {
ObjectMapper objectMapper = new ObjectMapper();
......
......@@ -10,6 +10,17 @@ public class Producer {
private Image[] images;
private String phone;
public Producer(int id, String name, String lastName, String email, String description) {
this.id = id;
this.name = name;
this.lastName = lastName;
this.email = email;
this.description = description;
}
public Producer() {
}
public int getId() {
return id;
}
......
......@@ -53,45 +53,13 @@ public class NewsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_producers_list, container, false);
Context context = view.getContext();
// Set the adapter
if (view instanceof RecyclerView) {
Call<News> call = ApiAdapter.getApiService().getNews();
call.enqueue(new Callback<News>(){
@Override
public void onResponse(Call<News> call, Response<News> response) {
try {
if(response.isSuccessful()) {
newsListViewModel.setNewsList(response.body().getPage());
}else {
ArrayList<NewsItem> auxList = new ArrayList<NewsItem>();
for (int i = 1; i < 6; i++) {
auxList.add(new NewsItem("Título "+ i,"Subtítulo "+ i, "Este es un texto de prueba", null));
}
newsListViewModel.setNewsList(auxList);
}
} catch (Exception ex){
Toast.makeText(context,ex.getMessage(),Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<News> call, Throwable t) {
Toast.makeText(context,t.getMessage(),Toast.LENGTH_LONG).show();
}
});
}
return view;
return inflater.inflate(R.layout.fragment_producers_list, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
newsListViewModel = new ViewModelProvider(getActivity()).get(NewsListViewModel.class);
newsListViewModel.setNewsList(new ArrayList<NewsItem>());
newsListViewModel.getNewsList().observe(getViewLifecycleOwner(), list ->{
if (list != null) {
RecyclerView recyclerView = (RecyclerView) view;
......
......@@ -50,32 +50,7 @@ public class ProducersFragment extends Fragment implements ProducersRecyclerView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_producers_list, container, false);
Context context = view.getContext();
// Set the adapter
if (view instanceof RecyclerView) {
Call<ArrayList<Producer>> call = ApiAdapter.getApiService().getProducers();
call.enqueue(new Callback<ArrayList<Producer>>(){
@Override
public void onResponse(Call<ArrayList<Producer>> call, Response<ArrayList<Producer>> response) {
try {
if(response.isSuccessful()) {
producerListViewModel.setProducerList(response.body());
}
} catch (Exception ex){
Toast.makeText(context,ex.getMessage(),Toast.LENGTH_LONG);
}
}
@Override
public void onFailure(Call<ArrayList<Producer>> call, Throwable t) {
Toast.makeText(context,t.getMessage(),Toast.LENGTH_LONG);
}
});
}
return view;
return inflater.inflate(R.layout.fragment_producers_list, container, false);
}
@Override
......@@ -83,7 +58,6 @@ public class ProducersFragment extends Fragment implements ProducersRecyclerView
super.onViewCreated(view, savedInstanceState);
producerViewModel = new ViewModelProvider(getActivity()).get(ProducerViewModel.class);
producerListViewModel = new ViewModelProvider(getActivity()).get(ProducerListViewModel.class);
producerListViewModel.setProducerList(new ArrayList<Producer>());
producerListViewModel.getProducerList().observe(getViewLifecycleOwner(), list ->{
if (list != null) {
RecyclerView recyclerView = (RecyclerView) view;
......
......@@ -4,20 +4,58 @@ import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.cesanelli.lajusta.io.ApiAdapter;
import com.cesanelli.lajusta.io.models.News;
import com.cesanelli.lajusta.io.models.NewsItem;
import com.cesanelli.lajusta.io.models.Producer;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class NewsListViewModel extends ViewModel {
final private MutableLiveData<List<NewsItem>> news = new MutableLiveData<List<NewsItem>>();
private MutableLiveData<List<NewsItem>> news;
public LiveData<List<NewsItem>> getNewsList() {
if (news == null) {
news = new MutableLiveData<List<NewsItem>>();
loadNews();
}
return news;
}
public void setNewsList(List<NewsItem> news) {
this.news.setValue(news);
}
private void loadNews() {
Call<News> call = ApiAdapter.getApiService().getNews();
call.enqueue(new Callback<News>() {
@Override
public void onResponse(Call<News> call, Response<News> response) {
try {
if (response.isSuccessful()) {
setNewsList(response.body().getPage());
} else {
ArrayList<NewsItem> auxList = new ArrayList<NewsItem>();
for (int i = 1; i < 6; i++) {
auxList.add(new NewsItem("Título " + i, "Subtítulo " + i, "Este es un texto de prueba", null));
}
setNewsList(auxList);
}
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
@Override
public void onFailure(Call<News> call, Throwable t) {
System.out.println(t.toString());
}
});
}
}
\ No newline at end of file
......@@ -4,18 +4,58 @@ import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.cesanelli.lajusta.io.ApiAdapter;
import com.cesanelli.lajusta.io.models.Producer;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class ProducerListViewModel extends ViewModel {
final private MutableLiveData<List<Producer>> producers = new MutableLiveData<List<Producer>>();
private MutableLiveData<List<Producer>> producers;
public LiveData<List<Producer>> getProducerList() {
if (producers == null) {
producers = new MutableLiveData<List<Producer>>();
loadProducers();
}
return producers;
}
public void setProducerList(List<Producer> producers) {
this.producers.setValue(producers);
}
private void loadProducers() {
Call<ArrayList<Producer>> call = ApiAdapter.getApiService().getProducers();
call.enqueue(new Callback<ArrayList<Producer>>() {
@Override
public void onResponse(Call<ArrayList<Producer>> call, Response<ArrayList<Producer>> response) {
try {
if (response.isSuccessful()) {
setProducerList(response.body());
} else {
ArrayList<Producer> auxList = new ArrayList<Producer>();
for (int i = 1; i < 6; i++) {
auxList.add(new Producer(i, "Nombre " + i, "Apellido " + i, "email@email.com", null));
}
setProducerList(auxList);
}
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
@Override
public void onFailure(Call<ArrayList<Producer>> call, Throwable t) {
System.out.println(t.toString());
}
});
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment