/* Ċ Ġ* ĠCreated Ġby ĠAnd ri i ĠK oval ch uk Ċ Ġ* ĠCopyright Ġ( C ) Ġ 2 0 2 1 . Ġro ove Ċ Ġ* Ċ Ġ* ĠThis Ġprogram Ġis Ġfree Ġsoftware : Ġyou Ġcan Ġredistribute Ġit Ġand / or Ġmodify Ċ Ġ* Ġit Ġunder Ġthe Ġterms Ġof Ġthe ĠGNU ĠGeneral ĠPublic ĠLicense Ġas Ġpublished Ġby Ċ Ġ* Ġthe ĠFree ĠSoftware ĠFoundation , Ġeither Ġversion Ġ 3 Ġof Ġthe ĠLicense , Ġor Ċ Ġ* Ġ( at Ġyour Ġoption ) Ġany Ġlater Ġversion . Ċ Ġ* Ċ Ġ* ĠThis Ġprogram Ġis Ġdistributed Ġin Ġthe Ġhope Ġthat Ġit Ġwill Ġbe Ġuseful , Ċ Ġ* Ġbut ĠWITHOUT ĠANY ĠWARRANTY ; Ġwithout Ġeven Ġthe Ġimplied Ġwarranty Ġof Ċ Ġ* ĠMERCHANTABILITY Ġor ĠFITNESS ĠFOR ĠA ĠPARTICULAR ĠPURPOSE . Ġ ĠSee Ġthe Ċ Ġ* ĠGNU ĠGeneral ĠPublic ĠLicense Ġfor Ġmore Ġdetails . Ċ Ġ* Ċ Ġ* ĠYou Ġshould Ġhave Ġreceived Ġa Ġcopy Ġof Ġthe ĠGNU ĠGeneral ĠPublic ĠLicense Ċ Ġ* Ġalong Ġwith Ġthis Ġprogram . Ġ ĠIf Ġnot , Ġsee Ġhttps :// www . gnu . org / licenses Ċ Ġ*/ Ċ Ċ package Ġcom . mm dev . data . repository . user Ċ Ċ import Ġandroid . net . Uri Ċ import Ġandroid . text . format . DateFormat Ċ import Ġcom . google . firebase . firestore . FieldValue Ċ import Ġcom . google . firebase . firestore . Firebase Fire store Ċ import Ġcom . google . firebase . storage . Storage Reference Ċ import Ġcom . mm dev . data . core . Base Repository Ċ import Ġcom . mm dev . data . core . My Schedulers Ċ import Ġcom . mm dev . data . core . firebase . as Single Ċ import Ġcom . mm dev . data . core . firebase . delete As Comp letable Ċ import Ġcom . mm dev . data . core . log . log Debug Ċ import Ġcom . mm dev . data . datasource . User DataSource Ċ import Ġcom . mm dev . domain . photo . Photo Item Ċ import Ġcom . mm dev . domain . user . I Settings Repository Ċ import Ġcom . mm dev . domain . user . data . User Item Ċ import Ġio . reactivex . rx java 3 . core . Comp letable Ċ import Ġio . reactivex . rx java 3 . core . Observable Ċ import Ġio . reactivex . rx java 3 . internal . operators . comp letable . Comp letable Create Ċ import Ġjava . util .* Ċ import Ġjavax . inject . Inject Ċ Ċ /** Ċ Ġ* ĠRepository Ġto Ġyour Ġinfo Ċ Ġ*/ Ċ Ċ class ĠSettings Repository Impl Ġ@ Inject Ġconstructor ( Ċ ĉ private Ġval Ġfs : ĠFirebase Fire store , Ċ ĉ private Ġval Ġstorage : ĠStorage Reference , Ċ ĉ private Ġval Ġuser DataSource : ĠUser DataSource Ċ ): ĠBase Repository (), ĠI Settings Repository Ġ{ ĊĉĊĉĊ ĉ comp anion Ġobject Ġ{ Ċĉ ĉ private Ġconst Ġval ĠUSER _ MAIN _ PH OTO _ FIELD Ġ= Ġ" base UserInfo . main Photo Url " Ċĉ ĉ private Ġconst Ġval ĠUSER _ PH OT OS _ LIST _ FIELD Ġ= Ġ" photo URLs " ĊĉĉĊĉ ĉ private Ġconst Ġval ĠPRO FILE _ FOLDER _ STORAGE _ IMG Ġ= Ġ" profile Photos " Ċ ĉ } ĊĉĊ ĉ override Ġfun Ġdelete Photo ( Ċĉ ĉ user Item : ĠUser Item , Ċĉ ĉ photo Item : ĠPhoto Item , Ċĉ ĉ is Main Photo Deleting : ĠBoolean Ċ ĉ ): ĠComp letable Ġ= ĠComp letable . concat Array ( Ċĉ ĉ // update Ġlist Ġfield Ċĉ ĉ user DataSource . update Fire store User Field ( Ċĉĉ ĉ id Ġ= Ġuser Item . base UserInfo . userId , Ċĉĉ ĉ field Ġ= ĠUSER _ PH OT OS _ LIST _ FIELD , Ċĉĉ ĉ value Ġ= ĠField Value . array Remove ( photo Item ) Ċĉ ĉ ), Ċĉ ĉ // update Ġmain Photo Ġurl Ġfield Ċĉ ĉ if Ġ( is Main Photo Deleting ) Ġ{ Ċĉĉ ĉ user DataSource . update Fire store User Field ( Ċĉĉĉ ĉ id Ġ= Ġuser Item . base UserInfo . userId , Ċĉĉĉ ĉ field Ġ= ĠUSER _ MAIN _ PH OTO _ FIELD , Ċĉĉĉ ĉ value Ġ= Ġuser Item . photo URLs . minus ( photo Item )[ 0 ]. file Url Ċĉĉ ĉ ) Ċĉ ĉ } Ċĉ ĉ else ĠComp letable . complete (), Ċĉ ĉ // delete Ġphoto Ġfrom Ġstorage Ġif Ġit Ġis Ġnot Ġfrom Ġfacebook Ċĉ ĉ if Ġ( photo Item . fileName Ġ!= ĠPhoto Item . FACE BOOK _ PH OTO _ NAME ) Ċĉĉ ĉ storage Ċĉĉĉ ĉ . child ( GENERAL _ FOLDER _ STORAGE _ IMG ) Ċĉĉĉ ĉ . child ( PROFILE _ FOLDER _ STORAGE _ IMG ) Ċĉĉĉ ĉ . child ( user Item . base UserInfo . userId ) Ċĉĉĉ ĉ . child ( photo Item . fileName ) Ċĉĉĉ ĉ . delete As Comp letable () Ċĉ ĉ else ĠComp letable . complete () Ċ ĉ ) ĊĉĊ ĉ override Ġfun Ġdelete My self ( user : ĠUser Item ): ĠComp letable Ġ= ĠComp letable Create { Ġemitter Ġ-> ĊĉĉĊĉ ĉ val Ġref Ġ= Ġfs . collection ( USERS _ COLLECTION ). document ( user . base UserInfo . userId ) ĊĉĉĊĉ ĉ val Ġmatched Listener Ġ= Ċĉĉ ĉ ref . collection ( USER _ MATCH ED _ COLLECTION ) Ċĉĉĉ ĉ . add Snapshot Listener Ġ{ Ġsnapshots , Ġe Ġ-> Ċĉĉĉĉ ĉ if Ġ( e Ġ!= Ġnull ) Ġ{ Ċĉĉĉĉĉ ĉ emitter . onError ( e ) Ċĉĉĉĉĉ ĉ return @ add Snapshot Listener Ċĉĉĉĉ ĉ } Ċĉĉĉĉ ĉ if Ġ( snapshots Ġ!= Ġnull Ġ&& Ġsnapshots . documents . isNotEmpty ()) Ġ{ Ċĉĉĉĉĉ ĉ for Ġ( doc Ġin Ġsnapshots . documents ) Ċĉĉĉĉĉĉ ĉ doc . reference . delete () Ċĉĉĉĉ ĉ } Ċĉĉĉĉ ĉ else Ġlog Debug ( TAG , Ġ" matched Ġempty Ġor Ġdeleted ") ĊĉĉĉĉĉĊĉĉĉ ĉ } ĊĉĉĊĉ ĉ val Ġconversations Listener Ġ= Ċĉĉ ĉ ref . collection ( CONVER S ATIONS _ COLLECTION ) Ċĉĉĉ ĉ . add Snapshot Listener Ġ{ Ġsnapshots , Ġe Ġ-> Ċĉĉĉĉ ĉ if Ġ( e Ġ!= Ġnull ) Ġ{ Ċĉĉĉĉĉ ĉ emitter . onError ( e ) Ċĉĉĉĉĉ ĉ return @ add Snapshot Listener Ċĉĉĉĉ ĉ } Ċĉĉĉĉ ĉ if Ġ( snapshots Ġ!= Ġnull Ġ&& Ġsnapshots . documents . isNotEmpty ()) Ġ{ Ċĉĉĉĉĉ ĉ for Ġ( doc Ġin Ġsnapshots . documents ) Ċĉĉĉĉĉĉ ĉ doc . reference . delete () Ċĉĉĉĉ ĉ } Ċĉĉĉĉ ĉ else Ġlog Debug ( TAG , Ġ" conversation Ġempty Ġor Ġdeleted ") Ċĉĉĉ ĉ } ĊĉĉĊĉ ĉ val Ġskipped Listener Ġ= Ċĉĉ ĉ ref . collection ( USER _ SKIP PED _ COLLECTION ) Ċĉĉĉ ĉ . add Snapshot Listener Ġ{ Ġsnapshots , Ġe Ġ-> Ċĉĉĉĉ ĉ if Ġ( e Ġ!= Ġnull ) Ġ{ Ċĉĉĉĉĉ ĉ emitter . onError ( e ) Ċĉĉĉĉĉ ĉ return @ add Snapshot Listener Ċĉĉĉĉ ĉ } Ċĉĉĉĉ ĉ if Ġ( snapshots Ġ!= Ġnull Ġ&& Ġsnapshots . documents . isNotEmpty ()) Ġ{ Ċĉĉĉĉĉ ĉ for Ġ( doc Ġin Ġsnapshots . documents ) Ċĉĉĉĉĉĉ ĉ doc . reference . delete () Ċĉĉĉĉ ĉ } Ċĉĉĉĉ ĉ else Ġlog Debug ( TAG , Ġ" skipped Ġempty Ġor Ġdeleted ") Ċĉĉĉ ĉ } ĊĉĉĊĉ ĉ val Ġlik ed Listener Ġ= Ċĉĉ ĉ ref . collection ( USER _ LIK ED _ COLLECTION ) Ċĉĉĉ ĉ . add Snapshot Listener Ġ{ Ġsnapshots , Ġe Ġ-> Ċĉĉĉĉ ĉ if Ġ( e Ġ!= Ġnull ) Ġ{ Ċĉĉĉĉĉ ĉ emitter . onError ( e ) Ċĉĉĉĉĉ ĉ return @ add Snapshot Listener Ċĉĉĉĉ ĉ } Ċĉĉĉĉ ĉ if Ġ( snapshots Ġ!= Ġnull Ġ&& Ġsnapshots . documents . isNotEmpty ()) Ġ{ Ċĉĉĉĉĉ ĉ for Ġ( doc Ġin Ġsnapshots . documents ) Ċĉĉĉĉĉĉ ĉ doc . reference . delete () Ċĉĉĉĉ ĉ } Ċĉĉĉĉ ĉ else Ġlog Debug ( TAG , Ġ" lik ed Ġempty Ġor Ġdeleted ") Ċĉĉĉ ĉ } ĊĉĉĊĉ ĉ // base Ġdelete Ċĉ ĉ ref . delete () Ċĉĉ ĉ . add On Success Listener Ġ{ Ċĉĉĉ ĉ // general Ġdelete Ċĉĉĉ ĉ matched Listener . remove () Ċĉĉĉ ĉ con versations Listener . remove () Ċĉĉĉ ĉ lik ed Listener . remove () Ċĉĉĉ ĉ skipped Listener . remove () Ċĉĉĉ ĉ emitter . onComplete () Ċĉĉ ĉ }. add On Failure Listener Ġ{ Ġemitter . onError ( it ) Ġ} ĊĉĉĊĉ ĉ emitter . set C ancellable Ġ{ Ċĉĉ ĉ matched Listener . remove () Ċĉĉ ĉ con versations Listener . remove () Ċĉĉ ĉ lik ed Listener . remove () Ċĉĉ ĉ skipped Listener . remove () Ċĉ ĉ } Ċ ĉ }. subscribe On ( My Schedulers . io ()) ĊĉĊ ĉ override Ġfun Ġupload UserProfile Photo ( Ċĉ ĉ user Item : ĠUser Item , Ċĉ ĉ photo Uri : ĠString Ċ ĉ ): ĠObservable < Array < Photo Item >> Ġ{ Ċĉ ĉ val Ġname Photo Ġ= Ġ"${ DateFormat . format (" yyyy - MM - dd _ hh mm ss ", ĠDate ()) }_ user _ photo . jpg " ĊĉĉĊĉ ĉ val Ġstorage Ref Ġ= Ġstorage Ċĉĉ ĉ . child ( GENERAL _ FOLDER _ STORAGE _ IMG ) Ċĉĉ ĉ . child ( PROFILE _ FOLDER _ STORAGE _ IMG ) Ċĉĉ ĉ . child ( user Item . base UserInfo . userId ) Ċĉĉ ĉ . child ( name Photo ) ĊĉĉĊĉ ĉ return Ġstorage Ref . put File ( Uri . parse ( photo Uri )) Ċĉĉ ĉ . as Single () Ċĉĉ ĉ . concat Map Ġ{ Ġstorage Ref . download Url . as Single () Ġ} Ċĉĉ ĉ . map Ġ{ Ġdownload Url Ġ-> ĠPhoto Item ( fileName Ġ= Ġname Photo , Ġfile Url Ġ= Ġdownload Url . toString ()) Ġ} Ċĉĉ ĉ . flatMap Ġ{ Ċĉĉĉ ĉ user DataSource . update Fire store User Field ( Ċĉĉĉĉ ĉ user Item . base UserInfo . userId , ĠUSER _ PH OT OS _ LIST _ FIELD , ĠField Value . array Union ( it ) Ċĉĉĉ ĉ ). to Single Ġ{ Ċĉĉĉĉ ĉ array Of ( it ) Ċĉĉĉ ĉ } Ċĉĉ ĉ } Ċĉĉ ĉ . to Observable () Ċĉĉ ĉ . onError Return Ġ{ Ġempty Array < Photo Item >() Ġ} Ċ ĉ } ĊĉĊĉ Ċ }