Skip to main content

Launcher

There is a situation when it is necessary to synchronize the work of the cleaners. For this purpose, a separate functionality was created that allows you to do this without any difficulty.

important

This functionality synchronizes the work of existing cleaners. Nothing prevents you from subscribing to a specific cleaner and tracking its work in the launcher.

Analysis​

The following code example demonstrates how to use the analysis operation:

// Get the launcher through the cleaner manager
val launcher = CleanerManagerFactory.cleanerManager.launcher

// List of cleaners for the launcher
val cleanerList: List<Cleaner>

// Get coroutine scope to call cleaner library functions
val cleanerScope = CleanerManagerFactory.cleanerManager.scope

// Run all operations related to libraries only in the cleaner library
cleanerScope.launch {

// Start the analysis process with the following call
launcher.analyze(
cleaners = cleanerList,
isFullRefresh = true
)
}

A cleaner is represented by an object that implements the CleanerLauncher interface, a analyze method that takes the following two parameters:

  • cleaners: List<Cleaner> - list of cleaners
  • isFullRefresh: Boolean - true - when analyzing, the cleaner scans the system again; false - the method returns the cached results of the parsing (only non-deleted items).

Cleaning​

Use the following code example to clear items.

// Get the launcher through the cleaner manager
val launcher = CleanerManagerFactory.cleanerManager.launcher

// Get coroutine scope to call cleaner library functions
val cleanerScope = CleanerManagerFactory.cleanerManager.scope

// Get a list of items to remove
val trashItemList: List<TrashItem>

// Run all operations related to libraries only in the cleaner library
cleanerScope.launch {
// Start the analysis process with the following call
launcher.clean(
items = trashItemList
)
}

A cleaner is represented by an object that implements the Cleaner interface, whose clean method accepts content: List<TrashItem> (list of items to remove)

Getting results​

In order to get the state of the cleaner and the results of its work, you need to subscribe to its state as follows:

// Get the launcher through the cleaner manager
val launcher = CleanerManagerFactory.cleanerManager.launcher

// Get coroutine scope to call cleaner library functions
val cleanerScope = CleanerManagerFactory.cleanerManager.scope

// Run all operations related to libraries only in the cleaner library
cleanerScope.launch {
// Subscribe to the states of the launcher
launcher.stateFlow.collect { cleanerState -> // cleanerState: CleanerState
// Do something...
}
}

Launcher cleaner inherits Cleaner interface that provides stateFlow: StateFlow (StateFlow) which emits CleanerState objects containing information about the condition of the cleaner. Here are the following possible states of the cleaner:

Inactive - this is the initial state of the cleaner before starting work.

Analyzing - this state indicates that the cleaner is in the process of analyzing data. It includes the following fields:

  • itemCount: Int - number of items found
  • size: Long - size of found elements in bits

Analysed - this state indicates that the cleaner has finished analyzing the data. It includes the following fields:

  • items: List<TrashItem> - list of found items
  • error: Trowable? - an error occurred during the analysis process

Cleaning -this state indicates that the cleaner is in the process of deleting data. It includes the following fields:

  • itemCount: Int - number of items found
  • size: Long - size of found elements in bits

Cleaned - this state indicates that the cleaner has completed the data cleaning process. It includes the following fields:

  • items: List<TrashItem> - list of deleted items
  • error: Trowable? - an error occurred during the cleaning process

Links - CleanerState, TrashItem, CleanerLauncher.