-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathDaysAdapter.java
More file actions
129 lines (110 loc) Β· 5.13 KB
/
DaysAdapter.java
File metadata and controls
129 lines (110 loc) Β· 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.aniketjain.weatherapp.adapter;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.aniketjain.weatherapp.R;
import com.aniketjain.weatherapp.update.UpdateUI;
import com.aniketjain.weatherapp.url.URL;
import com.github.ybq.android.spinkit.SpinKitView;
import org.json.JSONException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class DaysAdapter extends RecyclerView.Adapter<DaysAdapter.DayViewHolder> {
private final Context context;
public DaysAdapter(Context context) {
this.context = context;
}
private String updated_at, min, max, pressure, wind_speed, humidity;
private int condition;
private long update_time, sunset, sunrise;
@NonNull
@Override
public DayViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.day_item_layout, parent, false);
return new DayViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull DayViewHolder holder, int position) {
getDailyWeatherInfo(position + 1, holder);
}
@Override
public int getItemCount() {
return 6;
}
@SuppressLint("DefaultLocale")
private void getDailyWeatherInfo(int i, DayViewHolder holder) {
URL url = new URL();
RequestQueue requestQueue = Volley.newRequestQueue(context);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url.getLink(), null, response -> {
try {
update_time = response.getJSONObject("current").getLong("dt");
updated_at = new SimpleDateFormat("EEEE", Locale.ENGLISH).format(new Date((update_time * 1000) + (i * 864_000_00L))); // i=0
condition = response.getJSONArray("daily").getJSONObject(i).getJSONArray("weather").getJSONObject(0).getInt("id");
sunrise = response.getJSONArray("daily").getJSONObject(i).getLong("sunrise");
sunset = response.getJSONArray("daily").getJSONObject(i).getLong("sunset");
min = String.format("%.0f", response.getJSONArray("daily").getJSONObject(i).getJSONObject("temp").getDouble("min") - 273.15);
max = String.format("%.0f", response.getJSONArray("daily").getJSONObject(i).getJSONObject("temp").getDouble("max") - 273.15);
pressure = response.getJSONArray("daily").getJSONObject(i).getString("pressure");
wind_speed = response.getJSONArray("daily").getJSONObject(i).getString("wind_speed");
humidity = response.getJSONArray("daily").getJSONObject(i).getString("humidity");
updateUI(holder);
hideProgressBar(holder);
} catch (JSONException e) {
e.printStackTrace();
}
}, null);
requestQueue.add(jsonObjectRequest);
Log.i("json_req", "Day " + i);
}
@SuppressLint("SetTextI18n")
private void updateUI(DayViewHolder holder) {
String day = UpdateUI.TranslateDay(updated_at, context);
holder.dTime.setText(day);
holder.temp_min.setText(min + "Β°C");
holder.temp_max.setText(max + "Β°C");
holder.pressure.setText(pressure + " mb");
holder.wind.setText(wind_speed + " km/h");
holder.humidity.setText(humidity + "%");
holder.icon.setImageResource(
context.getResources().getIdentifier(
UpdateUI.getWeatherIconDrawableName(condition, update_time, sunrise, sunset),
"drawable",
context.getPackageName()
));
}
private void hideProgressBar(DayViewHolder holder) {
holder.progress.setVisibility(View.GONE);
holder.layout.setVisibility(View.VISIBLE);
}
static class DayViewHolder extends RecyclerView.ViewHolder {
SpinKitView progress;
RelativeLayout layout;
TextView dTime, temp_min, temp_max, pressure, wind, humidity;
ImageView icon;
public DayViewHolder(@NonNull View itemView) {
super(itemView);
progress = itemView.findViewById(R.id.day_progress_bar);
layout = itemView.findViewById(R.id.day_relative_layout);
dTime = itemView.findViewById(R.id.day_time);
temp_min = itemView.findViewById(R.id.day_min_temp);
temp_max = itemView.findViewById(R.id.day_max_temp);
pressure = itemView.findViewById(R.id.day_pressure);
wind = itemView.findViewById(R.id.day_wind);
humidity = itemView.findViewById(R.id.day_humidity);
icon = itemView.findViewById(R.id.day_icon);
}
}
}