package im.delight.android.ddp;

/*
 * Copyright (c) delight.im <info@delight.im>
 *
 * 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.
 */

import android.content.Context;

/** Provides a single access point to the `Meteor` class that can be used across `Activity` instances */
public class MeteorSingleton extends Meteor {

	private static MeteorSingleton mInstance;

	public synchronized static MeteorSingleton createInstance(final Context context, final String serverUri) {
		return createInstance(context, serverUri, null);
	}

	public synchronized static MeteorSingleton createInstance(final Context context, final String serverUri, final String protocolVersion) {
		if (mInstance != null) {
			throw new IllegalStateException("An instance has already been created");
		}

		if (protocolVersion == null) {
			mInstance = new MeteorSingleton(context, serverUri);
		}
		else {
			mInstance = new MeteorSingleton(context, serverUri, protocolVersion);
		}

		return mInstance;
	}

	public synchronized static MeteorSingleton getInstance() {
		if (mInstance == null) {
			throw new IllegalStateException("Please call `createInstance(...)` first");
		}

		return mInstance;
	}

	public synchronized static boolean hasInstance() {
		return mInstance != null;
	}

	public synchronized static void destroyInstance() {
		if (mInstance == null) {
			throw new IllegalStateException("Please call `createInstance(...)` first");
		}

		mInstance.disconnect();
		mInstance.removeCallbacks();
		mInstance = null;
	}

	private MeteorSingleton(final Context context, final String serverUri) {
		super(context, serverUri);
	}

	private MeteorSingleton(final Context context, final String serverUri, final String protocolVersion) {
		super(context, serverUri, protocolVersion);
	}

}

@@ -639,16 +639,60 @@
 e.%0A */%0A%0A
+import im.delight.android.ddp.db.DataStore;%0A
 import a
@@ -1026,410 +1026,1144 @@
 %7B%0A%09%09
-return createInstance(context, serverUri, null);%0A%09%7D%0A%0A%09public synchronized static MeteorSingleton createInstance(final Context context, final String serverUri, final String protocolVersion) %7B%0A%09%09if (mInstance != null) %7B%0A%09%09%09throw new IllegalStateException(%22An instance has already been created%22);%0A%09%09%7D%0A%0A%09%09if (protocolVersion == null) %7B%0A%09%09%09mInstance = new MeteorSingleton(context, serverUri);%0A%09%09%7D%0A%09%09else %7B%0A%09
+if (mInstance != null) %7B%0A%09%09%09throw new IllegalStateException(%22An instance has already been created%22);%0A%09%09%7D%0A%0A%09%09mInstance = new MeteorSingleton(context, serverUri);%0A%0A%09%09return mInstance;%0A%09%7D%0A%0A%09public synchronized static MeteorSingleton createInstance(final Context context, final String serverUri, final DataStore dataStore) %7B%0A%09%09if (mInstance != null) %7B%0A%09%09%09throw new IllegalStateException(%22An instance has already been created%22);%0A%09%09%7D%0A%0A%09%09mInstance = new MeteorSingleton(context, serverUri, dataStore);%0A%0A%09%09return mInstance;%0A%09%7D%0A%0A%09public synchronized static MeteorSingleton createInstance(final Context context, final String serverUri, final String protocolVersion) %7B%0A%09%09if (mInstance != null) %7B%0A%09%09%09throw new IllegalStateException(%22An instance has already been created%22);%0A%09%09%7D%0A%0A%09%09mInstance = new MeteorSingleton(context, serverUri, protocolVersion);%0A%0A%09%09return mInstance;%0A%09%7D%0A%0A%09public synchronized static MeteorSingleton createInstance(final Context context, final String serverUri, final String protocolVersion, final DataStore dataStore) %7B%0A%09%09if (mInstance != null) %7B%0A%09%09%09throw new IllegalStateException(%22An instance has already been created%22);%0A%09%09%7D%0A%0A
 %09%09mI
@@ -2227,22 +2227,29 @@
 lVersion
-);%0A%09%09%7D
+, dataStore);
 %0A%0A%09%09retu
@@ -2979,77 +2979,414 @@
 nal 
-String protocolVersion) %7B%0A%09%09super(context, serverUri, protocolVersion
+DataStore dataStore) %7B%0A%09%09super(context, serverUri, dataStore);%0A%09%7D%0A%0A%09private MeteorSingleton(final Context context, final String serverUri, final String protocolVersion) %7B%0A%09%09super(context, serverUri, protocolVersion);%0A%09%7D%0A%0A%09private MeteorSingleton(final Context context, final String serverUri, final String protocolVersion, final DataStore dataStore) %7B%0A%09%09super(context, serverUri, protocolVersion, dataStore
 );%0A%09

Fix missing equivalents to 'Meteor' constructors in 'MeteorSingleton'