Use rxjava with feed synchronization
This commit is contained in:
parent
f7a0ef47fe
commit
ed9fb458c8
@ -43,6 +43,10 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
|
|
||||||
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||||
|
import io.reactivex.observers.DisposableCompletableObserver;
|
||||||
|
import io.reactivex.schedulers.Schedulers;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity implements SimpleCallback, SwipeRefreshLayout.OnRefreshListener {
|
public class MainActivity extends AppCompatActivity implements SimpleCallback, SwipeRefreshLayout.OnRefreshListener {
|
||||||
|
|
||||||
public static final String TAG = MainActivity.class.getSimpleName();
|
public static final String TAG = MainActivity.class.getSimpleName();
|
||||||
@ -214,7 +218,23 @@ public class MainActivity extends AppCompatActivity implements SimpleCallback, S
|
|||||||
@Override
|
@Override
|
||||||
public void onRefresh() {
|
public void onRefresh() {
|
||||||
Log.d(TAG, "syncing started");
|
Log.d(TAG, "syncing started");
|
||||||
viewModel.sync();
|
|
||||||
|
viewModel.sync()
|
||||||
|
.subscribeOn(Schedulers.io())
|
||||||
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
|
.subscribe(new DisposableCompletableObserver() {
|
||||||
|
@Override
|
||||||
|
public void onComplete() {
|
||||||
|
refreshLayout.setRefreshing(false);
|
||||||
|
adapter.submitList(newItems);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(Throwable e) {
|
||||||
|
refreshLayout.setRefreshing(false);
|
||||||
|
Toast.makeText(getApplication(), e.getMessage(), Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void displayAddFeedDialog(View view) {
|
public void displayAddFeedDialog(View view) {
|
||||||
|
@ -32,7 +32,7 @@ public abstract class ARepository {
|
|||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void sync();
|
public abstract Completable sync();
|
||||||
|
|
||||||
public abstract void addFeed(ParsingResult result);
|
public abstract void addFeed(ParsingResult result);
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.readrops.app.repositories;
|
package com.readrops.app.repositories;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
import android.app.Application;
|
import android.app.Application;
|
||||||
import android.arch.lifecycle.LiveData;
|
import android.arch.lifecycle.LiveData;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
@ -50,8 +51,8 @@ public class LocalFeedRepository extends ARepository implements QueryCallback {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sync() {
|
public Completable sync() {
|
||||||
executor.execute(() -> {
|
return Completable.create(emitter -> {
|
||||||
RSSNetwork rssNet = new RSSNetwork();
|
RSSNetwork rssNet = new RSSNetwork();
|
||||||
rssNet.setCallback(this);
|
rssNet.setCallback(this);
|
||||||
List<Feed> feedList = database.feedDao().getAllFeeds();
|
List<Feed> feedList = database.feedDao().getAllFeeds();
|
||||||
@ -66,11 +67,11 @@ public class LocalFeedRepository extends ARepository implements QueryCallback {
|
|||||||
|
|
||||||
rssNet.requestUrl(feed.getUrl(), headers);
|
rssNet.requestUrl(feed.getUrl(), headers);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
failureCallBackInMainThread(e);
|
emitter.onError(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
postCallBackSuccess();
|
emitter.onComplete();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,6 +12,8 @@ import com.readrops.readropslibrary.ParsingResult;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import io.reactivex.Completable;
|
||||||
|
|
||||||
public class MainViewModel extends AndroidViewModel {
|
public class MainViewModel extends AndroidViewModel {
|
||||||
|
|
||||||
private LiveData<List<ItemWithFeed>> itemsWithFeed;
|
private LiveData<List<ItemWithFeed>> itemsWithFeed;
|
||||||
@ -33,8 +35,8 @@ public class MainViewModel extends AndroidViewModel {
|
|||||||
repository.setCallback(simpleCallback);
|
repository.setCallback(simpleCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sync() {
|
public Completable sync() {
|
||||||
repository.sync();
|
return repository.sync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addFeed(ParsingResult parsingResult) {
|
public void addFeed(ParsingResult parsingResult) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user