Skip to content

Commit

Permalink
Populate custom workouts
Browse files Browse the repository at this point in the history
  • Loading branch information
amrhossamdev committed May 19, 2024
1 parent 31f3493 commit 4e80992
Show file tree
Hide file tree
Showing 17 changed files with 229 additions and 49 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/com/modarb/android/network/ApiService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.modarb.android.network
import com.modarb.android.network.models.BaseResponse
import com.modarb.android.ui.home.ui.home.domain.models.HomePageResponse
import com.modarb.android.ui.home.ui.plan.domain.models.PlanPageResponse
import com.modarb.android.ui.home.ui.plan.domain.models.customworkout.CustomWorkoutResponse
import com.modarb.android.ui.onboarding.models.LoginResponse
import com.modarb.android.ui.onboarding.models.RequestModels.LoginRequest
import com.modarb.android.ui.onboarding.models.RequestModels.RegisterRequest
Expand Down Expand Up @@ -51,7 +52,7 @@ interface ApiService {
@GET("api/v1/user/templates/")
suspend fun getCustomWorkouts(
@Header("Authorization") token: String
): Response<PlanPageResponse>
): Response<CustomWorkoutResponse>


}
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/com/modarb/android/network/NetworkHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import android.os.Build
import android.widget.Toast
import com.modarb.android.R
import com.modarb.android.network.models.BaseResponse
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.Locale

class NetworkHelper {

Expand Down Expand Up @@ -40,5 +43,17 @@ class NetworkHelper {

}

fun getDate(timestamp: String): String {
val dateTime = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LocalDateTime.parse(timestamp, DateTimeFormatter.ISO_DATE_TIME)
} else {
TODO("VERSION.SDK_INT < O")
}

val formattedDate =
dateTime.format(DateTimeFormatter.ofPattern("EEEE, d MMM", Locale.ENGLISH))
return formattedDate
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,23 @@ object WorkoutData {
return null
}

fun getTotalExerciseTime(exercise: List<com.modarb.android.ui.home.ui.plan.domain.models.customworkout.Exercise>): String {
var totalTime = 0
for (ex in exercise) {
totalTime += ex.duration
}
return totalTime.toString()
}


// TODO handle getting reps after fixing the api
// fun getTotalExerciseReps(exercise: List<com.modarb.android.ui.home.ui.plan.domain.models.customworkout.Exercise>): String {
// var totalReps = 0
// for (ex in exercise) {
// totalReps += ex.
// }
// return totalReps.toString()
// }


}
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
package com.modarb.android.ui.home.ui.plan.adapters

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.modarb.android.R
import com.modarb.android.databinding.ItemCustomworkoutTemplateBinding
import com.modarb.android.network.NetworkHelper
import com.modarb.android.ui.home.helpers.WorkoutData
import com.modarb.android.ui.home.ui.plan.domain.models.customworkout.Data

class CustomWorkoutTemplateAdapter(
private val context: Context,
private val data: MutableList<String>
private val data: List<Data>
) : RecyclerView.Adapter<CustomWorkoutTemplateAdapter.ViewHolder>() {

inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
inner class ViewHolder(val binding: ItemCustomworkoutTemplateBinding) :
RecyclerView.ViewHolder(binding.root)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(context)
.inflate(R.layout.item_customworkout_template, parent, false)
return ViewHolder(view)
val layoutInflater = LayoutInflater.from(context)
val binding = ItemCustomworkoutTemplateBinding.inflate(layoutInflater, parent, false)
return ViewHolder(binding)
}

@SuppressLint("SetTextI18n")
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = data[position]
holder.binding.workoutName.text = item.name
holder.binding.workoutDate.text = NetworkHelper.getDate(item.creationDate)
holder.binding.timeTxt.text = WorkoutData.getTotalExerciseTime(item.exercises) + " min"

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ import com.modarb.android.databinding.MyPlanViewBinding
import com.modarb.android.ui.home.helpers.WorkoutData
import com.modarb.android.ui.home.ui.plan.domain.models.Data
import com.modarb.android.ui.home.ui.plan.domain.models.PlanPageResponse
import com.modarb.android.ui.home.ui.plan.domain.models.customworkout.CustomWorkoutResponse
import com.modarb.android.ui.workout.activities.TodayWorkoutActivity
import com.modarb.android.ui.workout.adapters.TrainingWeeksAdapter

