package util;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javafx.application.Platform;

import org.eclipse.egit.github.core.Issue;

import model.Model;

public class ModelUpdater {
	private Model model;
	private IssueUpdateService issueUpdateService;
	private long pollInterval = 60000; //time between polls in ms
	private Timer pollTimer;
	
	public ModelUpdater(GitHubClientExtended client, Model model){
		this.model = model;
		this.issueUpdateService = new IssueUpdateService(client);
	}
	
	private void updateModelIssues(){
		List<Issue> updatedIssues = issueUpdateService.getUpdatedIssues(model.getRepoId());
		Platform.runLater(new Runnable() {
	        @Override
	        public void run() {
	        	model.updateCachedIssues(updatedIssues);
	        }
	   });
	}
	
	public void startModelUpdate(){
		if(pollTimer != null){
			stopModelUpdate();
		}
		pollTimer = new Timer();
		TimerTask pollTask = new TimerTask(){
			@Override
			public void run() {
				updateModelIssues();
			}
		};
		pollTimer.scheduleAtFixedRate(pollTask, 0, pollInterval);
	}
	
	public void stopModelUpdate(){
		if(pollTimer != null){
			pollTimer.cancel();
			pollTimer = null;
		}
	}
}

@@ -8,16 +8,52 @@
  util;%0A%0A
+import java.lang.ref.WeakReference;%0A
 import j
@@ -683,16 +683,83 @@
 oId());%0A
+%09%09WeakReference%3CModel%3E modelRef = new WeakReference%3CModel%3E(model);%0A
 %09%09Platfo
@@ -845,16 +845,86 @@
        %09
+Model model = modelRef.get();%0A%09        %09if(model != null)%7B%0A%09        %09%09
 model.up
@@ -956,16 +956,28 @@
 ssues);%0A
+%09        %09%7D%0A
 %09       

use weak reference in run method of auto updater