-
Notifications
You must be signed in to change notification settings - Fork 1
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 #15 from AdevintaSpain/use_vioew_model
TECH: Use view model to load data
- Loading branch information
Showing
6 changed files
with
183 additions
and
106 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
77 changes: 77 additions & 0 deletions
77
taggingviewer/src/main/java/com/adevinta/android/taggingviewer/DetailTaggingAdapter.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,77 @@ | ||
package com.adevinta.android.taggingviewer | ||
|
||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.adevinta.android.taggingviewer.databinding.TaggingDetailedItemBinding | ||
import com.adevinta.android.taggingviewer.internal.TagEntry | ||
import java.text.SimpleDateFormat | ||
import java.util.Date | ||
import java.util.Locale | ||
|
||
internal class DetailTaggingAdapter : RecyclerView.Adapter<DetailTaggingViewHolder>() { | ||
var entries: List<TagEntry> = mutableListOf() | ||
set(value) { | ||
field = value | ||
notifyDataSetChanged() | ||
} | ||
|
||
override fun getItemCount() = entries.size | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DetailTaggingViewHolder { | ||
val binding = TaggingDetailedItemBinding.inflate(LayoutInflater.from(parent.context), parent, false) | ||
return DetailTaggingViewHolder(binding) | ||
} | ||
|
||
override fun onBindViewHolder(holder: DetailTaggingViewHolder, position: Int) { | ||
val listPosition = when (position) { | ||
0 -> ListPosition.FIRST | ||
itemCount - 1 -> ListPosition.LAST | ||
else -> ListPosition.MIDDLE | ||
} | ||
holder.bind(entries[position], listPosition) | ||
} | ||
} | ||
|
||
internal class DetailTaggingViewHolder(binding: TaggingDetailedItemBinding) : RecyclerView.ViewHolder(binding.root) { | ||
|
||
private val timeFormat = SimpleDateFormat("HH:mm:ss", Locale.getDefault()) | ||
|
||
fun bind(tagEntry: TagEntry, listPosition: ListPosition) { | ||
val binding = TaggingDetailedItemBinding.bind(itemView) | ||
|
||
binding.detailItemTitle.text = tagEntry.name | ||
|
||
with(binding.detailItemExtra) { | ||
if (tagEntry is TagEntry.ItemEntry) { | ||
if (tagEntry.details.isEmpty()) { | ||
visibility = View.GONE | ||
} else { | ||
visibility = View.VISIBLE | ||
text = tagEntry.details | ||
.map { (key, value) -> "$key: $value" } | ||
.joinToString("\n") | ||
} | ||
} | ||
} | ||
|
||
binding.detailItemTime.text = timeFormat.format(Date(tagEntry.timestamp)) | ||
|
||
binding.detailItemIcon.setImageResource( | ||
when (tagEntry) { | ||
is TagEntry.ItemEntry.Click -> R.drawable.tgv_marker_click | ||
is TagEntry.ItemEntry.Event -> R.drawable.tgv_marker_event | ||
is TagEntry.ItemEntry.Screen -> R.drawable.tgv_marker_screen | ||
is TagEntry.ItemEntry.UserAttribute -> R.drawable.tgv_marker_user_attribute | ||
else -> error("No icon assigned to $tagEntry") | ||
} | ||
) | ||
|
||
binding.detailItemLineBottom.visibility = if (listPosition == ListPosition.LAST) { | ||
View.INVISIBLE | ||
} else { | ||
View.VISIBLE | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
taggingviewer/src/main/java/com/adevinta/android/taggingviewer/DetailTaggingViewModel.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,63 @@ | ||
package com.adevinta.android.taggingviewer | ||
|
||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.adevinta.android.taggingviewer.internal.TagEntry | ||
import com.adevinta.android.taggingviewer.internal.TrackingDispatcher | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.launch | ||
|
||
class DetailTaggingViewModel : ViewModel() { | ||
|
||
private var currentEntries: List<TagEntry> = emptyList() | ||
|
||
private var itemTypes: MutableMap<String, Boolean> = mutableMapOf() | ||
|
||
private val _flow: MutableStateFlow<List<TagEntry>> = MutableStateFlow(currentEntries) | ||
val flow: StateFlow<List<TagEntry>> = _flow.asStateFlow() | ||
|
||
private val _filtersShown: MutableStateFlow<Map<String, Boolean>?> = MutableStateFlow(null) | ||
val filterShown: StateFlow<Map<String, Boolean>?> = _filtersShown.asStateFlow() | ||
|
||
fun onInit(lifecycleOwner: LifecycleOwner) = viewModelScope.launch { | ||
TrackingDispatcher.entriesData().observe(lifecycleOwner) { entries -> | ||
currentEntries = entries | ||
|
||
itemTypes = currentEntries | ||
.map { it.name } | ||
.filterNot { it.isEmpty() } | ||
.distinct() | ||
.associateWith { key -> itemTypes[key] ?: true } | ||
.toMutableMap() | ||
|
||
sendEntries() | ||
} | ||
} | ||
|
||
private fun sendEntries() { | ||
_flow.value = currentEntries | ||
.sortedByDescending { it.timestamp } | ||
.filterNot { it is TagEntry.SeparatorEntry } | ||
.filter { itemTypes[it.name] ?: true } | ||
} | ||
|
||
fun clearAll() = viewModelScope.launch { | ||
TaggingViewer.clearAll() | ||
} | ||
|
||
fun onFilterUpdated(type: String, visible: Boolean) { | ||
itemTypes[type] = visible | ||
sendEntries() | ||
} | ||
|
||
fun onShowFilters() = viewModelScope.launch { | ||
_filtersShown.value = itemTypes | ||
} | ||
|
||
fun onFiltersShown() = viewModelScope.launch { | ||
_filtersShown.value = null | ||
} | ||
} |
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
5 changes: 5 additions & 0 deletions
5
taggingviewer/src/main/java/com/adevinta/android/taggingviewer/ListPosition.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,5 @@ | ||
package com.adevinta.android.taggingviewer | ||
|
||
internal enum class ListPosition { | ||
FIRST, LAST, MIDDLE | ||
} |