Skip to content

Commit b78ea9a

Browse files
committed
added content api support
1 parent 80c1c7b commit b78ea9a

File tree

8 files changed

+478
-3
lines changed

8 files changed

+478
-3
lines changed

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ android {
4040
dependencies {
4141
implementation(project(":kinestexsdkkotlin"))
4242
implementation("androidx.core:core-ktx:1.9.0")
43+
implementation("com.google.code.gson:gson:2.8.8")
4344
implementation("androidx.appcompat:appcompat:1.6.1")
4445
implementation("com.google.android.material:material:1.11.0")
4546
implementation("androidx.constraintlayout:constraintlayout:2.1.4")

app/src/main/java/com/kinestex/kotlin_sdk/MainActivity.kt

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,19 @@ import androidx.activity.result.contract.ActivityResultContracts
1919
import androidx.appcompat.app.AppCompatActivity
2020
import androidx.lifecycle.ViewModelProvider
2121
import androidx.lifecycle.lifecycleScope
22+
import com.google.gson.GsonBuilder
23+
import com.kinestex.kinestexsdkkotlin.APIContentResult
24+
import com.kinestex.kinestexsdkkotlin.ContentType
2225
import com.kinestex.kinestexsdkkotlin.GenericWebView
26+
import com.kinestex.kinestexsdkkotlin.KinesteXAPI
2327
import com.kinestex.kinestexsdkkotlin.KinesteXSDK
2428
import com.kinestex.kinestexsdkkotlin.PermissionHandler
2529
import com.kinestex.kinestexsdkkotlin.PlanCategory
2630
import com.kinestex.kinestexsdkkotlin.WebViewMessage
2731
import com.kinestex.kotlin_sdk.databinding.ActivityMainBinding
32+
import kotlinx.coroutines.Dispatchers
2833
import kotlinx.coroutines.launch
34+
import kotlinx.coroutines.withContext
2935

3036

3137
class MainActivity : AppCompatActivity(), PermissionHandler {
@@ -37,8 +43,8 @@ class MainActivity : AppCompatActivity(), PermissionHandler {
3743
private val iconSubOptions = mutableListOf<ImageView>()
3844
private var webView: GenericWebView? = null
3945

40-
private val apiKey = "apiKey" // store this key securely
41-
private val company = "company"
46+
private val apiKey = "your_api_key" // store this key securely
47+
private val company = "your_company_name"
4248
private val userId = "userId"
4349

4450
private val requestPermissionLauncher = registerForActivityResult(
@@ -111,10 +117,109 @@ class MainActivity : AppCompatActivity(), PermissionHandler {
111117
btnTestVideo.setOnClickListener {
112118
showHowToVideo()
113119
}
120+
btnApiRequest.setOnClickListener {
121+
lifecycleScope.launch {
122+
try {
123+
// Show loading state if needed
124+
// binding.progressBar.visibility = View.VISIBLE
125+
126+
// Switch to IO dispatcher for network request
127+
val result = withContext(Dispatchers.IO) {
128+
fetchContent(apiKey, company, contentType = ContentType.PLAN, title = "Circuit Training")
129+
130+
// FOR FETCHING WORKOUT
131+
// fetchContent(apiKey, company, contentType = ContentType.WORKOUT, title = "Fitness Lite")
132+
133+
// FOR FETCHING EXERCISE
134+
// fetchContent(apiKey, company, contentType = ContentType.EXERCISE, title = "Squats")
135+
}
136+
137+
// Handle the result on the main thread
138+
handleAPIResult(result)
139+
} catch (e: Exception) {
140+
// Handle any errors
141+
Toast.makeText(
142+
this@MainActivity,
143+
"Error: ${e.message}",
144+
Toast.LENGTH_LONG
145+
).show()
146+
} finally {
147+
// Hide loading state if needed
148+
// binding.progressBar.visibility = View.GONE
149+
}
150+
}
151+
}
114152

115153
}
116154
}
117155

156+
private suspend fun fetchContent(apiKey: String, companyName: String, contentType: ContentType, id: String? = null, title: String? = null): APIContentResult {
157+
return KinesteXAPI.fetchAPIContentData(
158+
apiKey = apiKey,
159+
companyName = companyName,
160+
contentType = contentType,
161+
title = title,
162+
id = id
163+
)
164+
}
165+
166+
private fun handleAPIResult(result: APIContentResult) {
167+
when (result) {
168+
is APIContentResult.Workout -> {
169+
// Handle successful workout data
170+
val workout = result.workout
171+
// Create a Gson instance with pretty printing
172+
val gson = GsonBuilder().setPrettyPrinting().create()
173+
174+
// Convert the workout object to a pretty JSON string
175+
val prettyJson = gson.toJson(workout)
176+
177+
// Print the formatted JSON string
178+
println("Workout Data:\n$prettyJson")
179+
}
180+
is APIContentResult.Plan -> {
181+
// Handle successful workout data
182+
val workout = result.plan
183+
// Create a Gson instance with pretty printing
184+
val gson = GsonBuilder().setPrettyPrinting().create()
185+
186+
// Convert the workout object to a pretty JSON string
187+
val prettyJson = gson.toJson(workout)
188+
189+
// Print the formatted JSON string
190+
println("Plan Data:\n$prettyJson")
191+
}
192+
is APIContentResult.Exercise -> {
193+
// Handle successful workout data
194+
val workout = result.exercise
195+
// Create a Gson instance with pretty printing
196+
val gson = GsonBuilder().setPrettyPrinting().create()
197+
198+
// Convert the workout object to a pretty JSON string
199+
val prettyJson = gson.toJson(workout)
200+
201+
// Print the formatted JSON string
202+
println("Exercise Data:\n$prettyJson")
203+
}
204+
is APIContentResult.Error -> {
205+
// Handle error
206+
Toast.makeText(
207+
this,
208+
"Error: ${result.message}",
209+
Toast.LENGTH_LONG
210+
).show()
211+
}
212+
else -> {
213+
// Handle unexpected result type
214+
Toast.makeText(
215+
this,
216+
"Unexpected result type",
217+
Toast.LENGTH_LONG
218+
).show()
219+
}
220+
}
221+
}
222+
118223
private fun handleNextButton() {
119224
createWebView()?.let { view ->
120225
viewModel.showWebView.value = WebViewState.SUCCESS

app/src/main/res/layout/activity_main.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,14 @@
277277
android:layout_marginHorizontal="20dp"
278278
android:text="how to video"
279279
android:visibility="visible" />
280+
<Button
281+
android:layout_marginTop="20dp"
282+
android:id="@+id/btn_api_request"
283+
android:layout_width="match_parent"
284+
android:layout_height="wrap_content"
285+
android:layout_marginHorizontal="20dp"
286+
android:text="Send API Request"
287+
android:visibility="visible" />
280288
</LinearLayout>
281289

282290
<FrameLayout

gradle/libs.versions.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[versions]
22

33
agp = "8.2.1"
4+
gson = "2.10.1"
45
kotlin = "1.9.0"
56
coreKtx = "1.12.0"
67
junit = "4.13.2"
@@ -11,19 +12,22 @@ material = "1.11.0"
1112
activity = "1.8.0"
1213
constraintlayout = "2.1.4"
1314
media3Exoplayer = "1.4.1"
15+
okhttp = "4.9.3"
1416

1517
[libraries]
1618
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
1719
androidx-media3-exoplayer = { module = "androidx.media3:media3-exoplayer", version.ref = "media3Exoplayer" }
1820
androidx-media3-exoplayer-dash = { module = "androidx.media3:media3-exoplayer-dash", version.ref = "media3Exoplayer" }
1921
androidx-media3-ui = { module = "androidx.media3:media3-ui", version.ref = "media3Exoplayer" }
22+
gson = { module = "com.google.code.gson:gson", version.ref = "gson" }
2023
junit = { group = "junit", name = "junit", version.ref = "junit" }
2124
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
2225
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
2326
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
2427
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
2528
androidx-activity = { group = "androidx.activity", name = "activity", version.ref = "activity" }
2629
androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
30+
okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" }
2731

2832
[plugins]
2933
androidApplication = { id = "com.android.application", version.ref = "agp" }

kinestexsdkkotlin/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ dependencies {
8181
testImplementation(libs.junit)
8282
implementation(libs.androidx.core.ktx)
8383
implementation(libs.androidx.appcompat)
84+
implementation(libs.okhttp)
85+
implementation(libs.gson)
8486
// ExoPlayer (Media3) dependencies
8587
implementation(libs.androidx.media3.exoplayer)
8688
implementation(libs.androidx.media3.exoplayer.dash)

0 commit comments

Comments
 (0)