Skip to content

Commit

Permalink
fixed preference,data duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
arjunadhikary committed Oct 19, 2020
1 parent 14578b4 commit 1f31f9b
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 61 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
/captures
.externalNativeBuild
.cxx
/app/src/main/java/com/arjun/weather/API.java
Binary file modified app/release/Weather.apk
Binary file not shown.
Binary file removed app/release/app-release.apk
Binary file not shown.
26 changes: 14 additions & 12 deletions app/src/main/java/com/arjun/weather/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.os.Bundle;
import android.os.Looper;
import android.provider.Settings;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand Down Expand Up @@ -185,8 +186,6 @@ public void onResponse(Call<FiveDaysWeather> call, Response<FiveDaysWeather> res
}
assert response.body() != null;
fiveDaysWeather = response.body();
editor.putString("location", fiveDaysWeather.getCity().getName());
editor.commit();
setDataFromAPI();
}

Expand Down Expand Up @@ -237,10 +236,9 @@ public void onClick(View view) {

@SuppressLint("SetTextI18n")
private void setDataFromAPI() {
precipitation.setVisibility(View.VISIBLE);
changeShimmerEffect(false);
changeShimmerEffect(false);
divide.convertData(fiveDaysWeather);
menu.getItem(0).getIcon().setTint(Color.parseColor("#000000"));
Objects.requireNonNull(getSupportActionBar()).setTitle(fiveDaysWeather.getCity().getName() + ", " + fiveDaysWeather.getCity().getCountry());
weatherCondition.setText(fiveDaysWeather.getList().get(0).getWeather().get(0).getDescription());
wind.setText((fiveDaysWeather.getList().get(0).getWind().getSpeed()) + " km/hr");
Expand All @@ -258,6 +256,9 @@ private void setDataFromAPI() {
}

private void bottom_data() {
if(itemHourlyArrayList.size()!=0){
itemHourlyArrayList.clear();
}
itemHourlyArrayList.add(divide.getDayOne().get(0));
itemHourlyArrayList.add(divide.getDayTwo().get(3));
itemHourlyArrayList.add(divide.getDayThree().get(3));
Expand All @@ -272,7 +273,7 @@ private void buildRetrofit(String baseUrl, String userSearch, String lat, String
.build();
weather = retrofit.create(Weather.class);
//API key will go down here
String appid = "API_KEY";
String appid = new API().sendAPIKEY();
if (lat == null && lon == null) {
getAllData(null, null, userSearch, appid);
} else {
Expand All @@ -282,11 +283,11 @@ private void buildRetrofit(String baseUrl, String userSearch, String lat, String


@Override
public void sendUserLocation(String location, boolean toSave) {
public void sendUserLocation(String location, Boolean toSave) {
if (toSave) {
//Store user Preference here
editor.putString("location", location);
editor.commit();
editor.apply();
}
changeShimmerEffect(true);
buildRetrofit(baseUrl, location, null, null);
Expand Down Expand Up @@ -315,8 +316,7 @@ private boolean isLocationEnabled() {
}

@Override
public void
onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == perm_id) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Expand All @@ -325,7 +325,6 @@ private boolean isLocationEnabled() {
}
}


@SuppressLint("MissingPermission")
private void requestNewLocationData() {
LocationRequest mLocationRequest = new LocationRequest();
Expand All @@ -338,10 +337,13 @@ private void requestNewLocationData() {
providerClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
}


@Override
public void setPosition(int position) {
ArrayList<ItemHourly> itemHourlies;
ArrayList<ItemHourly> itemHourlies = new ArrayList<>();
if(itemHourlies.size()!=0){
itemHourlies.clear();
Log.e("itemsHourlies", "itemsHourlies size: "+itemHourlies.size());
}
if (position == 0) {
itemHourlies = divide.getDayOne();
} else if (position == 1) {
Expand Down
33 changes: 22 additions & 11 deletions app/src/main/java/com/arjun/weather/RecyclerView/MainAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -26,13 +27,20 @@
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {

public boolean isShimmer = true;
ArrayList<ItemHourly> fiveDaysWeather;
Context context;
int shimmerNumber = 5;
setDataActivity setDataActivity;
private ArrayList<ItemHourly> fiveDaysWeather = new ArrayList<>();
private List<Integer> colors = new ArrayList<>();

private Context context;
private int shimmerNumber = 5;
private setDataActivity setDataActivity;

public MainAdapter(ArrayList<ItemHourly> itemHourlyList, Context context, setDataActivity dataActivity) {
this.fiveDaysWeather = itemHourlyList;
if(fiveDaysWeather.size()!=0){
fiveDaysWeather.clear();
fiveDaysWeather.addAll(itemHourlyList);
}else {
this.fiveDaysWeather = itemHourlyList;
}
this.setDataActivity = dataActivity;
this.context = context;

Expand All @@ -49,12 +57,7 @@ public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
String[] colorsTxt = context.getApplicationContext().getResources().getStringArray(R.array.colors);
List<Integer> colors = new ArrayList<Integer>();
for (String s : colorsTxt) {
int newColor = Color.parseColor(s);
colors.add(newColor);
}

if (isShimmer) {
holder.cardView.setCardBackgroundColor(null);
holder.shimmerFrameLayout.startShimmerAnimation();
Expand All @@ -64,6 +67,14 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
.getWeather().get(0)
.getIcon() + "@2x.png")
.into(holder.icon1);
String[] colorsTxt = context.getApplicationContext().getResources().getStringArray(R.array.colors);
Log.e("Color", "onBindViewHolder: "+colorsTxt.length);
if(colors.size()!=0){ colors.clear(); }
for (String s : colorsTxt) {
int newColor = Color.parseColor(s);
colors.add(newColor);
}
Log.e("ArrayList", "onBindViewHolder: "+colors.size());
holder.shimmerFrameLayout.stopShimmerAnimation();
holder.nomtemp.setText(String.format("%s°C", (fiveDaysWeather.get(position).getMain().getTemp())));
holder.cardView.setCardBackgroundColor(colors.get(position));
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/com/arjun/weather/utils/DialogBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.SearchView;

Expand All @@ -27,8 +28,9 @@
public class DialogBox extends DialogFragment {
private EditText searchView;
private CheckBox checkBox;
private boolean isChecked = false;
public interface sendLocation{
void sendUserLocation(String location,boolean toSave);
void sendUserLocation(String location,Boolean toSave);
}

sendLocation sendLocation;
Expand All @@ -40,11 +42,19 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
checkBox = view.findViewById(R.id.checkBox);
Button ok = view.findViewById(R.id.ok);
Button cancel = view.findViewById(R.id.cancel);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Log.e("TAG", "sendUserLocation: "+b +"This is happened" );
isChecked= b;
}
});
ok.setOnClickListener(view12 -> {
String location = searchView.getText().toString();
if(!location.isEmpty()){
//Send Data to main Activity using Interface
sendLocation.sendUserLocation(location, checkBox.isChecked());

sendLocation.sendUserLocation(location, isChecked);
Objects.requireNonNull(getDialog()).dismiss();
}
else{
Expand Down
52 changes: 25 additions & 27 deletions app/src/main/java/com/arjun/weather/utils/Divide.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.arjun.weather.utils;

import android.annotation.SuppressLint;
import android.util.Log;

import com.arjun.weather.Model.FiveDaysWeather;
import com.arjun.weather.Model.ItemHourly;
Expand All @@ -12,39 +11,37 @@
import java.util.Date;

public class Divide {
private ArrayList<ItemHourly> dayOne =new ArrayList<>();
private ArrayList<ItemHourly> dayTwo =new ArrayList<>();
private ArrayList<ItemHourly> dayThree =new ArrayList<>();
private ArrayList<ItemHourly> dayFour =new ArrayList<>();
private ArrayList<ItemHourly> dayFive =new ArrayList<>();
private ArrayList<ItemHourly> dayOne = new ArrayList<>();
private ArrayList<ItemHourly> dayTwo = new ArrayList<>();
private ArrayList<ItemHourly> dayThree = new ArrayList<>();
private ArrayList<ItemHourly> dayFour = new ArrayList<>();
private ArrayList<ItemHourly> dayFive = new ArrayList<>();



public void convertData(FiveDaysWeather daysWeather){
if(dayOne.size()!=0){
dayOne.clear();dayTwo.clear();dayThree.clear();dayFour.clear();
}
int day= getDay(daysWeather.getList().get(0).getDtTxt());
for (ItemHourly hourly:daysWeather.getList()) {
if(getDay(hourly.getDtTxt())==day){
public void convertData(FiveDaysWeather daysWeather) {
if (dayOne.size()!= 0) {
dayOne.clear();
dayTwo.clear();
dayThree.clear();
dayFour.clear();
dayFive.clear();
}
int day = getDay(daysWeather.getList().get(0).getDtTxt());
for (ItemHourly hourly : daysWeather.getList()) {
if (getDay(hourly.getDtTxt()) == day) {
dayOne.add(hourly);
Log.e("Day_One", "convertData: "+dayOne.size() );
}
else if(getDay(hourly.getDtTxt())==day+1){
} else if (getDay(hourly.getDtTxt()) == day + 1) {
dayTwo.add(hourly);
}
else if(getDay(hourly.getDtTxt())==day+2){
} else if (getDay(hourly.getDtTxt()) == day + 2) {
dayThree.add(hourly);
}
else if(getDay(hourly.getDtTxt())==day+3){
} else if (getDay(hourly.getDtTxt()) == day + 3) {
dayFour.add(hourly);
}
else if(getDay(hourly.getDtTxt())==day+4) {
} else if (getDay(hourly.getDtTxt()) == day + 4) {
dayFive.add(hourly);
}
}

}
}

public ArrayList<ItemHourly> getDayOne() {
return dayOne;
Expand All @@ -65,12 +62,13 @@ public ArrayList<ItemHourly> getDayFOur() {
public ArrayList<ItemHourly> getDayFive() {
return dayFive;
}

@SuppressLint("SimpleDateFormat")
private int getDay(String date){
private int getDay(String date) {
@SuppressLint("SimpleDateFormat") SimpleDateFormat format = new SimpleDateFormat("dd");
@SuppressLint("SimpleDateFormat") Date date1= null;
@SuppressLint("SimpleDateFormat") Date date1 = null;
try {
date1 = new SimpleDateFormat("yyyy-MM-dd").parse(date.substring(0,10));
date1 = new SimpleDateFormat("yyyy-MM-dd").parse(date.substring(0, 10));
} catch (ParseException e) {
e.printStackTrace();
}
Expand Down
21 changes: 12 additions & 9 deletions app/src/main/res/layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
android:id="@+id/pConstrain"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:background="@drawable/mainborder"
app:layout_constraintBottom_toTopOf="@+id/rv"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.659">
app:layout_constraintVertical_bias="0.0">


<TextView
Expand Down Expand Up @@ -73,8 +72,9 @@
android:id="@+id/pre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginStart="56dp"
android:fontFamily="@font/montserrat"
android:text="300C"
android:textAlignment="viewEnd"
android:textColor="#EEEEEE"
android:textSize="20sp"
Expand All @@ -98,14 +98,15 @@
android:id="@+id/humData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:fontFamily="@font/montserrat"
android:textAlignment="viewEnd"
android:textColor="#EEEEEE"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="@+id/pre"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/humidity"
app:layout_constraintTop_toBottomOf="@+id/pre" />
app:layout_constraintStart_toStartOf="@+id/pre"
app:layout_constraintTop_toBottomOf="@+id/pre"
tools:text="30km/hr" />

<TextView
android:id="@+id/wind"
Expand All @@ -124,13 +125,15 @@
android:id="@+id/windData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
android:layout_marginTop="4dp"
android:fontFamily="@font/montserrat"
android:textAlignment="viewEnd"
android:textColor="#EEEEEE"
android:textSize="20sp"
app:layout_constraintStart_toEndOf="@+id/wind"
app:layout_constraintTop_toBottomOf="@+id/humData" />
app:layout_constraintEnd_toEndOf="@+id/humData"
app:layout_constraintStart_toStartOf="@+id/humData"
app:layout_constraintTop_toBottomOf="@+id/humData"
tools:text="34km/hr" />

<TextView
android:id="@+id/bigTemp"
Expand Down

0 comments on commit 1f31f9b

Please sign in to comment.