/*
 * Copyright (C) 2018 Simon Vig Therkildsen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.simonvt.cathode.common.data;

import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import timber.log.Timber;

public abstract class ListenableLiveData<D> extends AsyncLiveData<D>
    implements ThrottleContentObserver.Callback {

  private Context context;

  private final List<Uri> notificationUris = new ArrayList<>();
  private final Map<Uri, ContentObserver> observers = new HashMap<>();

  public ListenableLiveData(Context context) {
    this.context = context;
  }

  public Context getContext() {
    return context;
  }

  @Override protected void onActive() {
    super.onActive();
    Timber.d("onActive");
    for (Uri uri : notificationUris) {
      registerUri(uri);
    }
  }

  @Override protected void onInactive() {
    super.onInactive();
    Timber.d("onInactive");
    for (Uri uri : notificationUris) {
      unregisterUri(uri);
    }
  }

  public void addNotificationUri(Uri uri) {
    synchronized (notificationUris) {
      notificationUris.add(uri);
      if (hasActiveObservers()) {
        registerUri(uri);
      }
    }
  }

  public void clearNotificationUris() {
    synchronized (notificationUris) {
      for (Uri uri : notificationUris) {
        unregisterUri(uri);
      }

      notificationUris.clear();
    }
  }

  public void removeNotificationUri(Uri uri) {
    synchronized (notificationUris) {
      notificationUris.remove(uri);
      if (hasActiveObservers()) {
        unregisterUri(uri);
      }
    }
  }

  protected void registerUri(Uri uri) {
    Timber.d("registerUri");
    ContentObserver observer = new ThrottleContentObserver(this);
    observers.put(uri, observer);
    context.getContentResolver().registerContentObserver(uri, true, observer);
  }

  protected void unregisterUri(Uri uri) {
    Timber.d("unregisterUri");
    if (observers.containsKey(uri)) {
      ContentObserver observer = observers.get(uri);
      observers.remove(uri);
      context.getContentResolver().unregisterContentObserver(observer);
    }
  }

  @Override public void onContentChanged() {
    loadData();
  }
}

@@ -1368,32 +1368,72 @@
 .d(%22onActive%22);%0A
+    synchronized (notificationUris) %7B%0A  
     for (Uri uri
@@ -1457,24 +1457,26 @@
 is) %7B%0A      
+  
 registerUri(
@@ -1473,32 +1473,40 @@
 gisterUri(uri);%0A
+      %7D%0A
     %7D%0A  %7D%0A%0A  @Ov
@@ -1590,24 +1590,64 @@
 Inactive%22);%0A
+    synchronized (notificationUris) %7B%0A  
     for (Uri
@@ -1675,24 +1675,26 @@
 is) %7B%0A      
+  
 unregisterUr
@@ -1697,24 +1697,32 @@
 erUri(uri);%0A
+      %7D%0A
     %7D%0A  %7D%0A%0A 
@@ -2380,24 +2380,57 @@
 isterUri%22);%0A
+    synchronized (observers) %7B%0A  
     ContentO
@@ -2483,24 +2483,26 @@
 (this);%0A    
+ 
+ 
 observers.pu
@@ -2515,24 +2515,26 @@
  observer);%0A
+  
     context.
@@ -2600,16 +2600,22 @@
 erver);%0A
+    %7D%0A
   %7D%0A%0A  p
@@ -2680,24 +2680,57 @@
 isterUri%22);%0A
+    synchronized (observers) %7B%0A  
     if (obse
@@ -2751,24 +2751,26 @@
 Key(uri)) %7B%0A
+  
       Conten
@@ -2808,32 +2808,34 @@
 get(uri);%0A      
+ 
+ 
 observers.remove
@@ -2837,24 +2837,26 @@
 emove(uri);%0A
+  
       contex
@@ -2907,32 +2907,40 @@
 rver(observer);%0A
+      %7D%0A
     %7D%0A  %7D%0A%0A  @Ov

Synchronize ListenableLiveData.