Android Open Source - lifesaver Session Shared Preferences Dao






From Project

Back to project page lifesaver.

License

The source code is released under:

Copyright (c) 2013 Nitin Dhar Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Softwar...

If you think the Android project lifesaver listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.nitindhar.lifesaver.data;
// w  w  w  . jav a 2 s  .  c om
import android.content.SharedPreferences;

import com.google.common.base.Optional;
import com.nitindhar.lifesaver.model.Session;
import com.nitindhar.lifesaver.model.Subway;

public class SessionSharedPreferencesDao implements SessionDao {

    private static final String SESSION_KEY_MRS = "most_recent_subway";

    private final SharedPreferences preferences;

    public SessionSharedPreferencesDao(SharedPreferences preferences) {
        this.preferences = preferences;
    }

    @Override
    public boolean sessionExists() {
        return preferences.getString(SESSION_KEY_MRS, null) != null;
    }

    @Override
    public boolean storeSession(Session session) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(SESSION_KEY_MRS, session.getMostRecentSubway().getCode());
        return editor.commit();
    }

    @Override
    public Session retrieveSession() {
        Optional<Subway> subwayOpt = Subway.fromString(preferences.getString(SESSION_KEY_MRS, null));
        if(subwayOpt.isPresent()) {
            return new Session(subwayOpt.get());
        } else {
            throw new IllegalStateException("No subway code received");
        }
    }

}




Java Source Code List

com.nitindhar.lifesaver.LifesaverActivity.java
com.nitindhar.lifesaver.data.SessionDao.java
com.nitindhar.lifesaver.data.SessionSharedPreferencesDao.java
com.nitindhar.lifesaver.model.Session.java
com.nitindhar.lifesaver.model.Subway.java