Skip to content

Instantly share code, notes, and snippets.

View adrianhall's full-sized avatar

Adrian Hall adrianhall

View GitHub Profile
/**
* Obtain an item based on the ID
*/
fun getNoteById(noteId: String): Note?
= items.first { it.noteId == noteId }
/**
* Save a new item to the list
*/
fun saveItem(item: Note) {
class MockNotesRepository : NotesRepository {
companion object {
private val TAG = this::class.java.simpleName
}
private val factory = MockNotesDataSourceFactory()
override val notes: LiveData<PagedList<Note>>
init {
data class Note(val noteId: String = UUID.randomUUID().toString()) {
companion object {
val DiffCallback = object : DiffUtil.ItemCallback<Note>() {
override fun areItemsTheSame(oldItem: Note?, newItem: Note?): Boolean
= oldItem != null && newItem != null && oldItem.noteId == newItem.noteId
override fun areContentsTheSame(oldItem: Note?, newItem: Note?): Boolean
= oldItem != null && newItem != null
&& oldItem.noteId == newItem.noteId
&& oldItem.title == newItem.title
class NotesPagedListAdapter(val onClick: (Note) -> Unit) : PagedListAdapter<Note, ListItemViewHolder>(Note.DiffCallback) {
companion object {
private val TAG = this::class.java.simpleName
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListItemViewHolder
= ListItemViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.note_list_item, parent, false))
override fun onBindViewHolder(holder: ListItemViewHolder, position: Int) {
Log.d(TAG, "Binding view holder at position $position")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_note_list)
setSupportActionBar(note_list_toolbar)
note_list_add_note.setOnClickListener {
Log.d(TAG, "addNote button pressed")
launchNoteDetailActivity(this)
}
package com.shellmonger.apps.mynotes.ui
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.PorterDuff
import android.graphics.drawable.ColorDrawable
import android.support.v4.content.ContextCompat
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.helper.ItemTouchHelper
val adapter = NotesPagedListAdapter {
launchNoteDetailActivity(this@NoteListActivity, it.noteId)
}
viewModel.notes.observe(this, Observer {
it?.let { adapter.submitList(it) }
})
note_list_recyclerview.layoutManager = LinearLayoutManager(this).apply {
orientation = LinearLayoutManager.VERTICAL
}
note_list_recyclerview.adapter = adapter
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.2.41'
ext.appsync_version = '2.6.18'
repositories {
google()
jcenter()
}
dependencies {
query GetNote($noteId: ID!) {
getNote(noteId: $noteId) {
noteId
title
content
}
}
query AllNotes($nextToken: String) {
allNotes(nextToken: $nextToken) {
package com.shellmonger.apps.mynotes.repositories.aws
import android.content.Context
import android.arch.lifecycle.LiveData
import android.arch.paging.LivePagedListBuilder
import android.arch.paging.PagedList
import com.shellmonger.apps.mynotes.models.Note
import com.shellmonger.apps.mynotes.repositories.NotesRepository
import com.shellmonger.apps.mynotes.services.AnalyticsService
import com.shellmonger.apps.mynotes.services.IdentityService