-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTodayWeatherFragment.java
More file actions
133 lines (109 loc) · 5.87 KB
/
TodayWeatherFragment.java
File metadata and controls
133 lines (109 loc) · 5.87 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
130
131
132
133
package com.example.weatherapp;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import com.example.weatherapp.Common.Common;
import com.example.weatherapp.Model.WeatherResult;
import com.example.weatherapp.Retrofit.IOpenWeatherApp;
import com.example.weatherapp.Retrofit.RetrofitClient;
import com.squareup.picasso.Picasso;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
/**
* A simple {@link Fragment} subclass.
*/
public class TodayWeatherFragment extends Fragment {
private static TodayWeatherFragment instance;
private ImageView img_weather;
private TextView text_city, text_temperature, text_details, text_date, text_windSpeed, text_windDeg, text_pressure,
text_humidity, text_sunrise, text_sunset, text_coord;
private LinearLayout weather_panel;
private ProgressBar loading;
private CompositeDisposable compositeDisposable;
private IOpenWeatherApp mService;
static TodayWeatherFragment getInstance() {
if (instance == null) {
instance = new TodayWeatherFragment();
}
System.out.println("getInstance - Instance sent to MainActivity");
return instance;
}
public TodayWeatherFragment() {
compositeDisposable = new CompositeDisposable();
Retrofit retrofit = RetrofitClient.getInstance();
mService = retrofit.create(IOpenWeatherApp.class);
System.out.println ("TodayWeatherFragment Running ...");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
System.out.println("OnCreateView running ...");
// Inflate the layout for this fragment
View itemView = inflater.inflate(R.layout.fragment_today_weather, container, false);
text_city = itemView.findViewById(R.id.text_city);
text_temperature = itemView.findViewById(R.id.text_temperature);
text_details = itemView.findViewById(R.id.text_details);
text_date = itemView.findViewById(R.id.text_date);
text_windSpeed = itemView.findViewById(R.id.text_wind);
text_pressure = itemView.findViewById(R.id.text_pressure);
text_humidity = itemView.findViewById(R.id.text_humidity);
text_sunrise = itemView.findViewById(R.id.text_sunrise);
text_sunset = itemView.findViewById(R.id.text_sunset);
text_coord = itemView.findViewById(R.id.text_coord);
img_weather = itemView.findViewById(R.id.img_weather);
weather_panel = itemView.findViewById(R.id.weather_panel);
loading = itemView.findViewById(R.id.loading);
System.out.println("OnCreateView - loading ...");
getWeatherInformation();
return itemView;
}
private void getWeatherInformation() {
System.out.println ("getWeatherInformation running ...");
compositeDisposable.add(mService.getWeather(String.valueOf(Common.current_location.getLatitude()), String.valueOf(Common.current_location.getLongitude()),
Common.API_ID, "metric")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<WeatherResult>() {
@Override
public void accept(WeatherResult weatherResult) throws Exception {
// Load Image
Picasso.get().load(new StringBuilder("https://openweathermap.org/img/wn/") // /04n@2x.png
.append(weatherResult.getWeather().get(0).getIcon())
.append(".png").toString()).into(img_weather); //https:// icons
// Load Value of Texts
text_city.setText(weatherResult.getName());
text_temperature.setText(new StringBuilder(String.valueOf(weatherResult.getMain().getTemp())).append("°C").toString());
text_details.setText(new StringBuilder("Weather In ").append(weatherResult.getName()).toString());
text_date.setText(Common.convertUnixToDate(weatherResult.getDt()));
text_pressure.setText(new StringBuilder(String.valueOf(weatherResult.getMain().getPressure())).append(" hpa").toString());
text_humidity.setText(new StringBuilder(String.valueOf(weatherResult.getMain().getHumidity())).append("%").toString());
text_sunrise.setText(Common.convertUnixToHour(weatherResult.getSys().getSunrise()));
text_sunset.setText(Common.convertUnixToHour(weatherResult.getSys().getSunset()));
text_coord.setText(new StringBuilder(weatherResult.getCoord().toString()));
// Display Panel
weather_panel.setVisibility(View.VISIBLE);
loading.setVisibility(View.GONE);
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Toast.makeText(getActivity(), "" + throwable.getMessage(), Toast.LENGTH_SHORT).show();
}
}
));
}
public void onStop(){
compositeDisposable.clear();
super.onStop();
}
}