Search in sources :

Example 1 with DataManager

use of ca.etsmtl.applets.etsmobile.http.DataManager in project ETSMobile-Android2 by ApplETS.

the class Utility method loadNotifications.

/**
     * Gets MonÉTS notifications and update DB
     * @param context
     * @param requestListener
     */
public static void loadNotifications(Context context, final RequestListener<Object> requestListener) {
    final SecurePreferences securePreferences = new SecurePreferences(context);
    final boolean allNotifsLoaded = securePreferences.getBoolean(Constants.ALL_NOTIFS_LOADED, false);
    MonETSNotificationsRequest monETSNotificationsRequest;
    if (!allNotifsLoaded) {
        monETSNotificationsRequest = new MonETSNotificationsRequest(context, false);
    } else {
        monETSNotificationsRequest = new MonETSNotificationsRequest(context, true);
    }
    final DataManager dataManager = DataManager.getInstance(context);
    dataManager.start();
    final DatabaseHelper databaseHelper = new DatabaseHelper(context);
    dataManager.sendRequest(monETSNotificationsRequest, new RequestListener<Object>() {

        @Override
        public void onRequestFailure(SpiceException spiceException) {
            requestListener.onRequestFailure(spiceException);
            dataManager.stop();
        }

        @Override
        public void onRequestSuccess(Object o) {
            if (o instanceof MonETSNotificationList) {
                try {
                    Dao<MonETSNotification, ?> dao = databaseHelper.getDao(MonETSNotification.class);
                    MonETSNotificationList list = (MonETSNotificationList) o;
                    for (MonETSNotification monETSNotification : list) {
                        dao.createOrUpdate(monETSNotification);
                    }
                    if (!allNotifsLoaded) {
                        securePreferences.edit().putBoolean(Constants.ALL_NOTIFS_LOADED, true).commit();
                    }
                    requestListener.onRequestSuccess(list);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            dataManager.stop();
        }
    });
}
Also used : MonETSNotificationList(ca.etsmtl.applets.etsmobile.model.MonETSNotificationList) MonETSNotification(ca.etsmtl.applets.etsmobile.model.MonETSNotification) SpiceException(com.octo.android.robospice.persistence.exception.SpiceException) SQLException(java.sql.SQLException) DataManager(ca.etsmtl.applets.etsmobile.http.DataManager) Dao(com.j256.ormlite.dao.Dao) MonETSNotificationsRequest(ca.etsmtl.applets.etsmobile.http.MonETSNotificationsRequest) DatabaseHelper(ca.etsmtl.applets.etsmobile.db.DatabaseHelper)

Example 2 with DataManager

use of ca.etsmtl.applets.etsmobile.http.DataManager in project ETSMobile-Android2 by ApplETS.

the class NotificationActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notifications);
    securePreferences = new SecurePreferences(this);
    progressBar = (ProgressBar) findViewById(R.id.progressBar_notifications);
    listView = (ListView) findViewById(R.id.listView_notifications);
    progressBar.getIndeterminateDrawable().setColorFilter(getResources().getColor(R.color.ets_red_fonce), PorterDuff.Mode.MULTIPLY);
    progressBar.setVisibility(View.VISIBLE);
    notificationsAdapter = new NotificationsAdapter(this, R.layout.row_notification, new ArrayList<>());
    listView.setAdapter(notificationsAdapter);
    listView.setOnItemClickListener((parent, view, position, id) -> {
        MonETSNotification item = notificationsAdapter.getItem(position);
        String url = item.getUrl();
        if (URLUtil.isValidUrl(url)) {
            Utility.openChromeCustomTabs(NotificationActivity.this, url);
        }
    });
    syncAdapterWithDB();
    DataManager datamanager = DataManager.getInstance(this);
    NotificationManager manager = new NotificationManager(this, datamanager.getMonETSService(), this);
    manager.updateNotifications();
}
Also used : NotificationsAdapter(ca.etsmtl.applets.etsmobile.ui.adapter.NotificationsAdapter) MonETSNotification(ca.etsmtl.applets.etsmobile.model.MonETSNotification) NotificationManager(ca.etsmtl.applets.etsmobile.util.NotificationManager) ArrayList(java.util.ArrayList) DataManager(ca.etsmtl.applets.etsmobile.http.DataManager) SecurePreferences(ca.etsmtl.applets.etsmobile.util.SecurePreferences)

Aggregations

DataManager (ca.etsmtl.applets.etsmobile.http.DataManager)2 MonETSNotification (ca.etsmtl.applets.etsmobile.model.MonETSNotification)2 DatabaseHelper (ca.etsmtl.applets.etsmobile.db.DatabaseHelper)1 MonETSNotificationsRequest (ca.etsmtl.applets.etsmobile.http.MonETSNotificationsRequest)1 MonETSNotificationList (ca.etsmtl.applets.etsmobile.model.MonETSNotificationList)1 NotificationsAdapter (ca.etsmtl.applets.etsmobile.ui.adapter.NotificationsAdapter)1 NotificationManager (ca.etsmtl.applets.etsmobile.util.NotificationManager)1 SecurePreferences (ca.etsmtl.applets.etsmobile.util.SecurePreferences)1 Dao (com.j256.ormlite.dao.Dao)1 SpiceException (com.octo.android.robospice.persistence.exception.SpiceException)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1