Skip to content

Commit

Permalink
update statfrom server
Browse files Browse the repository at this point in the history
  • Loading branch information
Rufim committed Jan 6, 2018
1 parent 351bab3 commit a4790af
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
5 changes: 3 additions & 2 deletions Client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,10 @@ dependencies {
exclude group: 'com.android.support', module: 'support-v4'
}
compile "com.snappydb:snappydb-lib:0.5.2"
compile "com.evernote:android-job:1.1.11-SNAPSHOT"
compile "com.evernote:android-job:1.2.1"
compile "net.vrallev.android:cat:1.0.4"
compile 'com.google.android.gms:play-services-location:11.8.0'

compile "com.google.android.gms:play-services-gcm:11.8.0"

compile "com.github.Raizlabs.DBFlow:dbflow-core:$dbflowVersion"
compile "com.github.Raizlabs.DBFlow:dbflow:$dbflowVersion"
Expand Down
4 changes: 4 additions & 0 deletions Client/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@
android:exported="true"
android:enabled="true"/>

<service
android:name="com.evernote.android.job.gcm.PlatformGcmService"
tools:node="remove"/>

<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.MainActivity"/>
Expand Down
7 changes: 0 additions & 7 deletions Client/src/main/java/ru/samlib/client/App.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
package ru.samlib.client;

import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.database.sqlite.SQLiteDatabase;
import android.support.multidex.MultiDexApplication;
import com.evernote.android.job.JobManager;
import com.evernote.android.job.util.JobApi;
import com.raizlabs.android.dbflow.config.FlowConfig;
import com.raizlabs.android.dbflow.config.FlowLog;
import com.raizlabs.android.dbflow.config.FlowManager;
import com.raizlabs.android.dbflow.converter.BigDecimalConverter;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import dagger.Module;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public static class App {
public static class Pattern {
public static final String TIME_PATTERN = "HH:mm";
public static final String DATA_PATTERN = "dd-MM-yyyy";
public static final String DATA_PATTERN_LOG = "dd/MM/yyyy";
public static final String DATA_TIME_PATTERN = "dd-MM-yyyy HH:mm";
public static final String DATA_ISO_8601_24H = "yyyy-MM-dd HH:mm:ss";
public static final String DATA_PATTERN_DIFF = "dd.MM.yyyy";
Expand Down
21 changes: 13 additions & 8 deletions Client/src/main/java/ru/samlib/client/job/ObservableUpdateJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ public static void updateObservable(DatabaseService service, Context context) {
List<CharSequence> notifyAuthors = new ArrayList<>();
MergeFromRequery.merge(context, service);
boolean statServerReachable = false;
//if(HTTPExecutor.pingHost(Constants.Net.STAT_SERVER, 8080, 10000)) {
// statServerReachable = true;
//}
if(HTTPExecutor.pingHost(Constants.Net.STAT_SERVER, 80, 10000)) {
statServerReachable = true;
}
Calendar yesterday = Calendar.getInstance();
yesterday.add(Calendar.DAY_OF_YEAR, -1);
yesterday.set(Calendar.HOUR_OF_DAY, 23);
Expand All @@ -110,7 +110,10 @@ public static void updateObservable(DatabaseService service, Context context) {
if (statServerReachable && author.getLastUpdateDate() != null) {
//use server api
try {
checkUpdateDateOnStatServer(author);
if(checkUpdateDateOnStatServer(author)) {
author = parser.parse();
parsed = true;
}
} catch (Throwable ex) {
Cat.e(ex);
author = parser.parse();
Expand Down Expand Up @@ -186,22 +189,23 @@ public static void stop() {
public static void startSchedule() {
jobId = AppJobCreator.request(JobType.UPDATE_OBSERVABLE)
.setPeriodic(10800000)
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
.setRequiredNetworkType(JobRequest.NetworkType.UNMETERED)
.setUpdateCurrent(true)
.setRequirementsEnforced(true)
.build()
.schedule();
}

public static void start() {
AppJobCreator.request(JobType.UPDATE_OBSERVABLE)
.setExecutionWindow(1L, 2000L)
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
.setRequiredNetworkType(JobRequest.NetworkType.UNMETERED)
.build()
.schedule();
}


public static void checkUpdateDateOnStatServer(Author author) throws Exception {
public static boolean checkUpdateDateOnStatServer(Author author) throws Exception {
Request update = new Request(Constants.Net.STAT_SERVER_UPDATE);
update.addParam("link", author.getLink());
Response response = update.execute();
Expand All @@ -217,12 +221,13 @@ public static void checkUpdateDateOnStatServer(Author author) throws Exception {
lastUpdate.set(Calendar.MILLISECOND, 999);
}
if (lastUpdateDate.after(lastUpdate.getTime())) {
author.hasNewUpdates();
author.setLastUpdateDate(lastUpdateDate);
return true;
}
} else {
throw new Exception("Author not found!");
}
return false;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ApiParser {
private static final String TAG = "Parser";

private final static SimpleDateFormat dateTimeFormat = new SimpleDateFormat(Constants.Pattern.DATA_ISO_8601_24H);
private final static SimpleDateFormat dateFormat = new SimpleDateFormat(Constants.Pattern.DATA_PATTERN);
private final static SimpleDateFormat dateFormat = new SimpleDateFormat(Constants.Pattern.DATA_PATTERN_LOG);
private final static SimpleDateFormat dateFormatDiff = new SimpleDateFormat(Constants.Pattern.DATA_PATTERN_DIFF);

private final static Pattern title = Pattern.compile("\\s*<title>Журнал &quot;Самиздат&quot;: Статистика: (.+), (.+)</title>\\s*");
Expand Down

0 comments on commit a4790af

Please sign in to comment.