Search in sources :

Example 1 with WPJsonResponse

use of org.michenux.drodrolib.wordpress.json.WPJsonResponse in project YourAppIdea by Michenux.

the class TutorialSyncAdapter method retrievePosts.

private WPJsonPost retrievePosts(long lastSync, ContentProviderClient provider) throws InterruptedException, ExecutionException, ParseException, RemoteException, OperationApplicationException {
    if (BuildConfig.DEBUG) {
        Log.d(YourApplication.LOG_TAG, "tutorialSyncAdapter.retrievePosts()");
    }
    WordpressService wordpressService = WordpressServiceFactory.create(getContext());
    Observable<WPJsonResponse> observable = wordpressService.query("get_recent_posts", "android", "android_desc", "android", 9999);
    WPJsonResponse response = observable.toBlocking().first();
    WPJsonPost newPost = null;
    if (response.getStatus().equals(WPJsonResponse.STATUS_OK) && response.getPosts() != null && !response.getPosts().isEmpty()) {
        final WPJsonPost lastPost = response.getPosts().get(0);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.FRENCH);
        sdf.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));
        Date lastPostDate = sdf.parse(lastPost.getDate());
        Date lastSyncDate = new Date(lastSync);
        if (BuildConfig.DEBUG) {
            Log.d(YourApplication.LOG_TAG, "title:" + lastPost.getTitle());
            Log.d(YourApplication.LOG_TAG, "date: " + lastPost.getDate());
            Log.d(YourApplication.LOG_TAG, "lastSync: " + sdf.format(lastSync));
        }
        if (lastPostDate.after(lastSyncDate)) {
            newPost = lastPost;
        }
        updateDatabase(response.getPosts(), provider);
    //if (ConnectivityUtils.isConnectedWifi(TutorialSyncAdapter.this.getContext()) &&
    //        BatteryUtils.isChargingOrFull(TutorialSyncAdapter.this.getContext())) {
    //load image
    //}
    } else if (BuildConfig.DEBUG) {
        Log.d(YourApplication.LOG_TAG, "result is null or empty");
    }
    return newPost;
}
Also used : WPJsonPost(org.michenux.drodrolib.wordpress.json.WPJsonPost) WPJsonResponse(org.michenux.drodrolib.wordpress.json.WPJsonResponse) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 2 with WPJsonResponse

use of org.michenux.drodrolib.wordpress.json.WPJsonResponse in project YourAppIdea by Michenux.

the class WordpressServiceTest method testQuery.

@Test
public void testQuery() throws Exception {
    WordpressService wordpressService = WordpressServiceFactory.create(mContext);
    Observable<WPJsonResponse> observable = wordpressService.query("get_recent_posts", "android", "android_desc", "android", 9999);
    TestSubscriber<WPJsonResponse> testSubscriber = new TestSubscriber<>();
    observable.subscribe(testSubscriber);
    testSubscriber.assertNoErrors();
    List<WPJsonResponse> jsonPosts = testSubscriber.getOnNextEvents();
    Assert.assertNotNull(jsonPosts);
    Assert.assertFalse(jsonPosts.isEmpty());
    WPJsonResponse response = jsonPosts.get(0);
    Assert.assertEquals(response.getStatus(), "ok");
    List<WPJsonPost> posts = response.getPosts();
    Assert.assertNotNull(posts);
    Assert.assertFalse(posts.isEmpty());
}
Also used : WPJsonPost(org.michenux.drodrolib.wordpress.json.WPJsonPost) TestSubscriber(rx.observers.TestSubscriber) WPJsonResponse(org.michenux.drodrolib.wordpress.json.WPJsonResponse) WordpressService(org.michenux.yourappidea.tutorial.sync.WordpressService) Test(org.junit.Test)

Aggregations

WPJsonPost (org.michenux.drodrolib.wordpress.json.WPJsonPost)2 WPJsonResponse (org.michenux.drodrolib.wordpress.json.WPJsonResponse)2 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Test (org.junit.Test)1 WordpressService (org.michenux.yourappidea.tutorial.sync.WordpressService)1 TestSubscriber (rx.observers.TestSubscriber)1