Skip to content

Commit

Permalink
Add ProUser data class
Browse files Browse the repository at this point in the history
  • Loading branch information
atavism committed Oct 23, 2023
1 parent 55e5162 commit 325cf36
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 231 deletions.

This file was deleted.

164 changes: 0 additions & 164 deletions android/app/src/main/java/org/getlantern/lantern/model/ProUser.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ class MainActivity :

override fun onDestroy() {
super.onDestroy()
if (accountInitDialog != null) {
accountInitDialog.dismiss()
}

vpnModel.destroy()
sessionModel.destroy()
replicaModel.destroy()
Expand Down Expand Up @@ -314,7 +318,7 @@ class MainActivity :
}

override fun onSuccess(response: Response, user: ProUser) {
val devices = user?.getDevices()
val devices = user?.devices
val deviceID = LanternApp.getSession().deviceID()
// if the payment test mode is enabled
// then do nothing To avoid restarting app while debugging
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ class LanternSessionManager(application: Application) : SessionManager(applicati

if (user.isProUser) {
EventBus.getDefault().post(UserStatus(user.isActive, user.monthsLeft().toLong()))
prefs.edit().putInt(PRO_MONTHS_LEFT, user.monthsLeft())
.putInt(PRO_DAYS_LEFT, user.daysLeft())
prefs.edit().putInt(PRO_MONTHS_LEFT, user.monthsLeft() ?: 0)
.putInt(PRO_DAYS_LEFT, user.daysLeft() ?: 0)
.apply()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.getlantern.lantern.model

import com.google.gson.JsonObject

data class ProError(
val id: String,
val message: String,
val details: JsonObject? = null
) {
constructor(result: JsonObject) : this(
result.get("errorId").asString,
result.get("error").asString,
result.get("details").asJsonObject,
) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.getlantern.lantern.model

import com.google.gson.JsonObject
import org.joda.time.Days
import org.joda.time.Months
import org.joda.time.LocalDateTime

data class ProUser(
val userId: Long,
val token: String,
val referral: String,
val email: String,
val userStatus: String,
val code: String,
val subscription: String,
val expiration: Long,
val devices: List<Device>,
val userLevel: String
) {

private fun isUserStatus(status: String) = userStatus == status

private fun expirationDate() = if (expiration == null) null else LocalDateTime(expiration * 1000)

fun monthsLeft(): Int {
val expDate = expirationDate()
if (expDate == null) return 0
return Months.monthsBetween(LocalDateTime.now(), expDate).getMonths()
}

fun daysLeft(): Int {
val expDate = expirationDate()
if (expDate == null) return 0
return Days.daysBetween(LocalDateTime.now(), expDate).getDays()
}

fun newUserDetails(): String {
return "User ID $userId referral $referral"
}

val isProUser: Boolean
get() = isUserStatus("active")

val isActive: Boolean
get() = isProUser

val isExpired: Boolean
get() = isUserStatus("expired")
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ open class LanternService : Service(), Runnable {
private val started: AtomicBoolean = AtomicBoolean()
private lateinit var autoUpdater: AutoUpdater

override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (intent == null) return START_NOT_STICKY
autoUpdater = AutoUpdater(this)
val autoBooted = intent.getBooleanExtra(AUTO_BOOTED, false)
Logger.d(TAG, "Called onStartCommand, autoBooted?: $autoBooted")
Expand Down Expand Up @@ -163,8 +164,8 @@ open class LanternService : Service(), Runnable {
}
service.createUserHandler.removeCallbacks(service.createUserRunnable)
Logger.debug(TAG, "Created new Lantern user: ${user.newUserDetails()}")
LanternApp.getSession().setUserIdAndToken(user.getUserId(), user.getToken())
val referral = user.getReferral()
LanternApp.getSession().setUserIdAndToken(user.userId, user.token)
val referral = user.referral
if (!referral.isEmpty()) {
LanternApp.getSession().setCode(referral)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import okhttp3.Response
import org.getlantern.lantern.LanternApp
import org.getlantern.mobilesdk.Logger
import org.getlantern.mobilesdk.util.HttpClient
import org.json.JSONObject
import java.io.IOException

// An OkHttp-Based HTTP client for communicating with the Pro server
Expand Down Expand Up @@ -60,7 +61,7 @@ open class LanternHttpClient : HttpClient() {
override fun onSuccess(response: Response?, result: JsonObject?) {
Logger.debug(TAG, "JSON response" + result.toString())
result?.let {
val user = parseData<ProUser>(result.asString)
val user = parseData<ProUser>(result.toString())
Logger.debug(TAG, "User ID is ${user.userId}")
LanternApp.getSession().storeUserData(user)
}
Expand Down Expand Up @@ -191,26 +192,26 @@ open class LanternHttpClient : HttpClient() {
}

override fun onResponse(call: Call, response: Response) {
if (response.isSuccessful) {
val responseData = response.body.toString()
response.use {
if (!response.isSuccessful) {
val error = ProError("", "Unexpected response code from server $response")
cb.onFailure(null, error)
return
}
val responseData = response.body!!.string()
Logger.d(TAG, "Response body " + responseData)
val result = JsonParser().parse(responseData).asJsonObject
if (result.get("error") != null) {
val error = result.get("error").asString
Logger.error(TAG, "Error making request to $url: $result error: $error")
cb.onFailure(null, ProError(result))
if (result == null) {
return
} else if (result.get("error") != null) {
var error = result.get("error").asString
error = "Error making request to $url: $result error: $error"
Logger.error(TAG, error)
cb.onFailure(null, ProError("", error))
return
}
cb.onSuccess(response, result)
return
}
Logger.error(TAG, "Request to $url failed")
Logger.error(TAG, "Response: $response")
val responseBody = response.body
if (responseBody != null) {
Logger.error(TAG, "Body: $responseBody")
}
val error = ProError("", "Unexpected response code from server")
cb.onFailure(null, error)
}
})
}
Expand Down

0 comments on commit 325cf36

Please sign in to comment.