Skip to content

Commit

Permalink
Clean up ViewModels and LiveData
Browse files Browse the repository at this point in the history
Fix some text
Change item type dropdown to images
  • Loading branch information
North101 committed May 4, 2018
1 parent 137bd41 commit 8d55e57
Show file tree
Hide file tree
Showing 58 changed files with 933 additions and 827 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ android {
applicationId "net.north101.android.ghplayertracker"
minSdkVersion 21
targetSdkVersion 27
versionCode 8
versionName "0.5.2"
versionCode 9
versionName "0.5.3"
setProperty("archivesBaseName", "ghplayertracker v$versionName")
}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package net.north101.android.ghplayertracker
import android.support.v7.util.DiffUtil
import android.support.v7.widget.RecyclerView
import android.view.ViewGroup
import net.north101.android.ghplayertracker.livedata.*

class CharacterAdapter : RecyclerView.Adapter<BaseViewHolder<*>>() {
private val items = ArrayList<RecyclerItemCompare>()
Expand All @@ -15,12 +16,12 @@ class CharacterAdapter : RecyclerView.Adapter<BaseViewHolder<*>>() {
Both(Left.id or Right.id)
}

data class Stats(val character: CharacterModel) : RecyclerItemCompare {
data class Stats(val character: CharacterLiveData) : RecyclerItemCompare {
override val compareItemId: String
get() = ""
}

data class Item(val item: ItemData) : RecyclerItemCompare {
data class Item(val item: net.north101.android.ghplayertracker.livedata.ItemLiveData) : RecyclerItemCompare {
override val compareItemId: String
get() = item.name.toString()
}
Expand All @@ -30,27 +31,27 @@ class CharacterAdapter : RecyclerView.Adapter<BaseViewHolder<*>>() {
get() = note.value.toString()
}

data class Perk(val perk: PerkData) : RecyclerItemCompare {
data class Perk(val perk: net.north101.android.ghplayertracker.livedata.PerkLiveData) : RecyclerItemCompare {
override val compareItemId: String
get() = perk.perk.text
}

data class PerkNote(val index: Int, val perkNote: PerkNoteData) : RecyclerItemCompare {
data class PerkNote(val index: Int, val perkNote: net.north101.android.ghplayertracker.livedata.PerkNoteLiveData) : RecyclerItemCompare {
override val compareItemId: String
get() = index.toString()
}

var onNumberEditClick: ((String) -> Unit)? = null

var onItemAddClick: (() -> Unit)? = null
var onItemEditClick: ((ItemData) -> Unit)? = null
var onItemDeleteClick: ((ItemData) -> Unit)? = null
var onItemEditClick: ((net.north101.android.ghplayertracker.livedata.ItemLiveData) -> Unit)? = null
var onItemDeleteClick: ((net.north101.android.ghplayertracker.livedata.ItemLiveData) -> Unit)? = null

var onNoteAddClick: (() -> Unit)? = null
var onNoteEditClick: ((CharacterAdapter.Note) -> Unit)? = null
var onNoteDeleteClick: ((CharacterAdapter.Note) -> Unit)? = null

fun updateItems(character: CharacterModel) {
fun updateItems(character: CharacterLiveData) {
val newItems = ArrayList<RecyclerItemCompare>()

if ((display.id and DisplayItems.Left.id) == DisplayItems.Left.id) {
Expand All @@ -60,25 +61,25 @@ class CharacterAdapter : RecyclerView.Adapter<BaseViewHolder<*>>() {
newItems.add(TextHeaderAdd("Items", {
onItemAddClick?.invoke()
}))
newItems.addAll(character.items.value!!.sortedBy { it.type.value }.map {
newItems.addAll(character.items.value.sortedBy { it.type.value }.map {
Item(it)
})
newItems.add(TextHeaderAdd("Notes", {
onNoteAddClick?.invoke()
}))
newItems.addAll(character.notes.value!!.map {
newItems.addAll(character.notes.value.map {
Note(it)
})
}

if ((display.id and DisplayItems.Right.id) == DisplayItems.Right.id) {
newItems.add(TextHeader("Perks"))
newItems.addAll(character.perks.value!!.map {
newItems.addAll(character.perks.value.map {
Perk(it)
})

newItems.add(TextHeader("Perk Notes"))
newItems.addAll(character.perkNotes.value!!.withIndex().map {
newItems.addAll(character.perkNotes.value.withIndex().map {
PerkNote(it.index, it.value)
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.Toolbar
import android.util.Log
import net.north101.android.ghplayertracker.data.Character
import net.north101.android.ghplayertracker.data.CharacterData
import org.androidannotations.annotations.*
Expand Down Expand Up @@ -53,27 +52,26 @@ open class CharacterFragment : Fragment(), OnBackPressedListener {
trackerResultModel = ViewModelProviders.of(this).get(TrackerResultModel::class.java)

if (state == null) {
characterModel.fromData(character)
characterModel.init(character)
} else {
Log.d("test", (state.getParcelable("character_model") as Character).name)
characterModel.fromData(state.getParcelable("character_model"))
characterModel.fromBundle(state.getBundle("character_model"))
}
}

override fun onSaveInstanceState(state: Bundle) {
state.putParcelable("character_model", characterModel.toData())
state.putBundle("character_model", characterModel.toBundle())

super.onSaveInstanceState(state)
}

@AfterViews
fun afterViews() {
if (trackerResultModel.gold > 0) {
characterModel.gold.value = (characterModel.gold.value ?: 0) + trackerResultModel.gold
characterModel.character.gold.value += trackerResultModel.gold
trackerResultModel.gold = 0
}
if (trackerResultModel.xp > 0) {
characterModel.xp.value = (characterModel.xp.value ?: 0) + trackerResultModel.xp
characterModel.character.xp.value += trackerResultModel.xp
trackerResultModel.xp = 0
}

Expand Down Expand Up @@ -113,7 +111,7 @@ open class CharacterFragment : Fragment(), OnBackPressedListener {
}
listAdapter1.onItemEditClick = {
val args = Bundle()
args.putInt("index", characterModel.items.value!!.indexOf(it))
args.putInt("index", characterModel.character.items.value.indexOf(it))

val fragment = CharacterItemDialog_.builder().build()
fragment.arguments = args
Expand All @@ -122,8 +120,8 @@ open class CharacterFragment : Fragment(), OnBackPressedListener {
fragment.show(fragmentManager, "CharacterNoteDialog")
}
listAdapter1.onItemDeleteClick = {
characterModel.items.value!!.remove(it)
characterModel.items.value = characterModel.items.value
characterModel.character.items.value.remove(it)
characterModel.character.items.value = characterModel.character.items.value

}
listAdapter1.onNoteAddClick = {
Expand All @@ -134,7 +132,7 @@ open class CharacterFragment : Fragment(), OnBackPressedListener {
}
listAdapter1.onNoteEditClick = {
val args = Bundle()
args.putInt("index", characterModel.notes.value!!.indexOf(it.note))
args.putInt("index", characterModel.character.notes.value.indexOf(it.note))

val fragment = CharacterNoteDialog_.builder().build()
fragment.arguments = args
Expand All @@ -143,8 +141,8 @@ open class CharacterFragment : Fragment(), OnBackPressedListener {
fragment.show(fragmentManager, "CharacterNoteDialog")
}
listAdapter1.onNoteDeleteClick = {
characterModel.notes.value!!.remove(it.note)
characterModel.notes.value = characterModel.notes.value
characterModel.character.notes.value.remove(it.note)
characterModel.character.notes.value = characterModel.character.notes.value
}

val listLayoutManager1 = GridLayoutManager(context, 3)
Expand Down Expand Up @@ -186,59 +184,59 @@ open class CharacterFragment : Fragment(), OnBackPressedListener {
listAdapter1.display = CharacterAdapter.DisplayItems.Left
listAdapter2.display = CharacterAdapter.DisplayItems.Right
}
listAdapter1.updateItems(characterModel)
listAdapter2.updateItems(characterModel)
listAdapter1.updateItems(characterModel.character)
listAdapter2.updateItems(characterModel.character)

characterModel.items.observe(this, Observer {
listAdapter1.updateItems(characterModel)
listAdapter2.updateItems(characterModel)
characterModel.character.items.observe(this, Observer {
listAdapter1.updateItems(characterModel.character)
listAdapter2.updateItems(characterModel.character)
})
characterModel.notes.observe(this, Observer {
listAdapter1.updateItems(characterModel)
listAdapter2.updateItems(characterModel)
characterModel.character.notes.observe(this, Observer {
listAdapter1.updateItems(characterModel.character)
listAdapter2.updateItems(characterModel.character)
})
}

override fun onBackPressed(): Boolean {
if (characterModel.toData() == character)
if (characterModel.character.toParcel() == character)
return false

val builder = AlertDialog.Builder(context!!)
builder.setTitle("Would you like to save your changes?")
.setPositiveButton("Yes") { dialog, which ->
saveCharacter(characterModel.toData(), Runnable {
fragmentManager!!.popBackStack()
})
}
.setNegativeButton("No") { dialog, which ->
.setPositiveButton("Yes") { dialog, which ->
saveCharacter(characterModel.character.toParcel(), Runnable {
fragmentManager!!.popBackStack()
}
.setNeutralButton("Cancel") { dialog, which ->
dialog.dismiss()
}
.show()
})
}
.setNegativeButton("No") { dialog, which ->
fragmentManager!!.popBackStack()
}
.setNeutralButton("Cancel") { dialog, which ->
dialog.dismiss()
}
.show()

return true
}

@OptionsItem(R.id.start)
fun onMenuStartClick() {
val args = Bundle()
args.putParcelable("character", characterModel.toData())
args.putParcelable("character", characterModel.character.toParcel())

val fragment = TrackerFragment_()
fragment.arguments = args
fragment.setTargetFragment(this, 1)

fragmentManager!!.beginTransaction()
.replace(R.id.content, fragment)
.addToBackStack(null)
.commit()
.replace(R.id.content, fragment)
.addToBackStack(null)
.commit()
}

@OptionsItem(R.id.save)
fun onMenuSaveClick() {
character = characterModel.toData()
character = characterModel.character.toParcel()
saveCharacter(character, null)
}

Expand Down Expand Up @@ -304,9 +302,9 @@ open class CharacterEditLevelDialog : CharacterNumberDialog() {
override val title = "Edit Level"

override var value: Int
get() = characterModel.level.value!!
get() = characterModel.character.level.value
set(value) {
characterModel.level.value = value
characterModel.character.level.value = value
}
}

Expand All @@ -316,9 +314,9 @@ open class CharacterEditXPDialog : CharacterNumberDialog() {
override val title = "Edit XP"

override var value: Int
get() = characterModel.xp.value!!
get() = characterModel.character.xp.value
set(value) {
characterModel.xp.value = value
characterModel.character.xp.value = value
}
}

Expand All @@ -328,8 +326,8 @@ open class CharacterEditGoldDialog : CharacterNumberDialog() {
override val title = "Edit Gold"

override var value: Int
get() = characterModel.gold.value!!
get() = characterModel.character.gold.value
set(value) {
characterModel.gold.value = value
characterModel.character.gold.value = value
}
}
Loading

0 comments on commit 8d55e57

Please sign in to comment.