diff --git a/app/src/main/java/com/modarb/android/network/ApiService.kt b/app/src/main/java/com/modarb/android/network/ApiService.kt index 1d64944..8d11c96 100644 --- a/app/src/main/java/com/modarb/android/network/ApiService.kt +++ b/app/src/main/java/com/modarb/android/network/ApiService.kt @@ -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 diff --git a/app/src/main/java/com/modarb/android/ui/home/helpers/WorkoutData.kt b/app/src/main/java/com/modarb/android/ui/home/helpers/WorkoutData.kt index 7aade2d..d6a9dcb 100644 --- a/app/src/main/java/com/modarb/android/ui/home/helpers/WorkoutData.kt +++ b/app/src/main/java/com/modarb/android/ui/home/helpers/WorkoutData.kt @@ -42,4 +42,20 @@ object WorkoutData { return null } + + fun getTodayWorkout(weekList: List): Day? { + for (week in weekList) { + if (!week.is_done) { + for (day in week.days) { + if (!day.is_done) { + return day + } + } + break + } + } + return null + } + + } \ No newline at end of file diff --git a/app/src/main/java/com/modarb/android/ui/home/ui/home/HomeFragment.kt b/app/src/main/java/com/modarb/android/ui/home/ui/home/HomeFragment.kt index 2ce518b..3f842ff 100644 --- a/app/src/main/java/com/modarb/android/ui/home/ui/home/HomeFragment.kt +++ b/app/src/main/java/com/modarb/android/ui/home/ui/home/HomeFragment.kt @@ -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 @@ -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 } @@ -57,6 +55,7 @@ class HomeFragment : Fragment() { } } + private fun initLogout() { binding.profileBtn.setOnClickListener { UserPrefUtil.saveUserData(requireContext(), null) @@ -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() { @@ -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 { diff --git a/app/src/main/java/com/modarb/android/ui/home/ui/home/data/HomeRepositoryImpl.kt b/app/src/main/java/com/modarb/android/ui/home/ui/home/data/HomeRepositoryImpl.kt new file mode 100644 index 0000000..d3c7291 --- /dev/null +++ b/app/src/main/java/com/modarb/android/ui/home/ui/home/data/HomeRepositoryImpl.kt @@ -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 { + + 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) + } + } + + +} diff --git a/app/src/main/java/com/modarb/android/ui/home/ui/home/domain/HomeRepository.kt b/app/src/main/java/com/modarb/android/ui/home/ui/home/domain/HomeRepository.kt new file mode 100644 index 0000000..ea9af0b --- /dev/null +++ b/app/src/main/java/com/modarb/android/ui/home/ui/home/domain/HomeRepository.kt @@ -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 +} \ No newline at end of file diff --git a/app/src/main/java/com/modarb/android/ui/home/ui/home/models/Data.kt b/app/src/main/java/com/modarb/android/ui/home/ui/home/domain/models/Data.kt similarity index 63% rename from app/src/main/java/com/modarb/android/ui/home/ui/home/models/Data.kt rename to app/src/main/java/com/modarb/android/ui/home/ui/home/domain/models/Data.kt index 208f6b5..916f86a 100644 --- a/app/src/main/java/com/modarb/android/ui/home/ui/home/models/Data.kt +++ b/app/src/main/java/com/modarb/android/ui/home/ui/home/domain/models/Data.kt @@ -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, diff --git a/app/src/main/java/com/modarb/android/ui/home/ui/home/models/HomePageResponse.kt b/app/src/main/java/com/modarb/android/ui/home/ui/home/domain/models/HomePageResponse.kt similarity index 68% rename from app/src/main/java/com/modarb/android/ui/home/ui/home/models/HomePageResponse.kt rename to app/src/main/java/com/modarb/android/ui/home/ui/home/domain/models/HomePageResponse.kt index f6d2b28..d7d45ee 100644 --- a/app/src/main/java/com/modarb/android/ui/home/ui/home/models/HomePageResponse.kt +++ b/app/src/main/java/com/modarb/android/ui/home/ui/home/domain/models/HomePageResponse.kt @@ -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 diff --git a/app/src/main/java/com/modarb/android/ui/home/ui/home/domain/models/MyMealPlan.kt b/app/src/main/java/com/modarb/android/ui/home/ui/home/domain/models/MyMealPlan.kt new file mode 100644 index 0000000..d132fb4 --- /dev/null +++ b/app/src/main/java/com/modarb/android/ui/home/ui/home/domain/models/MyMealPlan.kt @@ -0,0 +1,3 @@ +package com.modarb.android.ui.home.ui.home.domain.models + +class MyMealPlan \ No newline at end of file diff --git a/app/src/main/java/com/modarb/android/ui/home/ui/home/models/MyWorkout.kt b/app/src/main/java/com/modarb/android/ui/home/ui/home/domain/models/MyWorkout.kt similarity index 52% rename from app/src/main/java/com/modarb/android/ui/home/ui/home/models/MyWorkout.kt rename to app/src/main/java/com/modarb/android/ui/home/ui/home/domain/models/MyWorkout.kt index 54dd5bf..da9e650 100644 --- a/app/src/main/java/com/modarb/android/ui/home/ui/home/models/MyWorkout.kt +++ b/app/src/main/java/com/modarb/android/ui/home/ui/home/domain/models/MyWorkout.kt @@ -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, diff --git a/app/src/main/java/com/modarb/android/ui/home/ui/home/models/Preferences.kt b/app/src/main/java/com/modarb/android/ui/home/ui/home/domain/models/Preferences.kt similarity index 79% rename from app/src/main/java/com/modarb/android/ui/home/ui/home/models/Preferences.kt rename to app/src/main/java/com/modarb/android/ui/home/ui/home/domain/models/Preferences.kt index e40bf4e..6624962 100644 --- a/app/src/main/java/com/modarb/android/ui/home/ui/home/models/Preferences.kt +++ b/app/src/main/java/com/modarb/android/ui/home/ui/home/domain/models/Preferences.kt @@ -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, diff --git a/app/src/main/java/com/modarb/android/ui/home/ui/home/models/User.kt b/app/src/main/java/com/modarb/android/ui/home/ui/home/domain/models/User.kt similarity index 68% rename from app/src/main/java/com/modarb/android/ui/home/ui/home/models/User.kt rename to app/src/main/java/com/modarb/android/ui/home/ui/home/domain/models/User.kt index 0f11561..7677493 100644 --- a/app/src/main/java/com/modarb/android/ui/home/ui/home/models/User.kt +++ b/app/src/main/java/com/modarb/android/ui/home/ui/home/domain/models/User.kt @@ -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, diff --git a/app/src/main/java/com/modarb/android/ui/home/ui/home/models/Workout.kt b/app/src/main/java/com/modarb/android/ui/home/ui/home/domain/models/Workout.kt similarity index 60% rename from app/src/main/java/com/modarb/android/ui/home/ui/home/models/Workout.kt rename to app/src/main/java/com/modarb/android/ui/home/ui/home/domain/models/Workout.kt index 1be392f..f2b8553 100644 --- a/app/src/main/java/com/modarb/android/ui/home/ui/home/models/Workout.kt +++ b/app/src/main/java/com/modarb/android/ui/home/ui/home/domain/models/Workout.kt @@ -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, diff --git a/app/src/main/java/com/modarb/android/ui/home/ui/home/domain/usecase/HomePageUseCase.kt b/app/src/main/java/com/modarb/android/ui/home/ui/home/domain/usecase/HomePageUseCase.kt new file mode 100644 index 0000000..6cd5787 --- /dev/null +++ b/app/src/main/java/com/modarb/android/ui/home/ui/home/domain/usecase/HomePageUseCase.kt @@ -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 { + return homeRepository.getHomePage(token) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/modarb/android/ui/home/ui/home/logic/HomeRepository.kt b/app/src/main/java/com/modarb/android/ui/home/ui/home/logic/HomeRepository.kt deleted file mode 100644 index fe3d7be..0000000 --- a/app/src/main/java/com/modarb/android/ui/home/ui/home/logic/HomeRepository.kt +++ /dev/null @@ -1,18 +0,0 @@ -package com.modarb.android.ui.home.ui.home.logic - -import android.content.Context -import com.modarb.android.network.ApiService -import com.modarb.android.ui.home.ui.home.models.HomePageResponse -import com.modarb.android.ui.onboarding.utils.UserPref.UserPrefUtil -import retrofit2.Response - -class HomeRepository(private val apiService: ApiService) { - - suspend fun getUserHomePage(context: Context): Response { - return apiService.getHomePage( - "Bearer " + UserPrefUtil.getUserData(context)!!.token - ) - } - - -} diff --git a/app/src/main/java/com/modarb/android/ui/home/ui/home/logic/HomeViewModel.kt b/app/src/main/java/com/modarb/android/ui/home/ui/home/logic/HomeViewModel.kt deleted file mode 100644 index ff7d4bd..0000000 --- a/app/src/main/java/com/modarb/android/ui/home/ui/home/logic/HomeViewModel.kt +++ /dev/null @@ -1,24 +0,0 @@ -package com.modarb.android.ui.home.ui.home.logic - -import android.content.Context -import androidx.lifecycle.LiveData -import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.ViewModel -import androidx.lifecycle.viewModelScope -import com.modarb.android.ui.home.ui.home.models.HomePageResponse -import kotlinx.coroutines.launch -import retrofit2.Response - -class HomeViewModel(private val homeRepository: HomeRepository) : ViewModel() { - - private val _homeResponse = MutableLiveData>() - val homeResponse: LiveData> = _homeResponse - - - fun getUserHomePage(ctx: Context) { - viewModelScope.launch { - val response = homeRepository.getUserHomePage(ctx) - _homeResponse.value = response - } - } -} \ No newline at end of file diff --git a/app/src/main/java/com/modarb/android/ui/home/ui/home/logic/HomeViewModelFactory.kt b/app/src/main/java/com/modarb/android/ui/home/ui/home/logic/HomeViewModelFactory.kt deleted file mode 100644 index 238f520..0000000 --- a/app/src/main/java/com/modarb/android/ui/home/ui/home/logic/HomeViewModelFactory.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.modarb.android.ui.home.ui.home.logic - -import androidx.lifecycle.ViewModel -import androidx.lifecycle.ViewModelProvider - -class HomeViewModelFactory(private val repository: HomeRepository) : ViewModelProvider.Factory { - override fun create(modelClass: Class): T { - if (modelClass.isAssignableFrom(HomeViewModel::class.java)) { - @Suppress("UNCHECKED_CAST") - return HomeViewModel(repository) as T - } - throw IllegalArgumentException("Unknown ViewModel class") - } -} diff --git a/app/src/main/java/com/modarb/android/ui/home/ui/home/models/Day.kt b/app/src/main/java/com/modarb/android/ui/home/ui/home/models/Day.kt deleted file mode 100644 index 05d341c..0000000 --- a/app/src/main/java/com/modarb/android/ui/home/ui/home/models/Day.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.modarb.android.ui.home.ui.home.models - -data class Day( - val day_number: Int, - val day_type: String, - val is_done: Boolean, - val total_number_exercises: Int -) \ No newline at end of file diff --git a/app/src/main/java/com/modarb/android/ui/home/ui/home/models/MyMealPlan.kt b/app/src/main/java/com/modarb/android/ui/home/ui/home/models/MyMealPlan.kt deleted file mode 100644 index 9b9c2e6..0000000 --- a/app/src/main/java/com/modarb/android/ui/home/ui/home/models/MyMealPlan.kt +++ /dev/null @@ -1,3 +0,0 @@ -package com.modarb.android.ui.home.ui.home.models - -class MyMealPlan \ No newline at end of file diff --git a/app/src/main/java/com/modarb/android/ui/home/ui/home/models/Week.kt b/app/src/main/java/com/modarb/android/ui/home/ui/home/models/Week.kt deleted file mode 100644 index 055bef8..0000000 --- a/app/src/main/java/com/modarb/android/ui/home/ui/home/models/Week.kt +++ /dev/null @@ -1,7 +0,0 @@ -package com.modarb.android.ui.home.ui.home.models - -data class Week( - val days: List, - val is_done: Boolean, - val week_number: Int -) \ No newline at end of file diff --git a/app/src/main/java/com/modarb/android/ui/home/ui/home/presentation/HomeViewModel.kt b/app/src/main/java/com/modarb/android/ui/home/ui/home/presentation/HomeViewModel.kt new file mode 100644 index 0000000..fc20892 --- /dev/null +++ b/app/src/main/java/com/modarb/android/ui/home/ui/home/presentation/HomeViewModel.kt @@ -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?>(null) + val homeResponse: StateFlow?> get() = _homeResponse + + + fun getUserHomePage(token: String) { + viewModelScope.launch { + val result = homeUseCase.invoke(token) + _homeResponse.value = result + } + + } +} \ No newline at end of file