mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-29 22:03:55 +01:00
Convert API clients to kotlin (#5567)
* Convert UserClient to kotlin * Added tests for WikiBaseClient * Removed superfluous dao tests in review helper and got the proper ReviewDaoTest running in the unit test suite * Improved tests for ReviewHelper * Convert the ReviewHelper to kotlin * Convert the WikiBaseClient to kotlin * Convert the WikidataClient to kotlin
This commit is contained in:
parent
c43405267a
commit
728712c4e1
15 changed files with 496 additions and 526 deletions
|
|
@ -1,46 +0,0 @@
|
|||
package fr.free.nrw.commons.mwapi;
|
||||
|
||||
import fr.free.nrw.commons.wikidata.mwapi.MwQueryResponse;
|
||||
import fr.free.nrw.commons.wikidata.mwapi.MwQueryResult;
|
||||
import fr.free.nrw.commons.wikidata.mwapi.UserInfo;
|
||||
import fr.free.nrw.commons.utils.DateUtil;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.Single;
|
||||
|
||||
public class UserClient {
|
||||
private final UserInterface userInterface;
|
||||
|
||||
@Inject
|
||||
public UserClient(UserInterface userInterface) {
|
||||
this.userInterface = userInterface;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if a user is currently blocked from Commons
|
||||
*
|
||||
* @return whether or not the user is blocked from Commons
|
||||
*/
|
||||
public Single<Boolean> isUserBlockedFromCommons() {
|
||||
return userInterface.getUserBlockInfo()
|
||||
.map(MwQueryResponse::query)
|
||||
.map(MwQueryResult::userInfo)
|
||||
.map(UserInfo::blockexpiry)
|
||||
.map(blockExpiry -> {
|
||||
if (blockExpiry.isEmpty())
|
||||
return false;
|
||||
else if ("infinite".equals(blockExpiry))
|
||||
return true;
|
||||
else {
|
||||
Date endDate = DateUtil.iso8601DateParse(blockExpiry);
|
||||
Date current = new Date();
|
||||
return endDate.after(current);
|
||||
}
|
||||
}).single(false);
|
||||
}
|
||||
}
|
||||
36
app/src/main/java/fr/free/nrw/commons/mwapi/UserClient.kt
Normal file
36
app/src/main/java/fr/free/nrw/commons/mwapi/UserClient.kt
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package fr.free.nrw.commons.mwapi
|
||||
|
||||
import fr.free.nrw.commons.utils.DateUtil
|
||||
import fr.free.nrw.commons.wikidata.mwapi.MwQueryResponse
|
||||
import fr.free.nrw.commons.wikidata.mwapi.MwQueryResult
|
||||
import fr.free.nrw.commons.wikidata.mwapi.UserInfo
|
||||
import io.reactivex.Single
|
||||
import java.text.ParseException
|
||||
import java.util.Date
|
||||
import javax.inject.Inject
|
||||
|
||||
class UserClient @Inject constructor(private val userInterface: UserInterface) {
|
||||
/**
|
||||
* Checks to see if a user is currently blocked from Commons
|
||||
*
|
||||
* @return whether or not the user is blocked from Commons
|
||||
*/
|
||||
fun isUserBlockedFromCommons(): Single<Boolean> =
|
||||
userInterface.getUserBlockInfo()
|
||||
.map(::processBlockExpiry)
|
||||
.single(false)
|
||||
|
||||
@Throws(ParseException::class)
|
||||
private fun processBlockExpiry(response: MwQueryResponse): Boolean {
|
||||
val blockExpiry = response.query()?.userInfo()?.blockexpiry()
|
||||
return when {
|
||||
blockExpiry.isNullOrEmpty() -> false
|
||||
"infinite" == blockExpiry -> true
|
||||
else -> {
|
||||
val endDate = DateUtil.iso8601DateParse(blockExpiry)
|
||||
val current = Date()
|
||||
endDate.after(current)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue