@ -3,6 +3,7 @@ package org.tasks.ui;
import static com.todoroo.andlib.utility.AndroidUtilities.assertMainThread ;
import static com.todoroo.andlib.utility.AndroidUtilities.assertMainThread ;
import static com.todoroo.andlib.utility.AndroidUtilities.assertNotMainThread ;
import static com.todoroo.andlib.utility.AndroidUtilities.assertNotMainThread ;
import static com.todoroo.andlib.utility.DateUtilities.now ;
import static com.todoroo.andlib.utility.DateUtilities.now ;
import static io.reactivex.Single.fromCallable ;
import static org.tasks.data.TaskListQuery.getQuery ;
import static org.tasks.data.TaskListQuery.getQuery ;
import androidx.annotation.NonNull ;
import androidx.annotation.NonNull ;
@ -26,6 +27,7 @@ import java.util.Collections;
import java.util.List ;
import java.util.List ;
import javax.inject.Inject ;
import javax.inject.Inject ;
import org.tasks.BuildConfig ;
import org.tasks.BuildConfig ;
import org.tasks.data.SubtaskInfo ;
import org.tasks.data.TaskContainer ;
import org.tasks.data.TaskContainer ;
import org.tasks.preferences.Preferences ;
import org.tasks.preferences.Preferences ;
import timber.log.Timber ;
import timber.log.Timber ;
@ -40,25 +42,18 @@ public class TaskListViewModel extends ViewModel implements Observer<PagedList<T
@Inject TaskDao taskDao ;
@Inject TaskDao taskDao ;
private MutableLiveData < List < TaskContainer > > tasks = new MutableLiveData < > ( ) ;
private MutableLiveData < List < TaskContainer > > tasks = new MutableLiveData < > ( ) ;
private Filter filter ;
private Filter filter ;
private boolean manualSort ;
private boolean manualSort Filter ;
private final CompositeDisposable disposable = new CompositeDisposable ( ) ;
private final CompositeDisposable disposable = new CompositeDisposable ( ) ;
private LiveData < PagedList < TaskContainer > > internal ;
private LiveData < PagedList < TaskContainer > > internal ;
public void setFilter ( @NonNull Filter filter ) {
public void setFilter ( @NonNull Filter filter ) {
boolean manualSort =
preferences . showSubtasks ( ) | | ( filter . supportsManualSort ( ) & & preferences . isManualSort ( ) ) ;
setFilter ( filter , manualSort ) ;
}
public void setFilter ( @NonNull Filter filter , boolean manualSort ) {
if ( ! filter . equals ( this . filter )
if ( ! filter . equals ( this . filter )
| | ! filter . getSqlQuery ( ) . equals ( this . filter . getSqlQuery ( ) )
| | ! filter . getSqlQuery ( ) . equals ( this . filter . getSqlQuery ( ) ) ) {
| | this . manualSort ! = manualSort ) {
this . filter = filter ;
this . filter = filter ;
this . manualSort = manualSort ;
tasks = new MutableLiveData < > ( ) ;
tasks = new MutableLiveData < > ( ) ;
invalidate ( ) ;
invalidate ( ) ;
}
}
manualSortFilter = filter . supportsManualSort ( ) & & preferences . isManualSort ( ) ;
}
}
public void observe ( LifecycleOwner owner , Observer < List < TaskContainer > > observer ) {
public void observe ( LifecycleOwner owner , Observer < List < TaskContainer > > observer ) {
@ -85,53 +80,61 @@ public class TaskListViewModel extends ViewModel implements Observer<PagedList<T
return ;
return ;
}
}
if ( manualSort ) {
disposable . add (
disposable . add (
Single . fromCallable ( taskDao : : getSubtaskInfo )
Single . fromCallable (
. subscribeOn ( Schedulers . io ( ) )
( ) - >
. observeOn ( AndroidSchedulers . mainThread ( ) )
taskDao . fetchTasks (
. subscribe (
( ( includeGoogleSubtasks , includeCaldavSubtasks ) - >
subtasks - > {
getQuery (
if ( manualSortFilter | | ( subtasks . usesSubtasks ( ) & & preferences . showSubtasks ( ) ) ) {
preferences ,
performNonPagedQuery ( subtasks ) ;
filter ,
} else {
includeGoogleSubtasks ,
performPagedListQuery ( ) ;
includeCaldavSubtasks ) ) ) )
}
. subscribeOn ( Schedulers . io ( ) )
} ,
. observeOn ( AndroidSchedulers . mainThread ( ) )
Timber : : e ) ) ;
. subscribe ( tasks : : postValue , Timber : : e ) ) ;
}
} else {
List < String > queries = getQuery ( preferences , filter , false , false ) ;
private void performNonPagedQuery ( SubtaskInfo subtasks ) {
if ( BuildConfig . DEBUG & & queries . size ( ) ! = 1 ) {
disposable . add (
throw new RuntimeException ( "Invalid queries" ) ;
fromCallable ( ( ) - > taskDao . fetchTasks ( s - > getQuery ( preferences , filter , s ) , subtasks ) )
}
. subscribeOn ( Schedulers . io ( ) )
SimpleSQLiteQuery query = new SimpleSQLiteQuery ( queries . get ( 0 ) ) ;
. observeOn ( AndroidSchedulers . mainThread ( ) )
Timber . d ( "paged query: %s" , query . getSql ( ) ) ;
. subscribe ( tasks : : postValue , Timber : : e ) ) ;
Factory < Integer , TaskContainer > factory = taskDao . getTaskFactory ( query ) ;
}
LivePagedListBuilder < Integer , TaskContainer > builder =
new LivePagedListBuilder < > ( factory , PAGED_LIST_CONFIG ) ;
private void performPagedListQuery ( ) {
List < TaskContainer > current = tasks . getValue ( ) ;
List < String > queries = getQuery ( preferences , filter , new SubtaskInfo ( ) ) ;
if ( current instanceof PagedList ) {
if ( BuildConfig . DEBUG & & queries . size ( ) ! = 1 ) {
Object lastKey = ( ( PagedList < TaskContainer > ) current ) . getLastKey ( ) ;
throw new RuntimeException ( "Invalid queries" ) ;
if ( lastKey instanceof Integer ) {
}
builder . setInitialLoadKey ( ( Integer ) lastKey ) ;
SimpleSQLiteQuery query = new SimpleSQLiteQuery ( queries . get ( 0 ) ) ;
}
Timber . d ( "paged query: %s" , query . getSql ( ) ) ;
}
Factory < Integer , TaskContainer > factory = taskDao . getTaskFactory ( query ) ;
if ( BuildConfig . DEBUG ) {
LivePagedListBuilder < Integer , TaskContainer > builder =
builder . setFetchExecutor (
new LivePagedListBuilder < > ( factory , PAGED_LIST_CONFIG ) ;
command - >
List < TaskContainer > current = tasks . getValue ( ) ;
Completable . fromAction (
if ( current instanceof PagedList ) {
( ) - > {
Object lastKey = ( ( PagedList < TaskContainer > ) current ) . getLastKey ( ) ;
assertNotMainThread ( ) ;
if ( lastKey instanceof Integer ) {
long start = now ( ) ;
builder . setInitialLoadKey ( ( Integer ) lastKey ) ;
command . run ( ) ;
Timber . d ( "*** paged list execution took %sms" , now ( ) - start ) ;
} )
. subscribeOn ( Schedulers . io ( ) )
. subscribe ( ) ) ;
}
}
internal = builder . build ( ) ;
internal . observeForever ( this ) ;
}
}
if ( BuildConfig . DEBUG ) {
builder . setFetchExecutor (
command - >
Completable . fromAction (
( ) - > {
assertNotMainThread ( ) ;
long start = now ( ) ;
command . run ( ) ;
Timber . d ( "*** paged list execution took %sms" , now ( ) - start ) ;
} )
. subscribeOn ( Schedulers . io ( ) )
. subscribe ( ) ) ;
}
internal = builder . build ( ) ;
internal . observeForever ( this ) ;
}
}
@Override
@Override