-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from Modarb-Ai-Trainer/feature_add_custom_exer…
…cise Prepare for adding custom workout
- Loading branch information
Showing
38 changed files
with
596 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
app/src/main/java/com/modarb/android/ui/helpers/ViewUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.modarb.android.ui.helpers | ||
|
||
import android.content.Context | ||
import android.widget.ImageView | ||
import com.bumptech.glide.Glide | ||
|
||
object ViewUtils { | ||
|
||
|
||
fun loadImage(context: Context, imageUrl: String, imageView: ImageView) { | ||
Glide.with(context).asBitmap().load(imageUrl).into(imageView) | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
app/src/main/java/com/modarb/android/ui/home/ui/plan/ExercisesPagingSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.modarb.android.ui.home.ui.plan | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import com.modarb.android.network.ApiService | ||
import com.modarb.android.ui.home.ui.plan.domain.models.allExercises.Data | ||
|
||
class ExercisesPagingSource( | ||
private val apiService: ApiService, | ||
private val token: String, | ||
private val filter: String, | ||
) : PagingSource<Int, Data>() { | ||
|
||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Data> { | ||
val page = params.key ?: 0 | ||
try { | ||
val response = apiService.getExercises(token, filter, page, params.loadSize) | ||
return LoadResult.Page( | ||
data = response.body()!!.data, | ||
prevKey = if (page == 0) null else page - 1, | ||
nextKey = if (response.body()!!.data.isEmpty()) null else page + 1 | ||
) | ||
} catch (e: Exception) { | ||
return LoadResult.Error(e) | ||
} | ||
} | ||
|
||
|
||
override fun getRefreshKey(state: PagingState<Int, Data>): Int? { | ||
return state.anchorPosition?.let { anchorPosition -> | ||
state.closestPageToPosition(anchorPosition)?.prevKey?.plus(1) | ||
?: state.closestPageToPosition(anchorPosition)?.nextKey?.minus(1) | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
app/src/main/java/com/modarb/android/ui/home/ui/plan/LoadingStateListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.modarb.android.ui.home.ui.plan | ||
|
||
interface LoadingStateListener { | ||
fun onLoadingStarted() | ||
fun onLoadingFinished() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 73 additions & 28 deletions
101
app/src/main/java/com/modarb/android/ui/home/ui/plan/adapters/ExercisesAddAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,107 @@ | ||
package com.modarb.android.ui.home.ui.plan.adapters | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.paging.PagingData | ||
import androidx.paging.PagingDataAdapter | ||
import androidx.recyclerview.widget.DiffUtil | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.modarb.android.databinding.ItemExerciseSelectionDetailsBinding | ||
import com.modarb.android.ui.workout.domain.models.WorkoutModel | ||
import com.modarb.android.ui.home.ui.plan.domain.models.allExercises.Data | ||
|
||
class ExercisesAddAdapter(private val itemList: List<WorkoutModel>) : | ||
RecyclerView.Adapter<ExercisesAddAdapter.ViewHolder>() { | ||
class ExercisesAddAdapter(private var context: Context, private val isAdd: Boolean) : | ||
PagingDataAdapter<Data, ExercisesAddAdapter.ViewHolder>(DIFF_CALLBACK) { | ||
|
||
private val selectedItems = mutableListOf<Int>() | ||
private var selectedItems: HashMap<String, Data> = HashMap() | ||
|
||
inner class ViewHolder(private val binding: ItemExerciseSelectionDetailsBinding) : | ||
RecyclerView.ViewHolder(binding.root) { | ||
|
||
fun bind(item: WorkoutModel) { | ||
val isSelected = selectedItems.contains(item.id) | ||
binding.root.isActivated = isSelected | ||
binding.overlay.visibility = if (isSelected) ViewGroup.VISIBLE else ViewGroup.INVISIBLE | ||
binding.checkMark.visibility = | ||
if (isSelected) ViewGroup.VISIBLE else ViewGroup.INVISIBLE | ||
fun bind(item: Data?) { | ||
item?.let { data -> | ||
// TODO uncomment this | ||
//ViewUtils.loadImage(context,data.coverImage,binding.exerciseImage) | ||
|
||
binding.exerciseTitle.text = data.name | ||
binding.exerciseDesc.text = data.benefits | ||
|
||
if (isAdd) { | ||
val isSelected = selectedItems.contains(data.id) | ||
binding.root.isActivated = isSelected | ||
binding.overlay.visibility = | ||
if (isSelected) ViewGroup.VISIBLE else ViewGroup.INVISIBLE | ||
binding.checkMark.visibility = | ||
if (isSelected) ViewGroup.VISIBLE else ViewGroup.INVISIBLE | ||
|
||
binding.root.setOnClickListener { | ||
toggleSelection(data.id, adapterPosition) | ||
} | ||
} | ||
|
||
binding.root.setOnClickListener { | ||
toggleSelection(item.id, adapterPosition) | ||
Log.d("selected", getSelectedItems().toString()) | ||
} | ||
} | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | ||
val binding = ItemExerciseSelectionDetailsBinding.inflate( | ||
LayoutInflater.from(parent.context), | ||
parent, | ||
false | ||
LayoutInflater.from(parent.context), parent, false | ||
) | ||
return ViewHolder(binding) | ||
} | ||
|
||
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
val item = itemList[position] | ||
holder.bind(item) | ||
holder.bind(getItem(position)) | ||
} | ||
|
||
override fun getItemCount(): Int { | ||
return itemList.size | ||
} | ||
|
||
private fun toggleSelection(itemId: Int, position: Int) { | ||
private fun toggleSelection(itemId: String, position: Int) { | ||
if (selectedItems.contains(itemId)) { | ||
selectedItems.remove(itemId) | ||
|
||
} else { | ||
selectedItems.add(itemId) | ||
selectedItems[itemId] = getItem(position)!! | ||
} | ||
getSelectedItems() | ||
notifyItemChanged(position) | ||
} | ||
|
||
fun getSelectedItems(): List<Int> { | ||
return selectedItems.toList() | ||
private fun getSelectedItems() { | ||
Log.d("selectedItems", selectedItems.toString()) | ||
} | ||
|
||
fun updateData(lifecycleOwner: LifecycleOwner, newData: List<Data>) { | ||
submitData(lifecycleOwner.lifecycle, PagingData.from(newData)) | ||
} | ||
|
||
fun clearData(lifecycleOwner: LifecycleOwner) { | ||
submitData(lifecycleOwner.lifecycle, PagingData.empty()) | ||
} | ||
|
||
fun clearSelectedData() { | ||
selectedItems.clear() | ||
notifyDataSetChanged() | ||
} | ||
|
||
fun getSelectedData(): List<Data> { | ||
val selectedDataList = mutableListOf<Data>() | ||
|
||
for ((_, value) in selectedItems) { | ||
selectedDataList.add(value) | ||
} | ||
|
||
return selectedDataList | ||
} | ||
|
||
|
||
companion object { | ||
private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<Data>() { | ||
override fun areItemsTheSame(oldItem: Data, newItem: Data): Boolean { | ||
return oldItem.id == newItem.id | ||
} | ||
|
||
override fun areContentsTheSame(oldItem: Data, newItem: Data): Boolean { | ||
return oldItem == newItem | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.