Overview
The SectorsRepository provides CRUD operations for managing sectors within greenhouses. Sectors are subdivisions of greenhouses where devices are installed. All methods are suspend functions that return Result<T> for consistent error handling.
Methods
getSectorsByTenantId()
Fetches all sectors for a specific tenant.
suspend fun getSectorsByTenantId(tenantId: Long): Result<List<Sector>>
Parameters
The tenant ID to filter sectors by
Returns
Result containing a list of Sector objects or an error
createSector()
Creates a new sector for a tenant.
suspend fun createSector(
tenantId: Long,
greenhouseId: Long,
name: String?
): Result<Sector>
Parameters
The tenant ID the sector belongs to
The greenhouse ID the sector belongs to
The name of the sector (nullable)
Returns
Result containing the created Sector object or an error
updateSector()
Updates an existing sector.
suspend fun updateSector(
tenantId: Long,
sectorId: Long,
greenhouseId: Long?,
name: String?
): Result<Sector>
Parameters
The tenant ID the sector belongs to
New greenhouse ID (optional)
Returns
Result containing the updated Sector object or an error
deleteSector()
Deletes a sector by ID.
suspend fun deleteSector(tenantId: Long, sectorId: Long): Result<Unit>
Parameters
The tenant ID the sector belongs to
Returns
Result indicating success or error
Usage Example
class SectorsViewModel(
private val sectorsRepository: SectorsRepository
) : ViewModel() {
private val _sectors = MutableStateFlow<List<Sector>>(emptyList())
val sectors: StateFlow<List<Sector>> = _sectors.asStateFlow()
private val _error = MutableStateFlow<String?>(null)
val error: StateFlow<String?> = _error.asStateFlow()
fun loadSectors(tenantId: Long) {
viewModelScope.launch {
sectorsRepository.getSectorsByTenantId(tenantId)
.onSuccess { _sectors.value = it }
.onFailure { _error.value = it.message }
}
}
fun createSector(
tenantId: Long,
greenhouseId: Long,
name: String?
) {
viewModelScope.launch {
sectorsRepository.createSector(
tenantId = tenantId,
greenhouseId = greenhouseId,
name = name
)
.onSuccess { loadSectors(tenantId) }
.onFailure { _error.value = it.message }
}
}
fun updateSector(
tenantId: Long,
sectorId: Long,
greenhouseId: Long?,
name: String?
) {
viewModelScope.launch {
sectorsRepository.updateSector(
tenantId = tenantId,
sectorId = sectorId,
greenhouseId = greenhouseId,
name = name
)
.onSuccess { loadSectors(tenantId) }
.onFailure { _error.value = it.message }
}
}
fun deleteSector(tenantId: Long, sectorId: Long) {
viewModelScope.launch {
sectorsRepository.deleteSector(tenantId, sectorId)
.onSuccess { loadSectors(tenantId) }
.onFailure { _error.value = it.message }
}
}
}
Dependency Injection
Inject the repository using Koin in your Composables:
import org.koin.compose.viewmodel.koinViewModel
@Composable
fun SectorsScreen(tenantId: Long) {
val viewModel: SectorsViewModel = koinViewModel()
val sectors by viewModel.sectors.collectAsState()
LaunchedEffect(tenantId) {
viewModel.loadSectors(tenantId)
}
// UI implementation
}