Search in sources :

Example 6 with OpacClient

use of de.geeksfactory.opacclient.OpacClient in project opacclient by opacapp.

the class SyncAccountJob method syncAccounts.

boolean syncAccounts(OpacClient app, AccountDataSource data, SharedPreferences sp, ReminderHelper helper) {
    boolean failed = false;
    List<Account> accounts = data.getAccountsWithPassword();
    if (!sp.contains("update_151_clear_cache")) {
        data.invalidateCachedData();
        sp.edit().putBoolean("update_151_clear_cache", true).apply();
    }
    for (Account account : accounts) {
        if (BuildConfig.DEBUG) {
            Log.i(TAG, "Loading data for Account " + account.toString());
        }
        AccountData res;
        try {
            Library library = app.getLibrary(account.getLibrary());
            if (!library.isAccountSupported()) {
                data.deleteAccountData(account);
                continue;
            }
            OpacApi api = app.getNewApi(library);
            res = api.account(account);
            if (res == null) {
                failed = true;
                continue;
            }
        } catch (JSONException | IOException | OpacApi.OpacErrorException e) {
            e.printStackTrace();
            failed = true;
            continue;
        } catch (OpacClient.LibraryRemovedException e) {
            continue;
        }
        account.setPasswordKnownValid(true);
        try {
            data.update(account);
            data.storeCachedAccountData(account, res);
        } finally {
            helper.generateAlarms();
        }
    }
    return failed;
}
Also used : Account(de.geeksfactory.opacclient.objects.Account) OpacClient(de.geeksfactory.opacclient.OpacClient) AccountData(de.geeksfactory.opacclient.objects.AccountData) OpacApi(de.geeksfactory.opacclient.apis.OpacApi) JSONException(org.json.JSONException) Library(de.geeksfactory.opacclient.objects.Library) IOException(java.io.IOException)

Example 7 with OpacClient

use of de.geeksfactory.opacclient.OpacClient in project opacclient by opacapp.

the class SyncAccountJob method onRunJob.

@NonNull
@Override
protected Result onRunJob(Params params) {
    if (BuildConfig.DEBUG)
        Log.i(TAG, "SyncAccountJob started");
    if (getParams().getTag().equals(TAG_RETRY) && getParams().getFailureCount() >= 4) {
        // too many retries, give up and wait for the next regular scheduled run
        return Result.SUCCESS;
    }
    if (!getParams().getTag().equals(TAG_RETRY)) {
        updateLibraryConfig();
    }
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getContext());
    if (!sp.getBoolean(SyncAccountJobCreator.PREF_SYNC_SERVICE, false)) {
        if (BuildConfig.DEBUG)
            Log.i(TAG, "notifications are disabled");
        return Result.SUCCESS;
    }
    OpacClient app = getApp();
    AccountDataSource data = new AccountDataSource(getContext());
    ReminderHelper helper = new ReminderHelper(app);
    boolean failed = syncAccounts(app, data, sp, helper);
    if (BuildConfig.DEBUG) {
        Log.i(TAG, "SyncAccountJob finished " + (failed ? " with errors" : " " + "successfully"));
    }
    if (failed && params.getTag().equals(TAG)) {
        // only schedule a retry job if this is not already a retry
        scheduleRetryJob(getContext());
    }
    if (params.getTag().equals(TAG_RETRY)) {
        return failed ? Result.RESCHEDULE : Result.SUCCESS;
    } else {
        return failed ? Result.FAILURE : Result.SUCCESS;
    }
}
Also used : AccountDataSource(de.geeksfactory.opacclient.storage.AccountDataSource) OpacClient(de.geeksfactory.opacclient.OpacClient) SharedPreferences(android.content.SharedPreferences) NonNull(android.support.annotation.NonNull)

Example 8 with OpacClient

use of de.geeksfactory.opacclient.OpacClient in project opacclient by opacapp.

the class Utils method getAccountSubtitle.

