diff --git a/client/src/main/java/com/mytiki/publish/client/license/LicenseRequest.kt b/client/src/main/java/com/mytiki/publish/client/license/LicenseRequest.kt index 06027d1..b91df6b 100644 --- a/client/src/main/java/com/mytiki/publish/client/license/LicenseRequest.kt +++ b/client/src/main/java/com/mytiki/publish/client/license/LicenseRequest.kt @@ -10,22 +10,13 @@ import org.json.JSONObject class LicenseRequest { private val userSignature: ByteArray? -// TODO add parameters -// ptr -// tags -// uses -// description -// origin -// expiry -// terms + init { val keys = TikiClient.auth.getKey() val address = TikiClient.auth.address(keys) userSignature = address?.let { TikiClient.auth.signMessage(it, keys.private) } } - // TODO create a constructor for License Request - fun toJSON(context: Context, use: Use?, tag: List?): JSONObject { val usecaseJson = JSONArray().apply { use?.usecases?.forEach { put(it) } } val destinationJson = JSONArray().apply { use?.destinations?.forEach { put(it) } } @@ -33,7 +24,7 @@ class LicenseRequest { val jsonBody = JSONObject() - .put("ptr", TikiClient.userID) // TODO use PTR as parameter + .put("ptr", TikiClient.userID) .put("tags", tagsJson) .put( "uses", @@ -43,10 +34,10 @@ class LicenseRequest { put("usecases", usecaseJson) put("destinations", destinationJson) })) - .put("description", "") // TODO use description as parameter + .put("description", "") .put("origin", context.packageName) - .put("expiry", JSONObject.NULL) // TODO use expiry as parameter - .put("terms", TikiClient.license.terms(context)) // TODO use terms as parameter + .put("expiry", JSONObject.NULL) + .put("terms", TikiClient.license.terms(context)) val privateKey = TikiClient.auth.getKey().private ?: throw Exception("Private key not found") diff --git a/client/src/main/java/com/mytiki/publish/client/license/LicenseService.kt b/client/src/main/java/com/mytiki/publish/client/license/LicenseService.kt index 9e2a4e7..3e9a260 100644 --- a/client/src/main/java/com/mytiki/publish/client/license/LicenseService.kt +++ b/client/src/main/java/com/mytiki/publish/client/license/LicenseService.kt @@ -25,7 +25,7 @@ class LicenseService { * @throws Exception if there is an error creating the license. The exception message contains * details about the error. */ - suspend fun create(context: Context, ptr: String, use: List, tags: List) = + suspend fun create(context: Context, use: Use?, tags: List) = manageLicense(context, use, tags) /** @@ -37,7 +37,7 @@ class LicenseService { * @throws Exception if there is an error revoking the license. The exception message contains * details about the error. */ - suspend fun revoke(context: Context, prt: String, tags: List) = manageLicense(context, null, null) + suspend fun revoke(context: Context) = manageLicense(context, null, null) /** * Manages the license for the user. This is a private function used by the create and revoke @@ -79,7 +79,7 @@ class LicenseService { * @return Boolean indicating whether the license is valid. * @throws Exception if there is an error verifying the license. */ - suspend fun verify(ptr: String): Boolean { + suspend fun verify(): Boolean { val response = ApiService.post( header = diff --git a/client/src/main/java/com/mytiki/publish/client/offer/Offer.kt b/client/src/main/java/com/mytiki/publish/client/offer/Offer.kt index 75fca9e..6012527 100644 --- a/client/src/main/java/com/mytiki/publish/client/offer/Offer.kt +++ b/client/src/main/java/com/mytiki/publish/client/offer/Offer.kt @@ -14,9 +14,32 @@ private constructor( val ptr: String, val permissions: List? = null, val mutable: Boolean = true, + active: Boolean = false, ) { + var active: Boolean = active + private set + + private var isFirstChange = true var id = UUID.randomUUID().toString() + private set + + private constructor( + description: String, + rewards: List, + use: Use, + tags: List, + ptr: String, + permissions: List? = null, + mutable: Boolean = true, + active: Boolean = false, + isFirstChange: Boolean = true, + id: String = UUID.randomUUID().toString() + ) : this(description, rewards, use, tags, ptr, permissions, mutable) { + this.active = active + this.isFirstChange = isFirstChange + this.id = id + } class Builder { private var description: String = "" @@ -26,6 +49,8 @@ private constructor( private var ptr: String = "" private var permissions: List? = null private var mutable: Boolean = true + private var active: Boolean = false + private var isFirstChange: Boolean = true private var id: String = UUID.randomUUID().toString() fun description(description: String) = apply { this.description = description } @@ -94,4 +119,27 @@ private constructor( .put("ptr", ptr) } + internal fun activate(): Offer { + if (mutable) { + active = true + } else { + if (isFirstChange) { + active = true + isFirstChange = false + } + } + return this + } + + internal fun deactivate(): Offer { + if (mutable) { + active = false + } else { + if (isFirstChange) { + active = false + isFirstChange = false + } + } + return this + } } diff --git a/client/src/main/java/com/mytiki/publish/client/offer/OfferService.kt b/client/src/main/java/com/mytiki/publish/client/offer/OfferService.kt index 0f69d65..7f243d3 100644 --- a/client/src/main/java/com/mytiki/publish/client/offer/OfferService.kt +++ b/client/src/main/java/com/mytiki/publish/client/offer/OfferService.kt @@ -28,8 +28,22 @@ class OfferService { * @return The deactivated offer. */ fun decline(context: Context, offer: Offer): Offer { - MainScope().async { TikiClient.license.create(context, null, listOf(), offer.tags) } + MainScope().async { TikiClient.license.create(context, null, offer.tags) } return offer.deactivate() } + /** + * Updates the status of an offer. If the offer is active, it will be declined. If the offer is + * inactive, it will be accepted. However, if the offer is not mutable, the status of the offer + * will not be changed. + * + * @param context The context. This is typically the application context or the current activity. + * @param offer The offer whose status is to be updated. This is an instance of the Offer class. + * @return The updated offer. If the offer was mutable and active, the returned offer will be + * inactive, and vice versa. If the offer was not mutable, the same offer will be returned + * without any changes. + */ + fun updateOfferStatus(context: Context, offer: Offer): Offer { + return if (offer.active) decline(context, offer) else accept(context, offer) + } }