Skip to content

Commit

Permalink
Did a quick fix for statistics not updating for Vehicle Mileage
Browse files Browse the repository at this point in the history
Done so by forcing stats to be synced with Firebase
Releasing minor build
  • Loading branch information
itachi1706 committed Sep 17, 2017
1 parent 550d318 commit 1e4e66f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 50 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ android {
applicationId "com.itachi1706.cheesecakeutilities"
minSdkVersion 16
targetSdkVersion 25
versionCode 464
versionName "3.4.2"
versionCode 465
versionName "3.4.3"
vectorDrawables.useSupportLibrary = true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
resValue "string", "tray__authority", "${applicationId}.tray"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -38,7 +39,6 @@ public class VehicleMileageGeneralStatsFragment extends Fragment {
SwipeRefreshLayout refreshLayout;
SharedPreferences sp;

private static boolean ready = false;
private ArrayMap<String, String> legend;


Expand Down Expand Up @@ -68,22 +68,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
recyclerView.setAdapter(adapter);

sp = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
FirebaseUtils.getFirebaseDatabase().getReference().child("stat-legend").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
legend = new ArrayMap<>();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
legend.put(ds.getKey(), ds.getValue(String.class));
}
ready = true;
updateStats();
}

@Override
public void onCancelled(DatabaseError databaseError) {
ready = false;
}
});
}
refreshLayout = v.findViewById(R.id.pull_to_refresh);
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
Expand All @@ -102,7 +86,24 @@ public void onRefresh() {
@Override
public void onResume() {
super.onResume();
if (ready) updateStats();
if (legend == null) {
refreshLayout.setRefreshing(true);
FirebaseUtils.getFirebaseDatabase().getReference().child("stat-legend").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
legend = new ArrayMap<>();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
legend.put(ds.getKey(), ds.getValue(String.class));
}
updateStats();
}

@Override
public void onCancelled(DatabaseError databaseError) {
Log.e("VehicleMileageStats", "Error in Firebase DB call (General-Legend): " + databaseError.getDetails());
}
});
} else updateStats();
}

public void updateStats() {
Expand All @@ -112,7 +113,7 @@ public void updateStats() {
Toast.makeText(getActivity(), "Invalid Login Token, please re-login", Toast.LENGTH_SHORT).show();
return;
}
refreshLayout.setRefreshing(true);
if (!refreshLayout.isRefreshing()) refreshLayout.setRefreshing(true);
FirebaseUtils.getFirebaseDatabase().getReference().child("users").child(user_id).child("statistics").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Expand All @@ -128,7 +129,7 @@ public void onDataChange(DataSnapshot dataSnapshot) {

@Override
public void onCancelled(DatabaseError databaseError) {

Log.e("VehicleMileageStats", "Error in Firebase DB call (General-Data): " + databaseError.getDetails());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -38,7 +39,6 @@ public class VehicleMileageVTypeStatsFragment extends Fragment {
SwipeRefreshLayout refreshLayout;
SharedPreferences sp;

private static boolean ready = false;
private ArrayMap<String, String> vehicles;


Expand Down Expand Up @@ -68,6 +68,26 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
recyclerView.setAdapter(adapter);

sp = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
}
refreshLayout = v.findViewById(R.id.pull_to_refresh);
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
updateStats();
}
});
refreshLayout.setColorSchemeResources(
R.color.refresh_progress_1,
R.color.refresh_progress_2);

return v;
}

@Override
public void onResume() {
super.onResume();
if (vehicles == null) {
refreshLayout.setRefreshing(true);
FirebaseUtils.getFirebaseDatabase().getReference().child("vehicles").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Expand All @@ -81,34 +101,15 @@ public void onDataChange(DataSnapshot dataSnapshot) {
}
}
}
ready = true;
updateStats();
}

@Override
public void onCancelled(DatabaseError databaseError) {
ready = false;
Log.e("VehicleMileageStats", "Error in Firebase DB call (VType-Legend): " + databaseError.getDetails());
}
});
}
refreshLayout = v.findViewById(R.id.pull_to_refresh);
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
updateStats();
}
});
refreshLayout.setColorSchemeResources(
R.color.refresh_progress_1,
R.color.refresh_progress_2);

return v;
}

@Override
public void onResume() {
super.onResume();
if (ready) updateStats();
} else updateStats();
}

public void updateStats() {
Expand All @@ -118,16 +119,12 @@ public void updateStats() {
Toast.makeText(getActivity(), "Invalid Login Token, please re-login", Toast.LENGTH_SHORT).show();
return;
}
refreshLayout.setRefreshing(true);
if (!refreshLayout.isRefreshing()) refreshLayout.setRefreshing(true);
FirebaseUtils.getFirebaseDatabase().getReference().child("users").child(user_id).child("statistics")
.child("vehicleTypes").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<DualLineString> stats = new ArrayList<>();
/*for (Map.Entry<String, String> i : vehicles.entrySet()) {
if (!dataSnapshot.hasChild(i.getKey())) continue;
stats.add(new DualLineString(i.getValue(), dataSnapshot.child(i.getKey()).getValue(Double.class) + " km"));
}*/
for (DataSnapshot ds : dataSnapshot.getChildren()) {
if (vehicles.containsKey(ds.getKey()))
stats.add(new DualLineString("Total Mileage with " + vehicles.get(ds.getKey()), ds.getValue(Double.class) + " km"));
Expand All @@ -139,7 +136,7 @@ public void onDataChange(DataSnapshot dataSnapshot) {

@Override
public void onCancelled(DatabaseError databaseError) {

Log.e("VehicleMileageStats", "Error in Firebase DB call (VType-Data): " + databaseError.getDetails());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.itachi1706.cheesecakeutilities.Modules.VehicleMileageTracker;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.google.firebase.database.DatabaseReference;
import com.itachi1706.cheesecakeutilities.Modules.VehicleMileageTracker.Fragments.VehicleMileageDateStatsFragment;
import com.itachi1706.cheesecakeutilities.Modules.VehicleMileageTracker.Fragments.VehicleMileageGeneralStatsFragment;
import com.itachi1706.cheesecakeutilities.Modules.VehicleMileageTracker.Fragments.VehicleMileageMonthStatsFragment;
Expand Down Expand Up @@ -38,6 +41,16 @@ protected void onCreate(Bundle savedInstanceState) {
tabLayout.setupWithViewPager(pager);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_FIXED);

// Keep Firebase synced
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String user_id = sp.getString("firebase_uid", "nien");
if (!user_id.equals("nien")) {
DatabaseReference dbRef = FirebaseUtils.getFirebaseDatabase().getReference();
dbRef.child("users").child(user_id).child("statistics").keepSynced(true);
dbRef.child("stat-legend").keepSynced(true);
dbRef.child("vehicles").keepSynced(true);
}
}

private void setupViewPager(ViewPager viewPager)
Expand Down

0 comments on commit 1e4e66f

Please sign in to comment.