class MyPlanViewPagerAdapter(
private val context: Context, private var planResponse: PlanPageResponse
private val context: Context,
private var planResponse: PlanPageResponse,
private var customWorkoutResponse: CustomWorkoutResponse
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
Expand Down Expand Up @@ -103,14 +106,10 @@ class MyPlanViewPagerAdapter(

inner class CustomWorkoutViewHolder(private val binding: CustomWorkoutViewBinding) :
RecyclerView.ViewHolder(binding.root) {
private val data = mutableListOf<String>()

fun bind(context: Context) {
binding.recycleView.layoutManager = LinearLayoutManager(context)
for (i in 0..3) {
data.add("test")
}
val adapter = CustomWorkoutTemplateAdapter(context, data)
val adapter = CustomWorkoutTemplateAdapter(context, customWorkoutResponse.data)
binding.recycleView.adapter = adapter
binding.recycleView.isNestedScrollingEnabled = false
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
package com.modarb.android.ui.home.ui.plan.data

import android.content.Context
import com.modarb.android.network.ApiResult
import com.modarb.android.network.ApiService
import com.modarb.android.ui.home.ui.plan.domain.MyPlanRepository
import com.modarb.android.ui.home.ui.plan.domain.models.PlanPageResponse
import com.modarb.android.ui.onboarding.utils.UserPref.UserPrefUtil
import retrofit2.Response
import com.modarb.android.ui.home.ui.plan.domain.models.customworkout.CustomWorkoutResponse

class PlanRepositoryImp(private val apiService: ApiService) : MyPlanRepository {


// TODO handle get custom workouts
suspend fun getCustomWorkouts(context: Context): Response<PlanPageResponse> {
return apiService.getCustomWorkouts(
"Bearer " + UserPrefUtil.getUserData(context)!!.token
)
}

override suspend fun getMyPlanPage(
workoutId: String,
token: String
workoutId: String, token: String
): ApiResult<PlanPageResponse> {

return try {
Expand All @@ -37,5 +26,21 @@ class PlanRepositoryImp(private val apiService: ApiService) : MyPlanRepository {
}
}

override suspend fun getCustomWorkouts(token: String): ApiResult<CustomWorkoutResponse> {
return try {
val response = apiService.getCustomWorkouts(token)
if (response.isSuccessful) {
response.body()?.let {
ApiResult.Success(it)
} ?: ApiResult.Failure(Throwable("Response body is null"))
} else {
ApiResult.Error(response.body() as CustomWorkoutResponse)

}
} catch (e: Exception) {
ApiResult.Failure(e)
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.modarb.android.ui.home.ui.plan.domain

import com.modarb.android.network.ApiResult
import com.modarb.android.ui.home.ui.plan.domain.models.PlanPageResponse
import com.modarb.android.ui.home.ui.plan.domain.models.customworkout.CustomWorkoutResponse

interface MyPlanRepository {

suspend fun getMyPlanPage(workoutId: String, token: String): ApiResult<PlanPageResponse>
suspend fun getCustomWorkouts(token: String): ApiResult<CustomWorkoutResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.modarb.android.ui.home.ui.plan.domain.models.customworkout

data class CustomWorkoutResponse(
val `data`: List<Data>,
val message: String,
val meta: Meta,
val status: Int
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.modarb.android.ui.home.ui.plan.domain.models.customworkout

data class Data(
val creationDate: String,
val exercises: List<Exercise>,
val id: String,
val name: String,
val user: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.modarb.android.ui.home.ui.plan.domain.models.customworkout

data class Exercise(
val __v: Int,
val _id: String,
val benefits: String,
val category: String,
val coverImage: String,
val duration: Int,
val equipments: List<String>,
val exerciseType: String,
val expectedDurationRange: ExpectedDurationRange,
val instructions: String,
val media: Media,
val name: String,
val targetMuscles: TargetMuscles
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.modarb.android.ui.home.ui.plan.domain.models.customworkout

data class ExpectedDurationRange(
val max: Int,
val min: Int
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.modarb.android.ui.home.ui.plan.domain.models.customworkout

data class Media(
val type: String,
val url: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.modarb.android.ui.home.ui.plan.domain.models.customworkout

data class Meta(
val page: Int,
val perPage: Int,
val total: Int
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.modarb.android.ui.home.ui.plan.domain.models.customworkout

data class TargetMuscles(
val primary: String,
val secondary: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.modarb.android.ui.home.ui.plan.domain.usecase

import com.modarb.android.network.ApiResult
import com.modarb.android.ui.home.ui.plan.domain.MyPlanRepository
import com.modarb.android.ui.home.ui.plan.domain.models.customworkout.CustomWorkoutResponse

class GetCustomWorkoutUseCase(private var planRepository: MyPlanRepository) {
suspend fun invoke(token: String): ApiResult<CustomWorkoutResponse> {
return planRepository.getCustomWorkouts(token)
}
}
Loading

0 comments on commit 4e80992

Please sign in to comment.