Skip to content

Commit

Permalink
Refactor home 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 ab05695 commit 08032fa
Show file tree
Hide file tree
Showing 20 changed files with 138 additions and 125 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
@@ -1,7 +1,7 @@
package com.modarb.android.network

import com.modarb.android.network.models.BaseResponse
import com.modarb.android.ui.home.ui.home.models.HomePageResponse
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.onboarding.models.LoginResponse
import com.modarb.android.ui.onboarding.models.RequestModels.LoginRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,20 @@ object WorkoutData {
return null
}


fun getTodayWorkout(weekList: List<Week>): Day? {
for (week in weekList) {
if (!week.is_done) {
for (day in week.days) {
if (!day.is_done) {
return day
}
}
break
}
}
return null
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,28 @@ import android.view.View
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.logic.HomeRepository
import com.modarb.android.ui.home.ui.home.logic.HomeViewModel
import com.modarb.android.ui.home.ui.home.logic.HomeViewModelFactory
import com.modarb.android.ui.home.ui.home.models.Day
import com.modarb.android.ui.home.ui.home.models.HomePageResponse
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.onboarding.activities.SplashActivity
import com.modarb.android.ui.onboarding.utils.UserPref.UserPrefUtil
import com.modarb.android.ui.workout.activities.TodayWorkoutActivity
import kotlinx.coroutines.launch

class HomeFragment : Fragment() {

lateinit var viewModel: HomeViewModel
private val homeViewModel: HomeViewModel by viewModels()

private lateinit var binding: FragmentHomeBinding

Expand All @@ -39,15 +40,12 @@ class HomeFragment : Fragment() {
): View {
binding = FragmentHomeBinding.inflate(inflater, container, false)
val root: View = binding.root
initViewModels()
getHomeData()
initLogout()
initActions()
handleClick()



Log.e("User ID", UserPrefUtil.getUserData(requireContext())!!.user.id)
Log.d("User ID", UserPrefUtil.getUserData(requireContext())!!.user.id)
return root
}

Expand All @@ -57,6 +55,7 @@ class HomeFragment : Fragment() {
}
}


private fun initLogout() {
binding.profileBtn.setOnClickListener {
UserPrefUtil.saveUserData(requireContext(), null)
Expand All @@ -65,47 +64,34 @@ class HomeFragment : Fragment() {
}
}

private fun initViewModels() {
val homeRepository = HomeRepository(RetrofitService.createService())
viewModel = ViewModelProvider(
this, HomeViewModelFactory(homeRepository)
)[HomeViewModel::class.java]
}

private fun getTodayWorkout(): Day? {
val weekList = viewModel.homeResponse.value!!.body()!!.data.myWorkout.weeks
private fun getHomeData() {
binding.progressView.progressOverlay.visibility = View.VISIBLE
homeViewModel.getUserHomePage("Bearer " + UserPrefUtil.getUserData(requireContext())!!.token)


for (week in weekList) {
if (!week.is_done) {
for (day in week.days) {
if (!day.is_done) {
return day
}
lifecycleScope.launch {
homeViewModel.homeResponse.collect {
when (it) {
is Result.Success<*> -> handleSuccess(it.data as HomePageResponse)
is Result.Failure -> handleError(it.exception)
else -> {}
}
break
}
}
return null
}

}

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

viewModel.homeResponse.observe(requireActivity()) { response ->
RetrofitService.handleRequest(response = response, onSuccess = { res ->
setData(res)
WorkoutData.workoutId = res.data.myWorkout.id
getPlanData()
}, 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()
})
binding.progressView.progressOverlay.visibility = View.GONE
}
private fun handleError(exception: Throwable) {
Toast.makeText(requireContext(), exception.message, Toast.LENGTH_SHORT).show()
binding.progressView.progressOverlay.visibility = View.GONE
}

private fun getPlanData() {
Expand Down Expand Up @@ -142,7 +128,9 @@ class HomeFragment : Fragment() {
binding.workouttime.text =
formatWorkoutTime(response.data.myWorkout.workout.min_per_day, requireContext())
binding.exerciseCountTxt.text =
getTodayWorkout()?.total_number_exercises.toString() + " " + getString(R.string.exercise)
WorkoutData.getTodayWorkout(response.data.myWorkout.weeks)?.total_number_exercises.toString() + " " + getString(
R.string.exercise
)
}

private fun formatWorkoutTime(minutesPerDay: Int, context: Context): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.modarb.android.ui.home.ui.home.data

import com.modarb.android.network.ApiService
import com.modarb.android.network.Result
import com.modarb.android.ui.home.ui.home.domain.HomeRepository
import com.modarb.android.ui.home.ui.home.domain.models.HomePageResponse

class HomeRepositoryImpl(private val apiService: ApiService) : HomeRepository {
override suspend fun getHomePage(token: String): Result<HomePageResponse> {

return try {
val response = apiService.getHomePage(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.home.domain

import com.modarb.android.network.Result
import com.modarb.android.ui.home.ui.home.domain.models.HomePageResponse

interface HomeRepository {

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

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

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

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

class MyMealPlan
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.modarb.android.ui.home.ui.home.models
package com.modarb.android.ui.home.ui.home.domain.models

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

data class MyWorkout(
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.home.models
package com.modarb.android.ui.home.ui.home.domain.models

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

data class User(
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.home.models
package com.modarb.android.ui.home.ui.home.domain.models

data class Workout(
val id: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.modarb.android.ui.home.ui.home.domain.usecase

import com.modarb.android.network.Result
import com.modarb.android.ui.home.ui.home.domain.HomeRepository
import com.modarb.android.ui.home.ui.home.domain.models.HomePageResponse

class HomePageUseCase(private val homeRepository: HomeRepository) {
suspend operator fun invoke(token: String): Result<HomePageResponse> {
return homeRepository.getHomePage(token)
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.modarb.android.ui.home.ui.home.presentation

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.modarb.android.network.Result
import com.modarb.android.network.RetrofitService
import com.modarb.android.network.models.BaseResponse
import com.modarb.android.ui.home.ui.home.data.HomeRepositoryImpl
import com.modarb.android.ui.home.ui.home.domain.models.HomePageResponse
import com.modarb.android.ui.home.ui.home.domain.usecase.HomePageUseCase
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

class HomeViewModel : ViewModel() {

private var apiService = RetrofitService.createService()
private var homeRepository = HomeRepositoryImpl(apiService)
private var homeUseCase = HomePageUseCase(homeRepository)

private val _homeResponse = MutableStateFlow<Result<HomePageResponse>?>(null)
val homeResponse: StateFlow<Result<BaseResponse>?> get() = _homeResponse


fun getUserHomePage(token: String) {
viewModelScope.launch {
val result = homeUseCase.invoke(token)
_homeResponse.value = result
}

}
}

0 comments on commit 08032fa

Please sign in to comment.