Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
package com.sumin.shoppinglist.data

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.sumin.shoppinglist.domain.ShopItem
import com.sumin.shoppinglist.domain.ShopListRepository

object ShopListRepositoryImpl : ShopListRepository {

private val shopListLD = MutableLiveData<List<ShopItem>>()
private val shopList = mutableListOf<ShopItem>()

private var autoIncrementId = 0

init {
for (i in 0 until 10){
val item = ShopItem("Name $i", i, true)
shopList.add(item)
}
}

override fun addShopItem(shopItem: ShopItem) {
if (shopItem.id == ShopItem.UNDEFINED_ID){
shopItem.id = autoIncrementId++
}
shopList.add(shopItem)
updateList()
}

override fun deleteShopItem(shopItem: ShopItem) {
shopList.remove(shopItem)
updateList()
}

override fun editShopItem(shopItem: ShopItem) {
Expand All @@ -30,7 +42,11 @@ object ShopListRepositoryImpl : ShopListRepository {
return shopList.find { it.id == id } ?: throw RuntimeException("Error")
}

override fun getShopList(): List<ShopItem> {
return shopList.toList()
override fun getShopList(): LiveData<List<ShopItem>> {
return shopListLD
}

private fun updateList(){
shopListLD.value = shopList.toList()
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.sumin.shoppinglist.domain

import androidx.lifecycle.LiveData

class GetShopListUseCase(private val shopListRepository: ShopListRepository) {

fun getShopList(): List<ShopItem>{
fun getShopList():LiveData<List<ShopItem>> {
return shopListRepository.getShopList()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.sumin.shoppinglist.domain

import androidx.lifecycle.LiveData

interface ShopListRepository {

fun addShopItem(shopItem: ShopItem)
Expand All @@ -10,5 +12,5 @@ interface ShopListRepository {

fun getShopItemById(id: Int): ShopItem

fun getShopList(): List<ShopItem>
fun getShopList(): LiveData<List<ShopItem>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,26 @@ package com.sumin.shoppinglist.presentation

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import com.sumin.shoppinglist.R

class MainActivity : AppCompatActivity() {

private lateinit var viewModel: MainViewModel

private var count = 0

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewModel = ViewModelProvider(this)[MainViewModel::class.java]
viewModel.shopList.observe(this){
if (count == 0){
count++
val item = it[0]
viewModel.deleteShopItem(item)
}

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.sumin.shoppinglist.presentation

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.sumin.shoppinglist.data.ShopListRepositoryImpl
import com.sumin.shoppinglist.domain.DeleteShopItemUseCase
import com.sumin.shoppinglist.domain.EditShopItemUseCase
import com.sumin.shoppinglist.domain.GetShopListUseCase
import com.sumin.shoppinglist.domain.ShopItem
import com.sumin.shoppinglist.domain.ShopListRepository

class MainViewModel : ViewModel() {

private val repository = ShopListRepositoryImpl

private val getShopListUseCase = GetShopListUseCase(repository)
private val deleteShopItemUseCase = DeleteShopItemUseCase(repository)
private val editShopItemUseCase = EditShopItemUseCase(repository)

val shopList = getShopListUseCase.getShopList()

fun changeEnabledState(shopItem: ShopItem) {
val newItem = shopItem.copy(enabled = !shopItem.enabled)
editShopItemUseCase.editShopItem(newItem)


}

fun deleteShopItem(shopItem: ShopItem) {
deleteShopItemUseCase.deleteShopItem(shopItem)

}


}