This file contains hidden or 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
private fun parse(url: String): ParseResult { | |
val result = URL_PARSE_REGEX.find(url) | |
if(result == null) { | |
return ParseResult.Error("No match found") | |
} | |
val mimeType = result.groupValues[2] | |
val data = result.groupValues[4] | |
return ParseResult.ParsedData(data, mimeType) |
This file contains hidden or 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
sealed class ParseResult | |
data class Error(val errorMessage: String) : ParseResult() | |
data class ParsedData( | |
val data: String, | |
val mimeType: String) : ParseResult() | |
This file contains hidden or 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
class ResultWrapper( | |
val data: ParsedData? = null, | |
val errorMessage: String? = null | |
) |
This file contains hidden or 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
val result = parse(url) | |
if(result.errorMessage != null) { | |
// error occurred - log error message etc... | |
} else { | |
// happy path - use the data. But result.data is nullable 🙄 | |
val mimeType = result.data!!.mimeType | |
} |
This file contains hidden or 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
private fun parse(url: String): ParsedData { | |
val result = URL_PARSE_REGEX.find(url) | |
if(result == null) { | |
// what do we do here? Throw an exception? Return null? | |
} | |
val mimeType = result.groupValues[2] | |
val data = result.groupValues[4] |
This file contains hidden or 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
taskDao.getAll().observe(this, Observer<List<Task>> { | |
taskListAdapter.submitList(it) | |
}) |
This file contains hidden or 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
class TaskDiffCallback : DiffUtil.ItemCallback<Task>() { | |
override fun areItemsTheSame(oldItem: Task?, newItem: Task?): Boolean { | |
return oldItem?.id == newItem?.id | |
} | |
override fun areContentsTheSame(oldItem: Task?, newItem: Task?): Boolean { | |
return oldItem == newItem | |
} | |
} |
This file contains hidden or 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
class TaskListAdapter(private val clickListener: (Task) -> Unit) : | |
ListAdapter<Task, ViewHolder>(TaskDiffCallback()) { | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | |
val inflater = LayoutInflater.from(parent.context) | |
return ViewHolder(inflater.inflate(R.layout.item_task_row, parent, false)) | |
} | |
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | |
holder.bind(getItem(position), clickListener) |
This file contains hidden or 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
class TaskListAdapter : RecyclerView.Adapter<ViewHolder>() { | |
private val tasks: MutableList<Task> = ArrayList() | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | |
val inflater = LayoutInflater.from(parent.context) | |
return ViewHolder(inflater.inflate(R.layout.item_task_row, parent, false)) | |
} | |
override fun onBindViewHolder(holder: ViewHolder, position: Int) { |
This file contains hidden or 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
@Entity | |
data class Task(@PrimaryKey(autoGenerate = true) var id: Int = 0, | |
var title: String = "", | |
var completed: Boolean = false) |