Skip to content

Commit

Permalink
Fix TransactionTooLargeException by moving data to LoadingViewModel
Browse files Browse the repository at this point in the history
Add HashMap to LoadingViewModel to store data,
Add interfaces DataProvider and ChatData for accessing stored data,
Reduce Bundle size for TimeGraphFragment:
only put a String key for the Map in it instead of the data,
Remove Parcelable implementation for GraphData (not needed anymore)

Prevent NullPointerException in GraphView if loadingView is null
  • Loading branch information
JtheDroid committed Apr 14, 2019
1 parent 86b8921 commit e6e56b3
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 56 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "de.jthedroid.whatsappchatanalyzer"
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "0.01"
versionCode 2
versionName "0.02"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package de.jthedroid.whatsappchatanalyzer;

interface DataProvider {
ChatData getData(String key);

void putData(String key, ChatData chatData);
}

interface ChatData {
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,10 @@
package de.jthedroid.whatsappchatanalyzer;

import android.os.Parcel;
import android.os.Parcelable;

public class GraphData implements Parcelable { //TODO: more data
public static final Parcelable.Creator<GraphData> CREATOR
= new Parcelable.Creator<GraphData>() {
public GraphData createFromParcel(Parcel in) {
return new GraphData(in);
}

public GraphData[] newArray(int size) {
return new GraphData[size];
}
};
class GraphData implements ChatData {
private float[] rawXData, rawYData;
private float[] xData, yData;
private String[] xDesc, yDesc;


private GraphData(Parcel parcel) {
rawXData = parcel.createFloatArray();
rawYData = parcel.createFloatArray();
xData = parcel.createFloatArray();
yData = parcel.createFloatArray();
xDesc = parcel.createStringArray();
yDesc = parcel.createStringArray();
}

GraphData(float[] rawXData, float[] rawYData, String[] xDesc, String[] yDesc) {
this.rawXData = rawXData;
this.rawYData = rawYData;
Expand All @@ -36,22 +13,6 @@ private GraphData(Parcel parcel) {
scale();
}

//Parcelable
@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeFloatArray(rawXData);
parcel.writeFloatArray(rawYData);
parcel.writeFloatArray(xData);
parcel.writeFloatArray(yData);
parcel.writeStringArray(xDesc);
parcel.writeStringArray(yDesc);
}

/**
* Scales x- and y-values to range [0,1]
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected void onDraw(Canvas canvas) {
}

private void setLoadingVisible(boolean show) {
loadingView.setVisibility(show ? VISIBLE : GONE);
if (loadingView != null) loadingView.setVisibility(show ? VISIBLE : GONE);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@
import android.content.ContentResolver;
import android.net.Uri;

import java.util.HashMap;

@SuppressWarnings("WeakerAccess")
public class LoadingViewModel extends ViewModel {
static final int OPENING_FILE = 0, LOADING_FILE = 1, PROCESSING = 2, DONE = 3, ERROR = -1;
final MutableLiveData<Chat> chat;
final MutableLiveData<String> title;
final MutableLiveData<Integer> loadingStage;
final MutableLiveData<HashMap<String, ChatData>> dataMap;

public LoadingViewModel() {
chat = new MutableLiveData<>();
title = new MutableLiveData<>();
loadingStage = new MutableLiveData<>();
dataMap = new MutableLiveData<>();
}

void load(ContentResolver contentResolver, Uri uri) {
if (uri != null) {
ChatLoadingThread clt = new ChatLoadingThread(contentResolver, uri, this);
clt.start();
}
} else loadingStage.setValue(ERROR);
}

void setChat(Chat c) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,29 @@
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.Objects;

public class ShareActivity extends ThemeMenuActivity {
public class ShareActivity extends ThemeMenuActivity implements DataProvider {
private LoadingViewModel viewModel;

@Override
protected void onCreate(Bundle savedInstanceState) {
viewModel = android.arch.lifecycle.ViewModelProviders.of(this).get(LoadingViewModel.class);
//create dataMap if not existent
if (viewModel.dataMap.getValue() == null)
viewModel.dataMap.setValue(new HashMap<String, ChatData>());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share);
findViewById(R.id.progressBarLoading).setVisibility(View.VISIBLE);
findViewById(R.id.textViewLoading).setVisibility(View.VISIBLE);
final LoadingViewModel viewModel = android.arch.lifecycle.ViewModelProviders.of(this).get(LoadingViewModel.class);
final Observer<Chat> chatObserver = new Observer<Chat>() {
private FragmentTransaction transaction;

Expand All @@ -38,7 +45,9 @@ public void onChanged(@Nullable Chat c) {
}
tag = "graphView1";
if (fragmentIsNew(tag)) {
TimeGraphFragment tgf = TimeGraphFragment.newInstance(c.getTotalMessagesGraph());
String key = tag + "_data";
putData(key, c.getTotalMessagesGraph());
TimeGraphFragment tgf = TimeGraphFragment.newInstance(key);
addFragment(tgf, tag);
}
tag = "headingGraph2";
Expand All @@ -48,7 +57,9 @@ public void onChanged(@Nullable Chat c) {
}
tag = "graphView2";
if (fragmentIsNew(tag)) {
TimeGraphFragment tgf = TimeGraphFragment.newInstance(c.getMessagesPerDayGraph());
String key = tag + "_data";
putData(key, c.getMessagesPerDayGraph());
TimeGraphFragment tgf = TimeGraphFragment.newInstance(key);
addFragment(tgf, tag);
}
tag = "headingSender";
Expand Down Expand Up @@ -118,7 +129,7 @@ public void onChanged(@Nullable Integer integer) {
}
setTitle(title);
viewModel.title.setValue(title);
//start loading chat .txt file
//read uri from intent
Uri uri = null;
ArrayList<Parcelable> extraList = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (extraList != null && !extraList.isEmpty()) {
Expand All @@ -127,9 +138,10 @@ public void onChanged(@Nullable Integer integer) {
uri = (Uri) p;
}
}
if (uri == null) {
if (uri == null)
Toast.makeText(this, R.string.toast_faulty_data, Toast.LENGTH_LONG).show();
}
else Log.d("ShareActivity", "Uri: " + uri.toString());
//start loading the text file
viewModel.load(getContentResolver(), uri);
}
} else {
Expand All @@ -142,4 +154,14 @@ public void onChanged(@Nullable Integer integer) {
viewModel.chat.observe(this, chatObserver);
viewModel.loadingStage.observe(this, loadingStageObserver);
}

@Override
public ChatData getData(String key) {
return Objects.requireNonNull(viewModel.dataMap.getValue()).get(key);
}

@Override
public void putData(String key, ChatData chatData) {
Objects.requireNonNull(viewModel.dataMap.getValue()).put(key, chatData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -15,20 +16,24 @@ public class TimeGraphFragment extends Fragment {
public TimeGraphFragment() {
}

public static TimeGraphFragment newInstance(GraphData graphData) {
public static TimeGraphFragment newInstance(String graphDataKey) {
TimeGraphFragment fragment = new TimeGraphFragment();
Bundle args = new Bundle();
args.putParcelable(GRAPH_DATA, graphData);
args.putString(GRAPH_DATA, graphDataKey);

fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
graphData = getArguments().getParcelable(GRAPH_DATA);
}
DataProvider dataProvider = (DataProvider) getActivity();
if (getArguments() != null && dataProvider != null) {
String key = getArguments().getString(GRAPH_DATA);
graphData = (GraphData) dataProvider.getData(key);
} else
Log.e("TimeGraphFragment", "Error creating Fragment: " + (getArguments() == null ? "getArguments()==null" : " ") + (dataProvider == null ? "dataProvider==null" : ""));
}

@Override
Expand Down

0 comments on commit e6e56b3

Please sign in to comment.