I have a weather app that allows the user to save Cities (with nickname and zip) into a room database. I'm using mvvm and livedata to observe the list of cities and update the recyclerview accordingly. I then perform an API call for each item in the recyclerview, which to me seems wrong. It works but seems wrong.
I'd appreciate any reviews on things I could do to improve.
public class RecyclerAdapter extends ListAdapter<MyCity, RecyclerAdapter.ViewHolder> {
private static final DiffUtil.ItemCallback<MyCity> DIFF_CALLBACK = new DiffUtil.ItemCallback<MyCity>() {
@Override
public boolean areItemsTheSame(@NonNull MyCity oldItem, @NonNull MyCity newItem) {
return oldItem.getId() == newItem.getId();
}
@Override
public boolean areContentsTheSame(@NonNull MyCity oldItem, @NonNull MyCity newItem) {
return oldItem.getZipCode() == newItem.getZipCode();
}
};
public RecyclerAdapter() {
super(DIFF_CALLBACK);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item_list, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
MyCity city = getItem(position);
WeatherApi weatherApi = RetrofitService.createService(WeatherApi.class);
weatherApi.getWeatherWithZip(city.getZipCode(), Constants.API_KEY).enqueue(new Callback<WeatherResponse>() {
@Override
public void onResponse(Call<WeatherResponse> call, Response<WeatherResponse> response) {
if (response.isSuccessful()) {
String zip = String.valueOf(city.getZipCode());
holder.zip.setText(zip);
holder.city.setText(response.body().getName());
holder.weather.setText(Conversions.kelvinToFahrenheit(response.body().getMain().getTemp()) + "°C");
}
}
@Override
public void onFailure(Call<WeatherResponse> call, Throwable t) {
}
});
}
public MyCity getCityAt(int position) {
return getItem(position);
}
class ViewHolder extends RecyclerView.ViewHolder {
private TextView zip, city, weather;
public ViewHolder(@NonNull View itemView) {
super(itemView);
zip = itemView.findViewById(R.id.tv_zip);
city = itemView.findViewById(R.id.tv_city);
weather = itemView.findViewById(R.id.tv_weather);
}
}
}
EDIT: I just got an email from openweatherapi... I made 11791 calls in a minute. This is definitely not ideal.