Feature: di

This commit is contained in:
ArtemEmelyanov 2023-11-10 16:34:42 +04:00
parent 74007b5979
commit f61608f10f
10 changed files with 136 additions and 7 deletions

View File

@ -7,11 +7,11 @@
<deviceKey>
<Key>
<type value="VIRTUAL_DEVICE_PATH" />
<value value="C:\Users\Admin\.android\avd\Pixel_7_Pro_API_30.avd" />
<value value="C:\Users\Admin\.android\avd\Pixel_2_API_30.avd" />
</Key>
</deviceKey>
</Target>
</targetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2023-10-12T15:08:00.916663800Z" />
<timeTargetWasSelectedWithDropDown value="2023-10-29T18:24:53.426357Z" />
</component>
</project>

View File

@ -7,13 +7,10 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class App : Application() {
val database by lazy { AppDatabase.getInstance(this)}
lateinit var container: AppContainer
override fun onCreate() {
super.onCreate()
// this.deleteDatabase("my-db")
CoroutineScope(Dispatchers.IO).launch {
AppDatabase.populateDatabase()
}
container = AppDataContainer(this)
}
}

View File

@ -0,0 +1,11 @@
package com.example.android_programming
import com.example.android_programming.repository.OrderRepository
import com.example.android_programming.repository.SneakerRepository
import com.example.android_programming.repository.UserRepository
interface AppContainer {
val sneakerRepo: SneakerRepository
val userRepo: UserRepository
val orderRepo: OrderRepository
}

View File

@ -0,0 +1,22 @@
package com.example.android_programming
import android.content.Context
import com.example.android_programming.database.AppDatabase
import com.example.android_programming.repository.OrderRepoImpl
import com.example.android_programming.repository.OrderRepository
import com.example.android_programming.repository.SneakerRepoImpl
import com.example.android_programming.repository.SneakerRepository
import com.example.android_programming.repository.UserRepoImpl
import com.example.android_programming.repository.UserRepository
class AppDataContainer(private val context: Context) : AppContainer {
override val sneakerRepo: SneakerRepository by lazy {
SneakerRepoImpl(AppDatabase.getInstance(context).sneakerDao())
}
override val userRepo: UserRepository by lazy {
UserRepoImpl(AppDatabase.getInstance(context).userDao())
}
override val orderRepo: OrderRepository by lazy {
OrderRepoImpl(AppDatabase.getInstance(context).orderDao())
}
}

View File

@ -0,0 +1,20 @@
package com.example.android_programming.repository
import com.example.android_programming.dao.OrderDao
import com.example.android_programming.model.Order
import com.example.android_programming.model.OrderSneaker
import com.example.android_programming.model.OrderWithSneakers
import kotlinx.coroutines.flow.Flow
class OrderRepoImpl(private val orderDao: OrderDao) : OrderRepository {
override suspend fun createOrder(order: Order): Long = orderDao.createOrder(order)
override suspend fun insertOrderSneaker(orderSneaker: OrderSneaker) = orderDao.insertOrderSneaker(orderSneaker)
override suspend fun delete(order: Order) = orderDao.delete(order)
override fun getOrderWithSneakers(id: Int): Flow<OrderWithSneakers> = orderDao.getOrderWithSneakers(id)
override fun getAllOrder(): Flow<List<Order>> = orderDao.getAllOrder()
}

View File

@ -0,0 +1,14 @@
package com.example.android_programming.repository
import com.example.android_programming.model.Order
import com.example.android_programming.model.OrderSneaker
import com.example.android_programming.model.OrderWithSneakers
import kotlinx.coroutines.flow.Flow
interface OrderRepository {
suspend fun createOrder(order: Order): Long
suspend fun insertOrderSneaker(orderSneaker: OrderSneaker)
suspend fun delete(order: Order)
fun getOrderWithSneakers(id: Int): Flow<OrderWithSneakers>
fun getAllOrder(): Flow<List<Order>>
}

View File

@ -0,0 +1,18 @@
package com.example.android_programming.repository
import com.example.android_programming.dao.SneakerDao
import com.example.android_programming.model.Sneaker
import kotlinx.coroutines.flow.Flow
class SneakerRepoImpl(private val sneakerDao: SneakerDao) : SneakerRepository {
override suspend fun insertSneaker(sneaker: Sneaker) = sneakerDao.insert(sneaker)
override suspend fun updateSneaker(sneaker: Sneaker) = sneakerDao.update(sneaker)
override suspend fun deleteSneaker(sneaker: Sneaker) = sneakerDao.delete(sneaker)
override suspend fun getSneakerById(id: Int): Sneaker = sneakerDao.getSneakerById(id)
override fun getAllSneakers(): Flow<List<Sneaker>> = sneakerDao.getAllSneakers()
}

View File

@ -0,0 +1,12 @@
package com.example.android_programming.repository
import com.example.android_programming.model.Sneaker
import kotlinx.coroutines.flow.Flow
interface SneakerRepository {
suspend fun insertSneaker(sneaker: Sneaker)
suspend fun updateSneaker(sneaker: Sneaker)
suspend fun deleteSneaker(sneaker: Sneaker)
suspend fun getSneakerById(id: Int): Sneaker
fun getAllSneakers(): Flow<List<Sneaker>>
}

View File

@ -0,0 +1,21 @@
package com.example.android_programming.repository
import com.example.android_programming.dao.UserDao
import com.example.android_programming.model.User
import com.example.android_programming.model.UserWithOrder
import kotlinx.coroutines.flow.Flow
class UserRepoImpl(private val userDao: UserDao) : UserRepository {
override suspend fun createUser(user: User) = userDao.createUser(user)
override suspend fun updateUser(user: User) = userDao.updateUser(user)
override suspend fun deleteUser(user: User) = userDao.deleteUser(user)
override suspend fun getUserById(id: Int): User = userDao.getUserById(id)
override suspend fun getUserByEmail(email: String): User = userDao.getUserByEmail(email)
override fun getUserOrders(id: Int): Flow<UserWithOrder> = userDao.getUserOrders(id)
}

View File

@ -0,0 +1,14 @@
package com.example.android_programming.repository
import com.example.android_programming.model.User
import com.example.android_programming.model.UserWithOrder
import kotlinx.coroutines.flow.Flow
interface UserRepository {
suspend fun createUser(user: User)
suspend fun updateUser(user: User)
suspend fun deleteUser(user: User)
suspend fun getUserById(id: Int): User
suspend fun getUserByEmail(email: String): User
fun getUserOrders(id: Int) : Flow<UserWithOrder>
}