Skip to content

Commit

Permalink
Refactor plan to follow clean architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
amrhossamdev committed May 18, 2024
1 parent 08032fa commit 5ced33b
Show file tree
Hide file tree
Showing 32 changed files with 206 additions and 169 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/modarb/android/network/ApiService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,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.models.PlanPageResponse
import com.modarb.android.ui.home.ui.plan.domain.models.PlanPageResponse
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.modarb.android.ui.home.helpers

import com.modarb.android.ui.home.ui.plan.models.Day
import com.modarb.android.ui.home.ui.plan.models.Exercise
import com.modarb.android.ui.home.ui.plan.models.Week
import com.modarb.android.ui.home.ui.plan.domain.models.Day
import com.modarb.android.ui.home.ui.plan.domain.models.Exercise
import com.modarb.android.ui.home.ui.plan.domain.models.Week

object WorkoutData {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,16 @@ import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.modarb.android.R
import com.modarb.android.databinding.FragmentHomeBinding
import com.modarb.android.network.Result
import com.modarb.android.network.RetrofitService
import com.modarb.android.ui.home.helpers.WorkoutData
import com.modarb.android.ui.home.ui.home.domain.models.HomePageResponse
import com.modarb.android.ui.home.ui.home.presentation.HomeViewModel
import com.modarb.android.ui.home.ui.plan.logic.PlanRepository
import com.modarb.android.ui.home.ui.plan.logic.PlanViewModel
import com.modarb.android.ui.home.ui.plan.logic.PlanViewModelFactory
import com.modarb.android.ui.home.ui.plan.domain.models.PlanPageResponse
import com.modarb.android.ui.home.ui.plan.persentation.PlanViewModel
import com.modarb.android.ui.onboarding.activities.SplashActivity
import com.modarb.android.ui.onboarding.utils.UserPref.UserPrefUtil
import com.modarb.android.ui.workout.activities.TodayWorkoutActivity
Expand All @@ -32,6 +29,8 @@ import kotlinx.coroutines.launch
class HomeFragment : Fragment() {

private val homeViewModel: HomeViewModel by viewModels()
private val planViewModel: PlanViewModel by viewModels()


private lateinit var binding: FragmentHomeBinding

Expand Down Expand Up @@ -73,44 +72,56 @@ class HomeFragment : Fragment() {
lifecycleScope.launch {
homeViewModel.homeResponse.collect {
when (it) {
is Result.Success<*> -> handleSuccess(it.data as HomePageResponse)
is Result.Failure -> handleError(it.exception)
is Result.Success<*> -> handleHomeSuccess(it.data as HomePageResponse)
is Result.Failure -> handleHomeError(it.exception)
else -> {}
}
}
}

}

private fun handleSuccess(res: HomePageResponse) {
private fun handleHomeSuccess(res: HomePageResponse) {
WorkoutData.workoutId = res.data.myWorkout.id
setData(res)
getPlanData()
binding.progressView.progressOverlay.visibility = View.GONE
}

private fun handleError(exception: Throwable) {
private fun handleHomeError(exception: Throwable) {
Toast.makeText(requireContext(), exception.message, Toast.LENGTH_SHORT).show()
binding.progressView.progressOverlay.visibility = View.GONE
}

private fun getPlanData() {
val planRepository = PlanRepository(RetrofitService.createService())
val planViewModel = ViewModelProvider(
this, PlanViewModelFactory(planRepository)
)[PlanViewModel::class.java]

planViewModel.getPlanPage(requireContext())
planViewModel.planResponse.observe(this) { response ->
RetrofitService.handleRequest(response = response, onSuccess = { res ->
WorkoutData.weekList = planViewModel.planResponse.value?.body()?.data!!.weeks
}, onError = { errorResponse ->
val defaultErrorMessage = getString(R.string.an_error_occurred)
val message = errorResponse?.errors?.firstOrNull() ?: errorResponse?.error
?: defaultErrorMessage
Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show()
})

lifecycleScope.launch {
planViewModel.planResponse.collect {
when (it) {
is Result.Success<*> -> handlePlanSuccess(it.data as PlanPageResponse)
is Result.Failure -> handlePlanError(it.exception)
else -> {}
}
}
}
planViewModel.getPlanPage(
WorkoutData.workoutId, "Bearer " + UserPrefUtil.getUserData(requireContext())!!.token
)

}

private fun handlePlanError(exception: Throwable) {

// TODO handle plan error
// val defaultErrorMessage = getString(R.string.an_error_occurred)
// val message = errResponse.errors?.firstOrNull() ?: errResponse.error ?: defaultErrorMessage
// Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show()


}

private fun handlePlanSuccess(res: PlanPageResponse) {
WorkoutData.weekList = res.data.weeks
}

private fun initActions() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.modarb.android.ui.home.ui.home.domain.models

import com.modarb.android.ui.home.ui.plan.models.Week
import com.modarb.android.ui.home.ui.plan.domain.models.Week

data class MyWorkout(
val id: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.modarb.android.R
import com.modarb.android.databinding.CustomWorkoutViewBinding
import com.modarb.android.databinding.MyPlanViewBinding
import com.modarb.android.ui.home.ui.plan.logic.PlanViewModel
import com.modarb.android.ui.home.ui.plan.models.Data
import com.modarb.android.ui.home.ui.plan.models.Day
import com.modarb.android.ui.home.ui.plan.domain.models.Data
import com.modarb.android.ui.home.ui.plan.domain.models.Day
import com.modarb.android.ui.home.ui.plan.domain.models.PlanPageResponse
import com.modarb.android.ui.workout.activities.TodayWorkoutActivity
import com.modarb.android.ui.workout.adapters.TrainingWeeksAdapter

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

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
Expand All @@ -27,9 +26,11 @@ class MyPlanViewPagerAdapter(
0 -> MyPlanViewHolder(
MyPlanViewBinding.inflate(inflater, parent, false)
)

1 -> CustomWorkoutViewHolder(
CustomWorkoutViewBinding.inflate(inflater, parent, false)
)

else -> throw IllegalArgumentException("Invalid view type")
}
}
Expand All @@ -55,20 +56,14 @@ class MyPlanViewPagerAdapter(
binding.recyclerView.layoutManager = LinearLayoutManager(context)
if (!::adapter.isInitialized) {
adapter = TrainingWeeksAdapter(
viewModel.planResponse.value?.body()?.data!!.weeks,
context
planResponse.data.weeks, context
)
binding.recyclerView.adapter = adapter
}
}

private fun loadDataIntoViews(context: Context) {
viewModel.planResponse.value?.body()?.data?.let { data ->
updateTextViews(data, context)
} ?: run {
Toast.makeText(context, context.getString(R.string.error), Toast.LENGTH_SHORT)
.show()
}
updateTextViews(planResponse.data, context)
}

@SuppressLint("SetTextI18n")
Expand All @@ -90,7 +85,7 @@ class MyPlanViewPagerAdapter(
}

private fun getTodayWorkout(): Day? {
val weekList = viewModel.planResponse.value!!.body()!!.data.weeks
val weekList = planResponse.data.weeks
for (week in weekList) {
if (!week.is_done) {
for (day in week.days) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.modarb.android.ui.home.ui.plan.data

import android.content.Context
import com.modarb.android.network.ApiService
import com.modarb.android.network.Result
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

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): Result<PlanPageResponse> {

return try {
val response = apiService.getPlanPage(workoutId, token)
if (response.isSuccessful) {
response.body()?.let {
Result.Success(it)
} ?: Result.Failure(Throwable("Response body is null"))
} else {
Result.Failure(Throwable(response.errorBody()?.string() ?: "Unknown error"))
}
} catch (e: Exception) {
Result.Failure(e)
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.modarb.android.ui.home.ui.plan.domain

import com.modarb.android.network.Result
import com.modarb.android.ui.home.ui.plan.domain.models.PlanPageResponse

interface MyPlanRepository {

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

data class Data(
val id: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.modarb.android.ui.home.ui.plan.models
package com.modarb.android.ui.home.ui.plan.domain.models

data class Day(
val day_number: Int,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.modarb.android.ui.home.ui.plan.models
package com.modarb.android.ui.home.ui.plan.domain.models

data class Equipment(
val __v: Int,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.modarb.android.ui.home.ui.plan.models
package com.modarb.android.ui.home.ui.plan.domain.models

data class Exercise(
val __v: Int,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.modarb.android.ui.home.ui.plan.models
package com.modarb.android.ui.home.ui.plan.domain.models

data class ExpectedDurationRange(
val max: Int,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.modarb.android.ui.home.ui.plan.models
package com.modarb.android.ui.home.ui.plan.domain.models

data class Media(
val type: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.modarb.android.ui.home.ui.plan.models
package com.modarb.android.ui.home.ui.plan.domain.models

import com.modarb.android.network.models.BaseResponse

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.modarb.android.ui.home.ui.plan.models
package com.modarb.android.ui.home.ui.plan.domain.models

data class Primary(
val __v: Int,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.modarb.android.ui.home.ui.plan.models
package com.modarb.android.ui.home.ui.plan.domain.models

data class Secondary(
val __v: Int,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.modarb.android.ui.home.ui.plan.models
package com.modarb.android.ui.home.ui.plan.domain.models

data class TargetMuscles(
val primary: Primary,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.modarb.android.ui.home.ui.plan.models
package com.modarb.android.ui.home.ui.plan.domain.models

data class Week(
val days: List<Day>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.modarb.android.ui.home.ui.plan.models
package com.modarb.android.ui.home.ui.plan.domain.models

data class Workout(
val created_by: String,
Expand Down
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.Result
import com.modarb.android.ui.home.ui.plan.domain.MyPlanRepository
import com.modarb.android.ui.home.ui.plan.domain.models.PlanPageResponse

class PlanPageUseCase(private val planRepository: MyPlanRepository) {
suspend fun invoke(workoutId: String, token: String): Result<PlanPageResponse> {
return planRepository.getMyPlanPage(workoutId, token)
}
}
Loading

0 comments on commit 5ced33b

Please sign in to comment.