This repository has been archived by the owner on Oct 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 828
Converted classes to Kotlin #2405
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
95 changes: 0 additions & 95 deletions
95
android/app/src/main/java/org/fossasia/openevent/common/api/JWTUtils.java
This file was deleted.
Oops, something went wrong.
101 changes: 101 additions & 0 deletions
101
android/app/src/main/java/org/fossasia/openevent/common/api/JWTUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package org.fossasia.openevent.common.api | ||
|
||
import android.support.v4.util.SparseArrayCompat | ||
|
||
import org.json.JSONException | ||
import org.json.JSONObject | ||
|
||
object JWTUtils { | ||
|
||
private fun decode(token: String): SparseArrayCompat<String> { | ||
val decoded = SparseArrayCompat<String>(2) | ||
|
||
val split = token.split("\\.".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() | ||
decoded.append(0, getJson(split[0])) | ||
decoded.append(1, getJson(split[1])) | ||
|
||
return decoded | ||
} | ||
|
||
@Throws(JSONException::class) | ||
private fun getExpiry(token: String): Long { | ||
val decoded = decode(token) | ||
|
||
// We are using JSONObject instead of GSON as it takes about 5 ms instead of 150 ms taken by GSON | ||
return JSONObject(decoded.get(1)).get("exp").toString().toLong() | ||
} | ||
|
||
@Throws(JSONException::class) | ||
@JvmStatic | ||
fun getIdentity(token: String): Int { | ||
val decoded = decode(token) | ||
|
||
return JSONObject(decoded.get(1)).get("identity").toString().toInt() | ||
} | ||
|
||
@JvmStatic | ||
fun isExpired(token: String): Boolean { | ||
val expiry: Long | ||
|
||
try { | ||
expiry = getExpiry(token) | ||
} catch (jse: JSONException) { | ||
return true | ||
} | ||
|
||
return System.currentTimeMillis() / 1000 >= expiry | ||
} | ||
|
||
private fun getJson(strEncoded: String): String { | ||
val decodedBytes = Base64Utils.decode(strEncoded) | ||
return String(decodedBytes) | ||
} | ||
|
||
/** | ||
* Base64 class because we can't test Android class and this is faster | ||
*/ | ||
private object Base64Utils { | ||
|
||
private val ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray() | ||
|
||
private val toInt = IntArray(128) | ||
|
||
init { | ||
for (i in ALPHABET.indices) { | ||
toInt[ALPHABET[i].toInt()] = i | ||
} | ||
} | ||
|
||
/** | ||
* Translates the specified Base64 string into a byte array. | ||
* | ||
* @param s the Base64 string (not null) | ||
* @return the byte array (not null) | ||
*/ | ||
fun decode(s: String): ByteArray { | ||
val delta = if (s.endsWith("==")) 2 else if (s.endsWith("=")) 1 else 0 | ||
val buffer = ByteArray(s.length * 3 / 4 - delta) | ||
val mask = 0xFF | ||
var index = 0 | ||
var i = 0 | ||
while (i < s.length) { | ||
val c0 = toInt[s[i].toInt()] | ||
val c1 = toInt[s[i + 1].toInt()] | ||
buffer[index++] = (c0 shl 2 or (c1 shr 4) and mask).toByte() | ||
if (index >= buffer.size) { | ||
return buffer | ||
} | ||
val c2 = toInt[s[i + 2].toInt()] | ||
buffer[index++] = (c1 shl 4 or (c2 shr 2) and mask).toByte() | ||
if (index >= buffer.size) { | ||
return buffer | ||
} | ||
val c3 = toInt[s[i + 3].toInt()] | ||
buffer[index++] = (c2 shl 6 or c3 and mask).toByte() | ||
i += 4 | ||
} | ||
return buffer | ||
} | ||
|
||
} | ||
} |
85 changes: 0 additions & 85 deletions
85
android/app/src/main/java/org/fossasia/openevent/common/api/Urls.java
This file was deleted.
Oops, something went wrong.
85 changes: 85 additions & 0 deletions
85
android/app/src/main/java/org/fossasia/openevent/common/api/Urls.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package org.fossasia.openevent.common.api | ||
|
||
import android.webkit.URLUtil | ||
|
||
import org.fossasia.openevent.common.utils.Utils | ||
|
||
object Urls { | ||
|
||
const val API_VERSION = "v1" | ||
|
||
/** | ||
* Change EVENT Id Here * | ||
*/ | ||
const val EVENT_ID = 1 | ||
|
||
const val WEB_APP_URL_BASIC = "http://fossasia.github.io/open-event-webapp/#/" | ||
|
||
const val EVENT = "event" | ||
|
||
const val SPEAKERS = "speakers" | ||
|
||
const val TRACKS = "tracks" | ||
|
||
const val SESSIONS = "sessions" | ||
|
||
const val SPONSORS = "sponsors" | ||
|
||
const val BOOKMARKS = "bookmarks" | ||
|
||
const val MICROLOCATIONS = "microlocations" | ||
|
||
const val SESSION_TYPES = "session_types" | ||
|
||
const val MAP = "map" | ||
|
||
@JvmField | ||
var BASE_URL = "https://eventyay.com/api/v1/events/6" | ||
|
||
const val FACEBOOK_BASE_URL = "https://graph.facebook.com" | ||
|
||
const val LOKLAK_BASE_URL = "https://api.loklak.org" | ||
|
||
@JvmField | ||
val BASE_GET_URL = "$BASE_URL/api/$API_VERSION" | ||
|
||
const val BASE_GET_URL_ALT = "https://raw.githubusercontent.com/fossasia/open-event/master/testapi/" | ||
|
||
// Replace the template in getAppLink() if changing it | ||
private const val APP_LINK = "https://app_link_goes_here.com" | ||
|
||
const val INVALID_LINK = "http://abc//" | ||
|
||
const val EMPTY_LINK = "http://xyz//" | ||
|
||
private const val GOOGLE_PLAY_HOME = "https://play.google.com/store" | ||
|
||
var baseUrl: String | ||
@JvmStatic | ||
get() = BASE_URL | ||
@JvmStatic | ||
set(baseUrl) = if (URLUtil.isValidUrl(baseUrl)) { | ||
BASE_URL = if (!baseUrl.endsWith("/")) { | ||
"$baseUrl/" | ||
} else { | ||
baseUrl | ||
} | ||
} else { | ||
BASE_URL = if (!Utils.isEmpty(baseUrl)) { | ||
INVALID_LINK | ||
} else { | ||
EMPTY_LINK | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the app link is replaced by the generator, if not | ||
* returns null which is to be checked by caller to make decision | ||
* accordingly. | ||
* @return String | ||
*/ | ||
val appLink: String | ||
@JvmStatic | ||
get() = if (APP_LINK == "https://app_link_goes_here.com") GOOGLE_PLAY_HOME else APP_LINK | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not ?.eventBus
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because I could not set a default getter implementation because the getter referenced the eventBus and that turned into infinite recursion. So I had to create an explicit getEventBus method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would it recur? That doesn't make sense
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason, accessing the property from inside the getter calls the getter again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you accessing it inside getter, of course it will recur that way
It's like saying
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, android studio showed the error. But I found a solution using the 'field' naming.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you using that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Becuase we need to do this
if (field == null) {
field = Bus()
}
return field
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use lazy delegator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need the lazy approach here. We can just use the field attribute.