public static String getAccountSubtitle(Account account, Context context) {
    try {
        OpacClient app = (OpacClient) context.getApplicationContext();
        Library library = app.getLibrary(account.getLibrary());
        if (context.getString(R.string.default_account_name).equals(account.getLabel())) {
            return library.getTitle();
        } else {
            return library.getCity() + " ยท " + library.getTitle();
        }
    } catch (IOException | JSONException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : OpacClient(de.geeksfactory.opacclient.OpacClient) JSONException(org.json.JSONException) Library(de.geeksfactory.opacclient.objects.Library) IOException(java.io.IOException)

Example 9 with OpacClient

use of de.geeksfactory.opacclient.OpacClient in project opacclient by opacapp.

the class AccountEditActivity method delete.

private void delete() {
    AccountDataSource data = new AccountDataSource(this);
    data.remove(account);
    // Check whether he deleted account was selected
    if (((OpacClient) getApplication()).getAccount().getId() == account.getId()) {
        List<Account> available_accounts = data.getAllAccounts();
        if (available_accounts.size() == 0) {
            ((OpacClient) getApplication()).setAccount(0);
            ((OpacClient) getApplication()).addFirstAccount(this);
        } else {
            ((OpacClient) getApplication()).setAccount(available_accounts.get(0).getId());
        }
    }
    new ReminderHelper((OpacClient) getApplication()).generateAlarms();
}
Also used : Account(de.geeksfactory.opacclient.objects.Account) AccountDataSource(de.geeksfactory.opacclient.storage.AccountDataSource) ReminderHelper(de.geeksfactory.opacclient.reminder.ReminderHelper) OpacClient(de.geeksfactory.opacclient.OpacClient)

Example 10 with OpacClient

use of de.geeksfactory.opacclient.OpacClient in project opacclient by opacapp.

the class AccountItemDetailActivity method onCreateOptionsMenu.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_account_item_detail, menu);
    MenuItem prolong = menu.findItem(R.id.action_prolong);
    MenuItem download = menu.findItem(R.id.action_download);
    MenuItem cancel = menu.findItem(R.id.action_cancel);
    MenuItem booking = menu.findItem(R.id.action_booking);
    OpacClient app = (OpacClient) getApplication();
    OpacApi api = null;
    try {
        api = app.getApi();
    } catch (OpacClient.LibraryRemovedException e) {
        e.printStackTrace();
    }
    if (item instanceof LentItem) {
        final LentItem i = (LentItem) item;
        cancel.setVisible(false);
        booking.setVisible(false);
        if (i.getProlongData() != null) {
            prolong.setVisible(true);
            // ViewCompat.setAlpha(prolong, item.isRenewable() ? 1f : 0.4f);
            download.setVisible(false);
        } else if (i.getDownloadData() != null && api != null && api instanceof EbookServiceApi) {
            prolong.setVisible(false);
            download.setVisible(true);
        } else {
            prolong.setVisible(false);
            download.setVisible(false);
        }
    } else if (item instanceof ReservedItem) {
        final ReservedItem i = (ReservedItem) item;
        prolong.setVisible(false);
        download.setVisible(false);
        if (i.getBookingData() != null) {
            booking.setVisible(true);
            cancel.setVisible(false);
        } else if (i.getCancelData() != null) {
            cancel.setVisible(true);
            booking.setVisible(false);
        } else {
            cancel.setVisible(false);
            booking.setVisible(false);
        }
    }
    return true;
}
Also used : OpacClient(de.geeksfactory.opacclient.OpacClient) OpacApi(de.geeksfactory.opacclient.apis.OpacApi) EbookServiceApi(de.geeksfactory.opacclient.apis.EbookServiceApi) MenuItem(android.view.MenuItem) ReservedItem(de.geeksfactory.opacclient.objects.ReservedItem) LentItem(de.geeksfactory.opacclient.objects.LentItem)

Aggregations

OpacClient (de.geeksfactory.opacclient.OpacClient)10 AccountDataSource (de.geeksfactory.opacclient.storage.AccountDataSource)5 OpacApi (de.geeksfactory.opacclient.apis.OpacApi)3 IOException (java.io.IOException)3 JSONException (org.json.JSONException)3 Account (de.geeksfactory.opacclient.objects.Account)2 Library (de.geeksfactory.opacclient.objects.Library)2 ReminderHelper (de.geeksfactory.opacclient.reminder.ReminderHelper)2 NotificationManager (android.app.NotificationManager)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 AndroidHttpClient (android.net.http.AndroidHttpClient)1 NonNull (android.support.annotation.NonNull)1 MenuItem (android.view.MenuItem)1 ApacheBaseApi (de.geeksfactory.opacclient.apis.ApacheBaseApi)1 EbookServiceApi (de.geeksfactory.opacclient.apis.EbookServiceApi)1 OkHttpBaseApi (de.geeksfactory.opacclient.apis.OkHttpBaseApi)1 AccountData (de.geeksfactory.opacclient.objects.AccountData)1 LentItem (de.geeksfactory.opacclient.objects.LentItem)1 ReservedItem (de.geeksfactory.opacclient.objects.ReservedItem)1