Skip to content

Commit

Permalink
Modifying Data Manager to handle multiple loads
Browse files Browse the repository at this point in the history
- Made storage component dependent on net component
- Updated main activity to use no manager system
  • Loading branch information
w9jds committed Mar 14, 2016
1 parent b82ef10 commit 7489d77
Show file tree
Hide file tree
Showing 16 changed files with 199 additions and 174 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

android {
compileSdkVersion 'android-N'
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "com.w9jds.marketbot"
minSdkVersion 21
minSdkVersion 'N'
minSdkVersion 23
versionCode 1
versionName "1.0"
}
Expand Down
5 changes: 2 additions & 3 deletions app/src/debug/java/com/w9jds/marketbot/DebugMarketBot.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.w9jds.marketbot;

import android.app.Application;

import com.facebook.stetho.Stetho;
import com.w9jds.marketbot.classes.MarketBot;

/**
* Created by w9jds on 3/13/2016.
*/
public class DebugMarketBot extends Application {
public class DebugMarketBot extends MarketBot {

@Override
public void onCreate() {
Expand Down
131 changes: 55 additions & 76 deletions app/src/main/java/com/w9jds/marketbot/activities/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.w9jds.marketbot.activities;

import android.support.v4.util.ArrayMap;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
Expand All @@ -18,14 +19,10 @@
import com.w9jds.eveapi.Models.MarketItemBase;
import com.w9jds.marketbot.R;
import com.w9jds.marketbot.adapters.MarketGroupsAdapter;
import com.w9jds.marketbot.classes.MarketBot;
import com.w9jds.marketbot.data.BaseDataManager;
import com.w9jds.marketbot.data.DataManager;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import butterknife.Bind;
import butterknife.ButterKnife;
Expand All @@ -47,7 +44,7 @@ public class MainActivity extends AppCompatActivity implements MarketGroupsAdapt
private MarketGroupsAdapter adapter;
private LinearLayoutManager layoutManager;

private ArrayList<? extends MarketItemBase> marketGroupList;
private ArrayMap history = new ArrayMap();
private MarketGroup currentParent;

@Override
Expand All @@ -56,9 +53,6 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
ButterKnife.bind(this);

((MarketBot)getApplication()).getStorageComponent().inject(this);


setSupportActionBar(toolbar);
actionBar = getSupportActionBar();

Expand All @@ -71,21 +65,17 @@ protected void onCreate(Bundle savedInstanceState) {
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());

// dataManager = new DataManager(this, getApplication()) {
// @Override
// public void onDataLoaded(List<? extends MarketItemBase> data) {
// if (marketGroupList == null) {
// marketGroupList = new ArrayList<>(data);
// }
//
// adapter.addAndResort(data);
// }
//
// @Override
// public void onDataLoaded(Object data) {
// // never fired
// }
// };
dataManager = new DataManager(getApplication()) {
@Override
public void onDataLoaded(List<? extends MarketItemBase> data) {
adapter.updateCollection(data);
}

@Override
public void onDataLoaded(Object data) {
// never fired
}
};

dataManager.registerCallback(this);

Expand All @@ -110,31 +100,13 @@ public boolean onOptionsItemSelected(MenuItem item) {
public void updateSelectedParentGroup(MarketGroup group) {
currentParent = group;

if (group.items.size() > 0) {
adapter.updateCollection(group.items.values());
}

if (currentParent != null) {
if (group.children.size() > 0) {
if (group.items.size() > 0) {
adapter.addAndResort(group.children.values());
}
else {
adapter.clear();
adapter.updateCollection(group.children.values());
}
} else {
if (group.items.size() < 1) {
adapter.clear();
}

dataManager.loadGroupTypes(currentParent.getTypesLocation());
}
}
history.put(group.getId(), group);

adapter.setToClear(true);
dataManager.loadMarketGroups(group.getId(), true);
dataManager.loadGroupTypes(group.getTypesLocation());

animateTitleChange();
// showToolbar();
recyclerView.smoothScrollToPosition(0);
}

Expand Down Expand Up @@ -198,12 +170,19 @@ public void onAnimationRepeat(Animation animation) {
public void onBackPressed() {
if (!dataManager.isDataLoading()) {
if (currentParent != null && currentParent.hasParent()) {
bfsForParent(currentParent.getParentGroupId());
adapter.updateCollection(currentParent.children.values());
} else if (currentParent != null) {
adapter.updateCollection(marketGroupList);
long parentId = currentParent.getParentGroupId();

currentParent = (MarketGroup)history.get(parentId);
history.remove(parentId);

dataManager.loadMarketGroups(parentId, true);
}
else if (currentParent != null) {
history.clear();
dataManager.loadMarketGroups(null, true);
currentParent = null;
} else {
}
else {
super.onBackPressed();
}

Expand All @@ -223,30 +202,30 @@ public void dataFinishedLoading() {
progressBar.setVisibility(View.GONE);
}

private void bfsForParent(long parentId) {
boolean parentFound = false;
Queue queue = new LinkedList();

for (MarketItemBase group : marketGroupList) {
if (parentFound) {
break;
}

queue.add(group);

while(!queue.isEmpty()) {
MarketGroup node = (MarketGroup)queue.remove();
if (node.getId() == parentId) {
currentParent = node;
parentFound = true;
queue.clear();
}
else {
for (MarketGroup child : node.children.values()) {
queue.add(child);
}
}
}
}
}
// private void bfsForParent(long parentId) {
// boolean parentFound = false;
// Queue queue = new LinkedList();
//
// for (MarketItemBase group : marketGroupList) {
// if (parentFound) {
// break;
// }
//
// queue.add(group);
//
// while(!queue.isEmpty()) {
// MarketGroup node = (MarketGroup)queue.remove();
// if (node.getId() == parentId) {
// currentParent = node;
// parentFound = true;
// queue.clear();
// }
// else {
// for (MarketGroup child : node.children.values()) {
// queue.add(child);
// }
// }
// }
// }
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ public final class MarketGroupsAdapter extends RecyclerView.Adapter<RecyclerView
private final LayoutInflater layoutInflater;
private final @Nullable DataLoadingSubject dataLoading;

private boolean toClear = false;
private onMarketGroupChanged groupChangedListener;
private List<? extends MarketItemBase> items = new ArrayList<>();
private ArrayList<MarketItemBase> items = new ArrayList<>();

public interface onMarketGroupChanged {
void updateSelectedParentGroup(MarketGroup group);
Expand Down Expand Up @@ -197,29 +198,37 @@ private void bindMarketType(final Type type, MarketTypeHolder holder) {
.into(holder.image);
}

public void addAndResort(Collection<? extends MarketItemBase> newItems) {
ArrayList<MarketItemBase> groups = new ArrayList<>(newItems);
groups.addAll(items);
int oldSize = items.size();
Collections.sort(groups, new Comparitor());

items = groups;

if (items.size() > 0) {
notifyItemRangeChanged(0, oldSize);
}

notifyItemRangeInserted(oldSize, items.size() - oldSize);
}
// public void addAndResort(Collection<? extends MarketItemBase> newItems) {
// ArrayList<MarketItemBase> groups = new ArrayList<>(newItems);
// groups.addAll(items);
// int oldSize = items.size();
// Collections.sort(groups, new Comparitor());
//
// items = groups;
//
// if (items.size() > 0) {
// notifyItemRangeChanged(0, oldSize);
// }
//
// notifyItemRangeInserted(oldSize, items.size() - oldSize);
// }

public void updateCollection(Collection<? extends MarketItemBase> newChildren) {
ArrayList<MarketItemBase> groups = new ArrayList<>(newChildren);
Collections.sort(groups, new Comparitor());

int newSize = groups.size();
int newSize;
int oldSize = items.size();

items = groups;
if (toClear) {
toClear = false;
newSize = groups.size();
items = groups;
}
else {
newSize = oldSize + groups.size();
items.addAll(groups);
}

if (newSize < oldSize) {
notifyItemRangeRemoved(newSize, oldSize - newSize);
Expand All @@ -241,6 +250,10 @@ public void clear() {
notifyItemRangeRemoved(0, oldSize);
}

public void setToClear(boolean clear) {
toClear = clear;
}

private class Comparitor implements Comparator<MarketItemBase> {
@Override
public int compare(MarketItemBase lhs, MarketItemBase rhs) {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/w9jds/marketbot/classes/MarketBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* Created by Jeremy Shore on 3/9/16.
*/
public final class MarketBot extends Application {
public class MarketBot extends Application {

private StorageComponent storageComponent;
private NetComponent netComponent;
Expand All @@ -29,6 +29,7 @@ public void onCreate() {
.build();

storageComponent = DaggerStorageComponent.builder()
.netComponent(netComponent)
.storageModule(new StorageModule(this))
.build();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.w9jds.marketbot.classes.components;

import android.app.Application;

import com.w9jds.eveapi.Client.Crest;
import com.w9jds.marketbot.activities.ItemActivity;
import com.w9jds.marketbot.activities.MainActivity;
import com.w9jds.marketbot.classes.modules.ApplicationModule;
Expand All @@ -8,16 +11,20 @@

import java.lang.annotation.Retention;

import javax.inject.Named;
import javax.inject.Scope;
import javax.inject.Singleton;
import dagger.Component;
import retrofit2.Retrofit;

/**
* Created by Jeremy Shore on 3/9/16.
*/

@Singleton
@Component(modules={ApplicationModule.class, NetModule.class})
public interface NetComponent {
void inject(MainActivity activity);
void inject(ItemActivity activity);

Application application();
Retrofit retrofit();

}
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package com.w9jds.marketbot.classes.components;

import com.w9jds.eveapi.Client.Crest;
import com.w9jds.marketbot.activities.MainActivity;
import com.w9jds.marketbot.classes.modules.ApplicationModule;
import com.w9jds.marketbot.classes.modules.StorageModule;
import com.w9jds.marketbot.data.DataManager;

import javax.inject.Singleton;

import dagger.Component;

/**
* Created by Jeremy Shore on 3/10/16.
*/
@Singleton
@Component(modules={StorageModule.class})
@StorageScope
@Component(dependencies = NetComponent.class, modules = StorageModule.class)
public interface StorageComponent {

void inject(DataManager manager);
void inject(Crest crest);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.w9jds.marketbot.classes.components;

import javax.inject.Scope;

/**
* Created by w9jds on 3/13/2016.
*/
@Scope
public @interface StorageScope {
}
Loading

0 comments on commit 7489d77

Please sign in to comment.