<commit_before>package com.boztalay.picturewidget.persistence;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import android.content.Context;
import android.content.SharedPreferences;

import com.boztalay.picturewidget.R;

public class PictureWidgetPersistenceManager {
	private static final String ALBUM_IDS_KEY = "albumIds";
	private static final String CURRENT_ALBUM_KEY = "currentAlbum";

	private SharedPreferences sharedPrefs;

	private Set<String> albumIds;
	private Map<String, Album> albums;

	public PictureWidgetPersistenceManager(Context context) {
		this.sharedPrefs = context.getSharedPreferences(context.getString(R.string.shared_prefs_name), Context.MODE_PRIVATE);
	}

	public Set<String> getAlbumIds() {
		loadAlbumIdsFromSharedPrefsIfNeeded();

		return albumIds;
	}
	
	private void loadAlbumIdsFromSharedPrefsIfNeeded() {
		if(albumIds == null) {
			albumIds = sharedPrefs.getStringSet(ALBUM_IDS_KEY, null);
			if(albumIds == null) {
				albumIds = new HashSet<String>();
			}
		}
	}

	public Album getAlbumWithId(String albumId) {
		if(albums == null) {
			albums = new HashMap<String, Album>();
		}

		if(albums.containsKey(albumId)) {
			return albums.get(albumId);
		} else {
			String albumJson = sharedPrefs.getString(albumId, null);
			if(albumJson == null) {
				throw new RuntimeException("Couldn't find JSON stored for album " + albumId);
			}

			Album album = AlbumParser.parseFromJsonRepresentation(albumJson);
			albums.put(albumId, album);
			
			return album;
		}
	}
	
	public void saveAlbum(Album album) {
		loadAlbumIdsFromSharedPrefsIfNeeded();
		
		if(!albumIds.contains(album.getId())) {
			albumIds.add(album.getId());
		}
		albums.put(album.getId(), album);
		
		sharedPrefs.edit().putString(album.getId(), AlbumParser.makeJsonRepresentation(album))
						  .putStringSet(ALBUM_IDS_KEY, albumIds)
						  .commit();
	}
	
	public void setCurrentAlbum(Album album) {
		sharedPrefs.edit().putString(CURRENT_ALBUM_KEY, album.getId()).commit();
	}
	
	public String getCurrentAlbumId() {
		return sharedPrefs.getString(CURRENT_ALBUM_KEY, null);
	}
}<commit_msg>Made the persistence manager listen for shared prefs changes, so multiple instances stay up to date<commit_after>package com.boztalay.picturewidget.persistence;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import android.content.Context;
import android.content.SharedPreferences;

import com.boztalay.picturewidget.R;

public class PictureWidgetPersistenceManager implements SharedPreferences.OnSharedPreferenceChangeListener {
	private static final String ALBUM_IDS_KEY = "albumIds";
	private static final String CURRENT_ALBUM_KEY = "currentAlbum";

	private SharedPreferences sharedPrefs;

	private Set<String> albumIds;
	private Map<String, Album> albums;
	private String currentAlbum;

	public PictureWidgetPersistenceManager(Context context) {
		this.sharedPrefs = context.getSharedPreferences(context.getString(R.string.shared_prefs_name), Context.MODE_PRIVATE);
	}

	public Set<String> getAlbumIds() {
		loadAlbumIdsFromSharedPrefsIfNeeded();

		return albumIds;
	}
	
	private void loadAlbumIdsFromSharedPrefsIfNeeded() {
		if(albumIds == null) {
			forceLoadAlbumIdsFromSharedPrefs();
		}
	}
	
	private void forceLoadAlbumIdsFromSharedPrefs() {
		albumIds = sharedPrefs.getStringSet(ALBUM_IDS_KEY, null);
		if(albumIds == null) {
			albumIds = new HashSet<String>();
		}
	}

	public Album getAlbumWithId(String albumId) {
		loadAlbumIdsFromSharedPrefsIfNeeded();

		if(albums.containsKey(albumId)) {
			return albums.get(albumId);
		} else {
			return loadAlbumWithIdFromSharedPrefsAndUpdateWorkingCopy(albumId);
		}
	}
	
	private Album loadAlbumWithIdFromSharedPrefsAndUpdateWorkingCopy(String albumId) {
		String albumJson = sharedPrefs.getString(albumId, null);
		if(albumJson == null) {
			throw new RuntimeException("Couldn't find JSON stored for album " + albumId);
		}

		Album album = AlbumParser.parseFromJsonRepresentation(albumJson);
		albums.put(albumId, album);
		
		return album;
	}
	
	public void saveAlbum(Album album) {
		loadAlbumIdsFromSharedPrefsIfNeeded();
		
		if(!albumIds.contains(album.getId())) {
			albumIds.add(album.getId());
		}
		albums.put(album.getId(), album);
		
		sharedPrefs.edit().putString(album.getId(), AlbumParser.makeJsonRepresentation(album))
						  .putStringSet(ALBUM_IDS_KEY, albumIds)
						  .commit();
	}
	
	public void setCurrentAlbum(Album album) {
		sharedPrefs.edit().putString(CURRENT_ALBUM_KEY, album.getId()).commit();
	}
	
	public String getCurrentAlbumId() {
		if(currentAlbum == null) {
			forceLoadCurrentAlbumFromSharedPrefs();
		}
		
		return currentAlbum;
	}
	
	private void forceLoadCurrentAlbumFromSharedPrefs() {
		currentAlbum = sharedPrefs.getString(CURRENT_ALBUM_KEY, null);
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
		if(key.equals(ALBUM_IDS_KEY)) {
			forceLoadAlbumIdsFromSharedPrefs();
		} else if(key.equals(CURRENT_ALBUM_KEY)) {
			forceLoadCurrentAlbumFromSharedPrefs();
		} else if(albumIds.contains(key)) {
			loadAlbumWithIdFromSharedPrefsAndUpdateWorkingCopy(key);
		}
	}
}
{'fraction_non_alphanumeric': 0.1040184579888483, 'fraction_numerical': 0.0, 'mean_word_length': 5.34390243902439, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3439718', 'n_tokens_mistral': 1842, 'n_tokens_neox': 1620, 'n_words': 302}