package zielu.gittoolbox.repo;

import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.messages.MessageBusConnection;
import git4idea.GitUtil;
import git4idea.repo.GitRepository;
import git4idea.repo.GitRepositoryChangeListener;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import org.jetbrains.annotations.NotNull;

public class GtRepositoryManager implements ProjectComponent, GitRepositoryChangeListener {
  private final Map<GitRepository, GtConfig> configs = new ConcurrentHashMap<GitRepository, GtConfig>();
  private final Project project;
  private MessageBusConnection connection;

  public GtRepositoryManager(@NotNull Project project) {
    this.project = project;
  }

  @NotNull
  public static GtRepositoryManager getInstance(@NotNull Project project) {
    return project.getComponent(GtRepositoryManager.class);
  }

  @Override
  public void repositoryChanged(@NotNull GitRepository repository) {
    VirtualFile gitDir = GitUtil.findGitDir(repository.getRoot());
    Optional.ofNullable(gitDir)
        .map(dir -> dir.findChild("config"))
        .map(VfsUtilCore::virtualToIoFile)
        .map(GtConfig::load)
        .ifPresent(config -> configs.put(repository, config));
  }

  public Optional<GtConfig> configFor(GitRepository repository) {
    return Optional.ofNullable(configs.get(repository));
  }

  @Override
  public void initComponent() {
    connection = project.getMessageBus().connect();
    connection.subscribe(GitRepository.GIT_REPO_CHANGE, this);
  }

  @Override
  public void disposeComponent() {
    if (connection != null) {
      connection.disconnect();
      connection = null;
    }
    configs.clear();
  }
}

@@ -57,35 +57,18 @@
 api.
-components.ProjectComponent
+Disposable
 ;%0Aim
@@ -553,26 +553,8 @@
 ents
- ProjectComponent,
  Git
@@ -577,16 +577,28 @@
 Listener
+, Disposable
  %7B%0A  pri
@@ -669,39 +669,16 @@
 HashMap%3C
-GitRepository, GtConfig
 %3E();%0A  p
@@ -836,16 +836,183 @@
 roject;%0A
+    //TODO: should be changed to subscriber instead%0A    connection = project.getMessageBus().connect();%0A    connection.subscribe(GitRepository.GIT_REPO_CHANGE, this);%0A
   %7D%0A%0A  @
@@ -1683,284 +1683,19 @@
 oid 
-initComponent() %7B%0A    connection = project.getMessageBus().connect();%0A    connection.subscribe(GitRepository.GIT_REPO_CHANGE, this);%0A  %7D%0A%0A  @Override%0A  public void disposeComponent() %7B%0A    if (connection != null) %7B%0A      connection.disconnect();%0A      connection = null;%0A    %7D
+dispose() %7B
 %0A   

Replace deprecated APIs usage