Skip to content

Commit

Permalink
Covid-19 Bd Widget
Browse files Browse the repository at this point in the history
  • Loading branch information
AN IK committed Jun 10, 2020
1 parent 29fddc5 commit ed8ef60
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 9 deletions.
48 changes: 44 additions & 4 deletions app/src/main/java/com/example/anikrakib/covid_19bd/AppWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
* Implementation of App Widget functionality.
*/
public class AppWidget extends AppWidgetProvider {


static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager,
final int appWidgetId) {

Expand Down Expand Up @@ -63,7 +66,7 @@ public void onResponse(String response) {
// views[0] = setViewGroupDeath(views[0], death);
// views[0] = setViewGroupRecover(views[0], recover);
// views[0] = setViewGroupTest(views[0], test);
views[0] = setViewGroupLastUpdate(views[0], jsonObject);
// views[0] = setViewGroupLastUpdate(views[0], jsonObject);


Intent intentSync = new Intent(context, AppWidget.class);
Expand Down Expand Up @@ -106,6 +109,7 @@ public void onResponse(String response) {
JSONObject data = jsonObject.getJSONObject("data");

views[0] = setViewGroup(views[0], data);
//views[0] = setViewRate(views[0], data);
views[0] = setViewGroupSource(views[0], jsonObject);

Intent intentSync = new Intent(context, AppWidget.class);
Expand Down Expand Up @@ -149,6 +153,7 @@ public void onResponse(String response) {


views[0] = setViewGroupActive(views[0], jsonObject);
views[0] = setViewGroupLastUpdate(views[0], jsonObject);


Intent intentSync = new Intent(context, AppWidget.class);
Expand Down Expand Up @@ -183,7 +188,7 @@ public void onErrorResponse(VolleyError error) {

queue.add(stringRequest);

queue.add(stringRequest1);//this request use for active cases
queue.add(stringRequest1);
}

@Override
Expand Down Expand Up @@ -243,6 +248,7 @@ public void onReceive(Context context, Intent intent) {
public void onDisabled(Context context) {

}

static RemoteViews setViewGroup(RemoteViews views, JSONObject data){
try {
views.setTextViewText(R.id.todayPositiveBD,getdateInEnglish(data.getString("new_infected")));
Expand All @@ -252,6 +258,8 @@ static RemoteViews setViewGroup(RemoteViews views, JSONObject data){
views.setTextViewText(R.id.todayDeathBD, getdateInEnglish(data.getString("new_death")));
views.setTextViewText(R.id.deceasedTv, getdateInEnglish(data.getString("total_death")));
views.setTextViewText(R.id.todayTestBD, getdateInEnglish(data.getString("new_test")));

//views.setTextViewText(R.id.rate, "Today Death Rate "+todayDeath+"%\nToday Positive Rate "+todayPositive+"%\nToday Test Rate "+todayTest+"%\nToday Recover Rate "+todayRecover+"%\n");
} catch (JSONException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -289,6 +297,31 @@ static RemoteViews setViewGroupCases(RemoteViews views, JSONObject data){
}
return views;
}
static RemoteViews setViewRate(RemoteViews views, JSONObject data){
try {

int totalDeath,totalRecover,totalTest,totalPositive,todayDeath,todayPositive,todayTest,todayRecover;
float deathRate,positiveRate,testRate,recoverRate;
todayPositive=Integer.parseInt(getdateInEnglish(data.getString("new_infected")));
todayDeath=Integer.parseInt(getdateInEnglish(data.getString("new_death")));
todayRecover=Integer.parseInt(getdateInEnglish(data.getString("new_cured")));
todayTest=Integer.parseInt(getdateInEnglish(data.getString("new_test")));
totalPositive=Integer.parseInt(getdateInEnglish(data.getString("total_infected")));
totalDeath=Integer.parseInt(getdateInEnglish(data.getString("total_death")));
totalRecover=Integer.parseInt(getdateInEnglish(data.getString("total_cured")));
totalTest=Integer.parseInt(getdateInEnglish(data.getString("total_test")));

deathRate = (todayDeath*100)/totalDeath;
positiveRate = (todayPositive*100)/totalPositive;
recoverRate = (todayRecover*100)/totalRecover;
testRate = (todayTest*100)/totalTest;

//views.setTextViewText(R.id.rate, "Today Death Rate "+deathRate+"%");
} catch (JSONException e) {
e.printStackTrace();
}
return views;
}
static RemoteViews setViewGroupDeath(RemoteViews views, JSONObject data){
try {
views.setTextViewText(R.id.todayDeathBD, data.getString("last24"));
Expand Down Expand Up @@ -343,15 +376,22 @@ static RemoteViews setViewGroupSource(RemoteViews views, JSONObject data){
static RemoteViews setViewGroupLastUpdate(RemoteViews views, JSONObject data){
try {

views.setTextViewText(R.id.lastUpdatedTv,data.getString("updated_on"));
//views.setTextViewText(R.id.lastUpdatedTv,data.getString("updated_on"));
views.setTextViewText(R.id.lastUpdatedTv,getDate(data.getLong("updated")));

} catch (JSONException e) {
e.printStackTrace();
}
return views;
}
private static String getDate(long milliSecond){
// Mon, 23 Mar 2020 02:01:04 PM
SimpleDateFormat formatter = new SimpleDateFormat("EEEE | dd MMM yyyy | hh:mm:ss aaa");


Calendar calendar= Calendar.getInstance();
calendar.setTimeInMillis(milliSecond);
return formatter.format(calendar.getTime());
}
//Check if device is connected to a network
private boolean isNetworkConnected(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Expand Down
38 changes: 33 additions & 5 deletions app/src/main/res/layout/app_widget.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@
android:src="@drawable/coronaviruslogo"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Update"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="#000000"
android:textSize="13sp"
android:textStyle="bold"

android:gravity="center_horizontal" />
<TextView
android:id="@+id/lastUpdatedTv"
android:layout_width="wrap_content"
Expand Down Expand Up @@ -311,7 +322,7 @@
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:padding="10dp">
android:padding="0dp">

<TextView
android:layout_width="wrap_content"
Expand Down Expand Up @@ -384,15 +395,15 @@
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:padding="10dp">
android:padding="0dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:fontFamily="@font/gilroybold"
android:gravity="center"
android:text="Today Death"
android:text="Today Recover"
android:textAlignment="center"
android:textColor="@color/txtWhite"
android:textStyle="bold"
Expand Down Expand Up @@ -431,7 +442,7 @@
android:background="@drawable/today_test_bg"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
android:padding="0dp">

<TextView
android:layout_width="wrap_content"
Expand Down Expand Up @@ -469,7 +480,7 @@
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:padding="10dp">
android:padding="0dp">

<TextView
android:layout_width="wrap_content"
Expand Down Expand Up @@ -499,6 +510,23 @@
</LinearLayout>
</LinearLayout>

<!-- <TextView-->
<!-- android:id="@+id/rate"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_marginRight="20dp"-->
<!-- android:layout_marginLeft="20dp"-->
<!-- android:layout_marginTop="5dp"-->
<!-- android:text="Today Death rate 10.6%"-->
<!-- android:textColor="@color/txtWhite"-->
<!-- android:textStyle="bold"-->
<!-- android:padding="5dp"-->
<!-- android:textAlignment="center"-->
<!-- android:background="@drawable/custom2"-->
<!-- android:gravity="center_horizontal"/>-->

<!-- </TextView>-->

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down

0 comments on commit ed8ef60

Please sign in to comment.