package com.example.lab2;

import android.app.Application;

import androidx.lifecycle.LiveData;

import java.util.List;

public class PhoneRepository {
    private final PhoneDao dao;
    private final LiveData<List<Phone>> phones;

    public PhoneRepository(Application application) {
        PhoneDatabase db = PhoneDatabase.getDatabase(application);
        dao = db.phoneDao();
        phones = dao.getAllPhones();
    }

    public LiveData<List<Phone>> getAllPhones() {
        return phones;
    }

    public void deleteAll() {
        PhoneDatabase.databaseWriteExecutor.execute(dao::deleteAll);
    }

    public void delete(Phone phone) {
        PhoneDatabase.databaseWriteExecutor.execute(() -> {
            dao.delete(phone);
        });
    }

    public void update(Phone phone) {
        PhoneDatabase.databaseWriteExecutor.execute(() -> {
            dao.update(phone);
        });
    }

    public void insert(Phone phone) {
        PhoneDatabase.databaseWriteExecutor.execute(() -> {
            dao.insert(phone);
        });
    }
}
