Skip to content

Instantly share code, notes, and snippets.

View adrianhall's full-sized avatar

Adrian Hall adrianhall

View GitHub Profile
class NoteListActivity : AppCompatActivity() {
companion object {
private val TAG = this::class.java.simpleName
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_note_list)
note_list_add_note.setOnClickListener {
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/note_detail_toolbar"
android:layout_width="match_parent"
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/list_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
package com.shellmonger.apps.mynotes.ui
import android.support.v7.widget.RecyclerView
import android.view.View
import com.shellmonger.apps.mynotes.models.Note
import kotlinx.android.synthetic.main.note_list_item.view.*
class ListItemViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
var note: Note? = null
set(value) {
package com.shellmonger.apps.mynotes.adapters
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
import com.shellmonger.apps.mynotes.R
import com.shellmonger.apps.mynotes.models.Note
import com.shellmonger.apps.mynotes.ui.ListItemViewHolder
class NoteListAdapter(val onClick: (Note) -> Unit) : RecyclerView.Adapter<ListItemViewHolder>() {
class MockNotesRepository : NotesRepository {
companion object {
private val TAG = this::class.java.simpleName
}
private val _notes: MutableMap<String, Note> = HashMap()
private val mutableNotes: MutableLiveData<List<Note>> = MutableLiveData()
init {
mutableNotes.postValue(_notes.values.toList())
class NoteListActivity : AppCompatActivity() {
companion object {
private val TAG = this::class.java.simpleName
}
private val viewModel by viewModel<NoteListViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_note_list)
class MockNotesRepository : NotesRepository {
companion object {
private val TAG = this::class.java.simpleName
}
private val _notes: MutableMap<String, Note> = HashMap()
private val mutableNotes: MutableLiveData<List<Note>> = MutableLiveData()
init {
updateList()
package com.shellmonger.apps.mynotes.repositories.datasources
import android.arch.paging.DataSource
import android.arch.paging.ItemKeyedDataSource
import android.util.Log
import com.shellmonger.apps.mynotes.models.Note
class MockNotesDataSource : ItemKeyedDataSource<Int, Note>() {
companion object {
private val TAG = this::class.java.simpleName
/**
* Load the initial items
*/
override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Note>) {
Log.d(TAG, "loadInitial: key=${params.requestedInitialKey ?: "undefined"}, size=${params.requestedLoadSize}")
val pageSize = minOf(MAX_PAGE_SIZE, params.requestedLoadSize)
val firstItem = inRange(params.requestedInitialKey ?: 0,0, items.size)
val lastItem = inRange(firstItem + pageSize, 0, items.size)
Log.d(TAG, "loadInitial: firstItem = $firstItem, lastItem = $lastItem")
val data = if (firstItem == lastItem) emptyList<Note>() else items.subList(firstItem, lastItem)