Skip to content

Instantly share code, notes, and snippets.

implementation 'com.squareup.retrofit2:retrofit:2.8.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
data class Version(
@SerializedName("documents")
var documents: List<DocumentItems>
)
data class DocumentItems(
@SerializedName("name")
var name: String,
@SerializedName("fields")
var fields: Fields,
@SerializedName("createdTime")
var createdTime: String,
@SerializedName("updateTime")
var updateTime: String,
data class Fields(
@SerializedName("app_version")
var app_version: StringValue
)
data class StringValue(
@SerializedName("stringValue")
var version: String
)
private const val BASE_URL = "https://firestore.googleapis.com/v1/projects/firestoreforceupdate/databases/"
class FirebaseClient {
// Coroutines
private val job = Job()
private val coroutineContext = Dispatchers.IO + job
private val uiScope = CoroutineScope(coroutineContext)
fun getAppVersionFromApi(callback: (isSuccess: Boolean, response: Version?, message: String?) -> (Unit)) {
object ApiClient {
private var retrofit: Retrofit? = null
fun api(url: String): Retrofit {
retrofit =
Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build()
interface ApiService {
@GET("(default)/documents/forceupdate")
fun getVersion(): Call<Version>
}
class MainActivity : AppCompatActivity() {
private val apiClient by lazy { FirebaseClient() }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
apiClient.getAppVersionFromApi { isSuccess, response, message ->
if (isSuccess) {
android {
buildFeatures {
viewBinding = true
}
}