Skip to content

Commit

Permalink
fix compile issue. Added to ProcessModelTransaction a listener for pr…
Browse files Browse the repository at this point in the history
…ogress

updates
  • Loading branch information
Andrew Grosner committed Jul 2, 2015
1 parent 869b0cb commit d784660
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class Queen extends BaseModel {
saveForeignKeyModel = false)
Colony colony;

private List<Ant> ants;
List<Ant> ants;

@OneToMany(methods = {OneToMany.Method.ALL}, variableName = "ants")
public List<Ant> getMyAnts() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.raizlabs.android.dbflow.runtime.transaction.process;

import com.raizlabs.android.dbflow.runtime.transaction.BaseResultTransaction;
import com.raizlabs.android.dbflow.runtime.FlowContentObserver;
import com.raizlabs.android.dbflow.runtime.transaction.BaseResultTransaction;
import com.raizlabs.android.dbflow.structure.Model;

import java.util.List;
Expand All @@ -13,10 +13,28 @@
public abstract class ProcessModelTransaction<ModelClass extends Model> extends BaseResultTransaction<List<ModelClass>>
implements ProcessModel<ModelClass> {

/**
* Called during execution of the {@link #processModel(Model)} method. Gives an idea of progress.
*/
public interface OnProcessProgressChangeListener<ModelClass> {

/**
* @param current The current index of execution.
* @param maxProgress The maximum progress (total count of models)
* @param modifiedModel The model that was modified.
*/
void onProcessProgressChange(long current, long maxProgress, ModelClass modifiedModel);
}

protected ProcessModelInfo<ModelClass> processModelInfo;

private final FlowContentObserver contentObserver;

private long count = 0;
private final long totalCount;

private OnProcessProgressChangeListener<ModelClass> changeListener;

/**
* Constructs this transaction with a single model enabled.
*
Expand All @@ -27,6 +45,18 @@ public ProcessModelTransaction(ProcessModelInfo<ModelClass> modelInfo, FlowConte
super(modelInfo.getInfo(), modelInfo.transactionListener);
processModelInfo = modelInfo;
this.contentObserver = contentObserver;

totalCount = processModelInfo.models.size();
}

/**
* Registers a listener to get callbacks during the {@link #processModel(Model)} operation when this
* transaction is executed.
*
* @param changeListener The listener to call.
*/
public void setChangeListener(OnProcessProgressChangeListener<ModelClass> changeListener) {
this.changeListener = changeListener;
}

@Override
Expand All @@ -40,7 +70,17 @@ public List<ModelClass> onExecute() {
if (contentObserver != null) {
contentObserver.beginTransaction();
}
processModelInfo.processModels(this);
processModelInfo.processModels(new ProcessModel<ModelClass>() {
@Override
public void processModel(ModelClass model) {
ProcessModelTransaction.this.processModel(model);
count++;

if (changeListener != null) {
changeListener.onProcessProgressChange(count, totalCount, model);
}
}
});
List<ModelClass> models = processModelInfo.models;
if (contentObserver != null) {
contentObserver.endTransactionAndNotify();
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ for easier access without needing to call `where()` first.
7. Adds a `enableSelfRefreshes()` for the `FlowQueryList` and souped up the documentation
with a "best practices" section.
8. Fixes bugs with the [Getting Started](https://github.com/Raizlabs/DBFlow/blob/master/usage/GettingStarted.md) section implementation. `OneToMany.Method.SAVE` now actually works on `insert`, `update`, and `save` methods.
9. Fixes an issue where using a autoincrementing primary key with `length()` causes a strange exception. Now we
ignore columns with length specified on autoincrementing primary keys.
9. Adds a `OnProgressProcessChangeListener` to listen for the total progress while
looping through saving models in a `ProcessModelTransaction`.


#### 2.1.0
Expand Down

0 comments on commit d784660

Please sign in to comment.