Skip to content

Commit

Permalink
Merge pull request #24 from matchmore/feature/naming_updates
Browse files Browse the repository at this point in the history
Feature/naming updates
  • Loading branch information
maciejburda authored Mar 13, 2018
2 parents e663c3a + 4a985d5 commit 341c720
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected void onCreate(Bundle savedInstanceState) {
Log.d("JavaExample", device.getId());
return Unit.INSTANCE;
});

matchMore.getMatchMonitor().startPollingMatches();

// Start updating location
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Expand Down
12 changes: 6 additions & 6 deletions rx/src/main/java/io/matchmore/sdk/rx/MathMore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import io.matchmore.sdk.api.SuccessCallback
import io.matchmore.sdk.api.models.*
import io.matchmore.sdk.monitoring.MatchMonitor
import io.matchmore.sdk.store.AsyncClearable
import io.matchmore.sdk.store.AsyncCreateable
import io.matchmore.sdk.store.AsyncDeleteable
import io.matchmore.sdk.store.AsyncUpdateable
import io.matchmore.sdk.store.AsyncCreatable
import io.matchmore.sdk.store.AsyncDeletable
import io.matchmore.sdk.store.AsyncUpdatable
import io.reactivex.*

private fun completableEmitter(emitter: CompletableEmitter): () -> Unit = { if (!emitter.isDisposed) emitter.onComplete() }
Expand Down Expand Up @@ -49,13 +49,13 @@ fun MatchMoreSdk.rxCreateSubscription(subscription: Subscription, deviceWithId:
fun MatchMoreSdk.rxCreatePinDevice(pinDevice: PinDevice): Single<PinDevice> =
singleRx { success, error -> createPinDevice(pinDevice, success, error) }

fun <T> AsyncCreateable<T>.rxCreate(item: T): Single<T> =
fun <T> AsyncCreatable<T>.rxCreate(item: T): Single<T> =
singleRx { success, error -> create(item, success, error) }

fun <T> AsyncUpdateable<T>.rxUpdate(item: T): Single<T> =
fun <T> AsyncUpdatable<T>.rxUpdate(item: T): Single<T> =
singleRx { success, error -> update(item, success, error) }

fun <T> AsyncDeleteable<T>.rxDelete(item: T): Completable =
fun <T> AsyncDeletable<T>.rxDelete(item: T): Completable =
completableRx { complete, error -> delete(item, complete, error) }

fun AsyncClearable.rxDeleteAll(): Completable = completableRx { complete, error -> deleteAll(complete, error) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ class MatchMoreBeaconManager(

override fun onBeaconServiceConnect() {
beaconManager.addRangeNotifier { beacons, _ ->
beacons.forEach { beacon ->
Log.i("beacon", "${beacon.id1} ${beacon.distance}")
if (shouldTrigger(beacon)) {
val proximityEvent = ProximityEvent(deviceId = beacon.id1.toString(), distance = beacon.distance)
apiClient.deviceApi.triggerProximityEvents(deviceStore.main!!.id!!, proximityEvent)
.async({ beaconsTriggered.put(beacon.id1, TriggerInfo(System.currentTimeMillis(), beacon.distance)) })
deviceStore.main?.id?.let { mainDeviceId ->
beacons.forEach { beacon ->
Log.i("beacon", "${beacon.id1} ${beacon.distance}")
if (shouldTrigger(beacon)) {
val proximityEvent = ProximityEvent(deviceId = beacon.id1.toString(), distance = beacon.distance)
apiClient.deviceApi.triggerProximityEvents(mainDeviceId, proximityEvent)
.async({ beaconsTriggered[beacon.id1] = TriggerInfo(System.currentTimeMillis(), beacon.distance) })
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions sdk/src/main/java/io/matchmore/sdk/monitoring/MatchMonitor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import java.util.*
typealias MatchMonitorListener = (Set<Match>, Device) -> Unit

class MatchMonitor(private val manager: AlpsManager, private val config: MatchMoreConfig) {
val monitoredDevices = mutableSetOf<Device>()
val deliveredMatches = mutableSetOf<Match>()
val socketListener = MatchSocketListener()

private val monitoredDevices = mutableSetOf<Device>()
private val socketListener = MatchSocketListener()

private var listeners = mutableSetOf<MatchMonitorListener>()

Expand All @@ -34,6 +34,7 @@ class MatchMonitor(private val manager: AlpsManager, private val config: MatchMo
listeners.remove(listener)
}

// TODO: start new socket after adding new device ?
fun openSocketForMatches() {
if (socket != null) {
return
Expand Down
16 changes: 8 additions & 8 deletions sdk/src/main/java/io/matchmore/sdk/store/AssociatedDataType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,37 @@ import io.matchmore.sdk.api.ErrorCallback
import io.matchmore.sdk.api.SuccessCallback
import io.matchmore.sdk.utils.CallbacksGroup

interface CRUD<T> : CRD<T>, AsyncUpdateable<T>
interface CRUD<T> : CRD<T>, AsyncUpdatable<T>

interface CRD<T> : AsyncCreateable<T>, AsyncReadable<T>, AsyncDeleteable<T>, AsyncClearable {
interface CRD<T> : AsyncCreatable<T>, AsyncReadable<T>, AsyncDeletable<T>, AsyncClearable {
override fun deleteAll(complete: CompleteCallback?, error: ErrorCallback?) {
val group = CallbacksGroup(complete, error)
findAll().forEach {
findAll().forEach { item ->
group.enter()
delete(it, group::complete, group::error)
delete(item, group::complete, group::error)
}
group.end()
}
}

interface AsyncCreateable<T> {
interface AsyncCreatable<T> {
fun create(item: T, success: SuccessCallback<T>?) = create(item, success, null)
fun create(item: T) = create(item, null, null)
fun create(item: T, success: SuccessCallback<T>? = null, error: ErrorCallback? = null)
}

interface AsyncReadable<T> {
interface AsyncReadable<out T> {
fun find(byId: String): T?
fun findAll(): List<T>
}

interface AsyncUpdateable<T> {
interface AsyncUpdatable<T> {
fun update(item: T, success: SuccessCallback<T>?) = update(item, success, null)
fun update(item: T) = update(item, null, null)
fun update(item: T, success: SuccessCallback<T>? = null, error: ErrorCallback? = null)
}

interface AsyncDeleteable<T> {
interface AsyncDeletable<in T> {
fun delete(item: T, complete: CompleteCallback?) = delete(item, complete, null)
fun delete(item: T, error: ErrorCallback?) = delete(item, null, error)
fun delete(item: T) = delete(item, null, null)
Expand Down
36 changes: 21 additions & 15 deletions sdk/src/main/java/io/matchmore/sdk/store/DeviceStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.matchmore.sdk.api.SuccessCallback
import io.matchmore.sdk.api.async
import io.matchmore.sdk.api.models.Device
import io.matchmore.sdk.api.models.MobileDevice
import io.matchmore.sdk.utils.unwrap

typealias DeviceDeleteListener = (String) -> Unit

Expand Down Expand Up @@ -66,26 +67,31 @@ class DeviceStore(private val manager: AlpsManager)
}

override fun delete(item: Device, complete: CompleteCallback?, error: ErrorCallback?) {
manager.apiClient.deviceApi.deleteDevice(item.id!!).async({
deleteData(item)
if (item == main) main = null
manager.matchMonitor.stopMonitoringFor(item)
listeners.forEach {
it.invoke(item.id!!)
}
complete?.invoke()
}, error)
item.id?.let { itemId ->
manager.apiClient.deviceApi.deleteDevice(itemId).async({
deleteData(item)
if (item == main) main = null
manager.matchMonitor.stopMonitoringFor(item)
listeners.forEach {
it.invoke(itemId)
}
complete?.invoke()
}, error)
} ?: run {
error(Throwable("Item ID is required"))
}

}

fun registerDeviceToken(token: String) {
main?.let {
it.deviceToken = "fcm://$token"
manager.apiClient.deviceApi.updateDevice(it.id!!, it).async({ _ -> })
}
unwrap(main, main?.id, { main, mainId ->
main.deviceToken = "fcm://$token"
manager.apiClient.deviceApi.updateDevice(mainId, main).async({ _ -> })
})
}

companion object {
val MOBILE_DEVICES_FILE = "kMobileDevicesFile.Alps"
val MAIN_DEVICE_FILE = "kMainDeviceFile.Alps"
private const val MOBILE_DEVICES_FILE = "kMobileDevicesFile.Alps"
private const val MAIN_DEVICE_FILE = "kMainDeviceFile.Alps"
}
}
32 changes: 21 additions & 11 deletions sdk/src/main/java/io/matchmore/sdk/store/PublicationStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import io.matchmore.sdk.api.ErrorCallback
import io.matchmore.sdk.api.SuccessCallback
import io.matchmore.sdk.api.async
import io.matchmore.sdk.api.models.Publication
import io.matchmore.sdk.utils.unwrap
import io.matchmore.sdk.utils.withoutExpired

class PublicationStore(private val manager: AlpsManager) : CRD<Publication>,
Store<Publication>(manager.persistenceManager, PUBLICATIONS_FILE) {

override var items: List<Publication> = listOf<Publication>()
override var items = listOf<Publication>()
get() = field.withoutExpired()
set(value) {
Thread({ manager.persistenceManager.writeData(value, PUBLICATIONS_FILE) }).start()
Expand All @@ -29,19 +30,28 @@ class PublicationStore(private val manager: AlpsManager) : CRD<Publication>,

override fun create(item: Publication, success: SuccessCallback<Publication>?, error: ErrorCallback?) {
item.deviceId = item.deviceId ?: manager.main?.id
manager.apiClient.publicationApi.createPublication(item.deviceId!!, item)
.async({
createData(it)
success?.invoke(it)
}, error)
item.deviceId?.let { deviceId ->
manager.apiClient.publicationApi.createPublication(deviceId, item)
.async({
createData(it)
success?.invoke(it)
}, error)
} ?: run {
error(Throwable("Publication has to have device ID."))
}
}

override fun delete(item: Publication, complete: CompleteCallback?, error: ErrorCallback?) {
manager.apiClient.publicationApi.deletePublication(item.deviceId!!, item.id!!)
.async({
deleteData(item)
complete?.invoke()
}, error)
unwrap(item?.deviceId, item?.id, { deviceId, id ->
manager.apiClient.publicationApi.deletePublication(deviceId, id)
.async({
deleteData(item)
complete?.invoke()
}, error)
}, {
error(Throwable("Publication has to have ID and device ID."))
})

}

var onDeviceDelete: DeviceDeleteListener = { id ->
Expand Down
31 changes: 20 additions & 11 deletions sdk/src/main/java/io/matchmore/sdk/store/SubscriptionStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import io.matchmore.sdk.api.ErrorCallback
import io.matchmore.sdk.api.SuccessCallback
import io.matchmore.sdk.api.async
import io.matchmore.sdk.api.models.Subscription
import io.matchmore.sdk.utils.unwrap
import io.matchmore.sdk.utils.withoutExpired

class SubscriptionStore(private val manager: AlpsManager) : CRD<Subscription>,
Store<Subscription>(manager.persistenceManager, SUBSCRIPTIONS_FILE) {

override var items: List<Subscription> = listOf<Subscription>()
override var items = listOf<Subscription>()
get() = field.withoutExpired()
set(value) {
Thread({ manager.persistenceManager.writeData(value, SUBSCRIPTIONS_FILE) }).start()
Expand All @@ -28,19 +29,27 @@ class SubscriptionStore(private val manager: AlpsManager) : CRD<Subscription>,
}

override fun create(item: Subscription, success: SuccessCallback<Subscription>?, error: ErrorCallback?) {
manager.apiClient.subscriptionApi.createSubscription(item.deviceId!!, item)
.async({
createData(it)
success?.invoke(it)
}, error)
item?.deviceId?.let { deviceId ->
manager.apiClient.subscriptionApi.createSubscription(deviceId, item)
.async({
createData(it)
success?.invoke(it)
}, error)
} ?: run {
error(Throwable("Subscription has to have device ID."))
}
}

override fun delete(item: Subscription, complete: CompleteCallback?, error: ErrorCallback?) {
manager.apiClient.subscriptionApi.deleteSubscription(item.deviceId!!, item.id!!)
.async({
deleteData(item)
complete?.invoke()
}, error)
unwrap(item?.deviceId, item?.id, { deviceId, id ->
manager.apiClient.subscriptionApi.deleteSubscription(deviceId, id)
.async({
deleteData(item)
complete?.invoke()
}, error)
}, {
error(Throwable("Subscription has to have ID and device ID."))
})
}

var onDeviceDelete: DeviceDeleteListener = { id ->
Expand Down
5 changes: 5 additions & 0 deletions sdk/src/main/java/io/matchmore/sdk/utils/Unwrap.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.matchmore.sdk.utils

fun <T1: Any, T2: Any> unwrap(p1: T1?, p2: T2?, block: (T1, T2) -> Unit, failBlock: (() -> Unit)? = null) {
if (p1 != null && p2 != null) block.invoke(p1, p2) else failBlock?.invoke()
}

0 comments on commit 341c720

Please sign in to comment.