This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | class TeamActivity : FragmentActivity() { | |
| private val viewModel: TeamViewModel by bindViewModel() | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | inline fun <reified ViewModelT : ViewModel> ComponentActivity.bindViewModel() = | |
| bindViewModel(ViewModelT::class, viewModelFactoryProvider) | |
| @PublishedApi | |
| internal fun <ViewModelT : ViewModel> ComponentActivity.bindViewModel( | |
| viewModelType: KClass<ViewModelT> | |
| ): Lazy<ViewModelT> = lazy { | |
| ViewModelProvider(this).get(viewModelType.java) | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | class TeamRepository(appSchedulers: AppSchedulers) { | |
| private val viewState by lazy(::createViewStateLiveData) | |
| private fun createViewStateLiveData(): LiveData<ViewState> = | |
| teamRepository.teamMembersStream() | |
| .map(::mapPresentingState) | |
| .onErrorReturn(::mapErrorState) | |
| .startWith(ViewState.Loading) | |
| .subscribeOn(appSchedulers.io) | |
| .observeOn(appSchedulers.main) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | private val bindingController by lazy { | |
| FilesBindingController(viewModel::onFileOpened) | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | uploader.uploadAttachment(request.filename, request.file, request.mimeType) | |
| .subscribeOn(appRxSchedulers.io) | |
| .observeOn(appRxSchedulers.main) | |
| .subscribeBy( | |
| onError = { error -> | |
| // Display error alert | |
| }, | |
| onComplete = { | |
| // Display completed Snackbar | |
| }, | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | fun uploadAttachment( | |
| filename: String, file: File, mimeType: String | |
| ): Observable<AttachmentUploadRemoteResult> { | |
| val progressEmitter = PublishSubject.create<Double>() | |
| val uploadRequest = createUploadRequest( | |
| filename, file, mimeType, progressEmitter | |
| ) | |
| val uploadResult = uploadRequest | |
| .map<AttachmentUploadRemoteResult> { | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | private fun createUploadRequest( | |
| filename: String, | |
| file: File, | |
| mimeType: String, | |
| progressEmitter: PublishSubject<Double> | |
| ): Single<AttachmentUploadedRemoteDto> { | |
| val requestBody = createUploadRequestBody(file, mimeType, progressEmitter) | |
| return remoteApi.attachFile( | |
| filename = filename.toPlainTextBody(), | |
| mimeType = mimeType.toPlainTextBody(), | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | private fun createUploadRequestBody( | |
| file: File, | |
| mimeType: String, | |
| progressEmitter: PublishSubject<Double> | |
| ): RequestBody { | |
| val fileRequestBody = file.asRequestBody(mimeType.toMediaType()) | |
| return CountingRequestBody(fileRequestBody) { bytesWritten, contentLength -> | |
| val progress = 1.0 * bytesWritten / contentLength | |
| progressEmitter.onNext(progress) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | sealed class CountingRequestResult<ResultT> { | |
| data class Progress<ResultT>( | |
| val progressFraction: Double | |
| ) : CountingRequestResult<ResultT>() | |
| data class Completed<ResultT>( | |
| val result: ResultT | |
| ) : CountingRequestResult<ResultT>() | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | class CountingRequestBody(...) : RequestBody() { | |
| ... | |
| @Throws(IOException::class) | |
| override fun writeTo(sink: BufferedSink) { | |
| val countingSink = CountingSink(sink, this, onProgressUpdate) | |
| val bufferedSink = countingSink.buffer() | |
| requestBody.writeTo(bufferedSink) |