@@ -19,13 +19,19 @@ import androidx.activity.result.contract.ActivityResultContracts
19
19
import androidx.appcompat.app.AppCompatActivity
20
20
import androidx.lifecycle.ViewModelProvider
21
21
import androidx.lifecycle.lifecycleScope
22
+ import com.google.gson.GsonBuilder
23
+ import com.kinestex.kinestexsdkkotlin.APIContentResult
24
+ import com.kinestex.kinestexsdkkotlin.ContentType
22
25
import com.kinestex.kinestexsdkkotlin.GenericWebView
26
+ import com.kinestex.kinestexsdkkotlin.KinesteXAPI
23
27
import com.kinestex.kinestexsdkkotlin.KinesteXSDK
24
28
import com.kinestex.kinestexsdkkotlin.PermissionHandler
25
29
import com.kinestex.kinestexsdkkotlin.PlanCategory
26
30
import com.kinestex.kinestexsdkkotlin.WebViewMessage
27
31
import com.kinestex.kotlin_sdk.databinding.ActivityMainBinding
32
+ import kotlinx.coroutines.Dispatchers
28
33
import kotlinx.coroutines.launch
34
+ import kotlinx.coroutines.withContext
29
35
30
36
31
37
class MainActivity : AppCompatActivity (), PermissionHandler {
@@ -37,8 +43,8 @@ class MainActivity : AppCompatActivity(), PermissionHandler {
37
43
private val iconSubOptions = mutableListOf<ImageView >()
38
44
private var webView: GenericWebView ? = null
39
45
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 "
42
48
private val userId = " userId"
43
49
44
50
private val requestPermissionLauncher = registerForActivityResult(
@@ -111,10 +117,109 @@ class MainActivity : AppCompatActivity(), PermissionHandler {
111
117
btnTestVideo.setOnClickListener {
112
118
showHowToVideo()
113
119
}
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
+ }
114
152
115
153
}
116
154
}
117
155
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
+
118
223
private fun handleNextButton () {
119
224
createWebView()?.let { view ->
120
225
viewModel.showWebView.value = WebViewState .SUCCESS
0 commit comments