package com.mgaetan89.showsrage.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;

import com.mgaetan89.showsrage.Constants;
import com.mgaetan89.showsrage.R;
import com.mgaetan89.showsrage.adapter.ShowsAdapter;
import com.mgaetan89.showsrage.fragment.ShowsFragment;
import com.mgaetan89.showsrage.model.RootDir;
import com.mgaetan89.showsrage.model.RootDirs;
import com.mgaetan89.showsrage.model.Show;
import com.mgaetan89.showsrage.network.SickRageApi;

import java.lang.ref.WeakReference;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class ShowsActivity extends BaseActivity implements ShowsAdapter.OnShowSelectedListener {
	@Override
	public void onShowSelected(@NonNull Show show) {
		Intent intent = new Intent(this, ShowActivity.class);
		intent.putExtra(Constants.Bundle.SHOW_MODEL, show);

		this.startActivity(intent);
	}

	@Override
	protected boolean displayHomeAsUp() {
		return false;
	}

	@Override
	protected Fragment getFragment() {
		return new ShowsFragment();
	}

	@Override
	protected int getSelectedMenuId() {
		return R.id.menu_shows;
	}

	@Override
	protected int getTitleResourceId() {
		return R.string.shows;
	}

	@Override
	protected void onResume() {
		super.onResume();

		SickRageApi.getInstance().getServices().getRootDirs(new RootDirsCallback(this));
	}

	private static final class RootDirsCallback implements Callback<RootDirs> {
		@NonNull
		private WeakReference<Activity> activityReference = new WeakReference<>(null);

		private RootDirsCallback(Activity activity) {
			this.activityReference = new WeakReference<>(activity);
		}

		@Override
		public void failure(RetrofitError error) {
			error.printStackTrace();
		}

		@Override
		public void success(RootDirs rootDirs, Response response) {
			if (rootDirs == null) {
				return;
			}

			List<RootDir> data = rootDirs.getData();
			Set<String> rootPaths = new HashSet<>(data.size());

			for (RootDir rootDir : data) {
				rootPaths.add(rootDir.getLocation());
			}

			SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this.activityReference.get()).edit();
			editor.putStringSet(Constants.Preferences.Fields.ROOT_DIRS, rootPaths);
			editor.apply();
		}
	}
}

@@ -2050,12 +2050,86 @@
 %0A%09%09%09
-if (
+Activity activity = this.activityReference.get();%0A%0A%09%09%09if (activity == null %7C%7C 
 root
@@ -2427,29 +2427,24 @@
 erences(
-this.
 activity
 Referenc
@@ -2435,31 +2435,16 @@
 activity
-Reference.get()
 ).edit()

Fix NullPointerException