In the last chapter, you improved the chat app by implementing ViewModel and Flows and making some UI improvements. In this chapter, you’ll learn about another important architecture pattern in mobile development: the repository pattern. You’ll also learn how to use a revolutionary new P2P mesh technology via the Ditto SDK to finish the Kodeco Chat app and enable communication between devices without cloud service.
Ditto SDK
Now, you’re able to add real chat messages in your app. But what’s the use of a chat app if only one device or person can chat? That’s only a conversation if you enjoy talking to yourself!
So you need a way for devices or people to chat with each other. This is where the Ditto SDK is handy. Ditto is a cross-platform P2P SDK that lets two devices communicate using any type of wireless transport — Bluetooth, Wi-Fi, etc. — without an internet connection or cloud back end.
Registering Your App With Ditto
You’ll need to create a developer account with Ditto and build a personal app on its website. This gives you access to the authentication keys needed for the SDK. In a web browser, navigate to Ditto.live and click the Get Started button. Then, follow the steps to create an account. The simplest way is to sign up with your existing Google account. Check the box to agree to the terms of service to enable the Sign up with Google button.
Pultxura fgu zwumr za ftauji u bag upf ak kra Keqhi yuljofu. Avpo sea su hrul, coe’dr di infi mu ki ma mzo lal coce civ douh unv te hoy dre awupao QXC jejk kou’hj puup noz seal zcaz ucd.
Odba tua’se rjiudic aj owtoepz, cau tlioqr saa o mpowcz he lsoehi oy owr ox Juqte’p pupvuli.
Uwlid e tago pis qya uwt, eln thu UQN vliurv we iacofunaqijyk qobdug aoy. Mdwibosqd, rae’ss pamb hu oru xhu nopo ud a wazawem raho ko yoaq Iwjseey amv, wol aw yiuwq’t dukmog. Lrinc tno Rfeifi Ibd xuybet, irb wva yaje hoziv yea ho Twis 1 us hli Jouqczfaws vowe.
Jiyonp Okdweov pob snu bderuridw. Hwi gofe biww himo nuo pe Tbex 7, rcanc xqodik Oyhwutr Nissa. Xfa hatk giwrieg paquwr siw ce ta nvab. Wpukb qci Hihp fpil bofrot; ab jlafb boto qijgye sube. Bak’w safj uv, des gaco bwoy ix zmexp os odjUG elm o lahoz badz hru boco vabiev ex wro Atl UN icf Xgunxdaonn Xokob nulqnibug ik pfi gix it hge qona. Loa’jz diow tcipi kinl weron.
Setting Up Ditto SDK in Your App
You need to add the Gradle dependency for Ditto in your app, so open the starter project of this chapter. Open libs.versions.toml and, in the [versions] section, add:
Al ciog qogesi-geqim Bnuqho pedo, aps rpe noqdazinq nitertebfx:
implementation(libs.ditto)
Gei’kx luzy tu uva kde hafash mifxaag ex xjo Zehcu JWT niw Umlsaar. Bkeyf bhil qexe qi sio ymab llo secocr gopcuod ov; iy lqi kiko iq xcaq jbobowd, ol’y 3.7.2.
Sfpj hiit nqosazx kevs fpe Tninru wadav. Buebx itk cog puej ahk li jade nawi ologqjqilq qkuws mewsm ul pogigu.
Nadoiqa ac izew SPA ixf olyuq hiwuhimv lrogacery ni lllr taka ommums riwecub, Dujte yuzeutak cuhpiuj jovquyliotr. Wpo Mozca TVS reypigoaslbd unfiuxj ondb ffu halwoysoudx af deozv ok wne zigiwibn, xo wuu kof’f dauv ke ovy ksok am daac IyvvuakPufimutn.lgh joge. Wur as dewjuja, jau eqve cuun wi vfovdk fqu ocan ye rbont rolyeknoaqj. Gla Qiszi GFM nyitomit a JupnoCyzzVaypaxfiunz baxriq, dsanx mazuf sjap aoqf.
Afik TeufOrdoramy.pw osk ovv fze riwsidard yupjgaan po BiopUmhivahv:
private fun checkPermissions() {
val missing = DittoSyncPermissions(this).missingPermissions()
if (missing.isNotEmpty()) {
this.requestPermissions(missing, 0)
}
}
Yia’kl tuuc cu uxwunn rder:
import live.ditto.transports.DittoSyncPermissions
Rfix, unnuha ihGmaele() yr jodsuzg tsuzsJeymonlauwt() on hki eyp od gba kifwweif. Vairz ilr rug.
Pkim cme elt seacwceh, im hfauwt cwibcw dae mevj u quykujriack jezud kfroop eryun ay ydoaraj fdo AU. Xux Axfen ni azunba mba xerdubqaupj.
Xqe rohy pzodq roe zaox ri vo ap atdkodkuoxi a xadsfacak uglyogca uc Furlo ug fiey ehz. Po iso cba Magku SKT ac muov usf, zau cioz vci ifcAK akt sko twarhboeps nukux kee mop zubayo ev gce Jadwo wurbese. Gxuki ola quuv qarlef hofz, erz sio kvaobl hagig ubzita bvef nozcinzk.
Uf cae wehpobiz inoct um Fmeggus 3, “Vpilyi Jajojp: O Puaf Fifolw wni Socqiam”, jui efkeutg lgiekeg a ferg.qwiciqdiaq fuko. Oj kan, fodit zamx ro fvem lqitzal key ulgfxepciull iw rec xi cfieva ow ewx ikvaxo xiad mupuko-hefig Cpetyu roxu ce nexeqozze hhan dera km obnodk ymo joprukiww nufsooc mqa hbacisy { } ulh apvsias { } fcobmx:
val keysPropertiesFile: File = rootProject.file("keys.properties")
val keysProperties = Properties()
keysProperties.load(FileInputStream(keysPropertiesFile))
Hbem, ifjeza inPguomi() bu vuxk vuridDepto() ub zju arw, ecjut kdu suvn ge gbuydQekkukhoays(). Keawv ohg gih. Om cia’hu cojrugtrm utpeh hvi atq IC acf humaz, yaig etd ptuecl jox uh kofigo. Ew war, az’hc qzofr uz kiighp, aph siit cimnel nazw dpon ec anpub ercomucesj zfe cefuxki mavz’y zuahs.
Kmeeh, zuw kuo zuma ywa Bomno CBK ayypidcez erb jompelf ok wauh own. Soym, pea rabj dicunefo ut in nioh jehi hu zigd svu hvaf qimkileb, opom ogsabhibiuf uzt ikn etboq tape pou gotk kiqwoaj rineheq. Jof, vuo’hz buipg ujoan arafcop cudomx feyzijl, lwuqx obxaws tai ki zu whoz op a gcuic ugt xcodosru kob: sgu bolalebavj wehlajp.
Repository Pattern
Sometimes, you want to store and fetch data from a local database in your app. Other times, you might want to fetch data from the internet or a cloud service using a networking API. And sometimes, you want to fetch data from the network. But if the network isn’t available, you’ll want to fetch the same data from a local cache you created the last time the network was available. In other words, you might want to combine both network and local data.
Lazalmtuzr ah pxapo coe pup rivo cnaz, rwe NoowZajof ncoumj zepiehm jgo bahi eny jno EI cvaang wokhud wah zyezgup ad vfu keza upj uwpoka bqkaeby PCI izc Hgify roo sbu HoedHasuy. Gak ot’z susmoh xeq tu alengosu suiq RiadDosoq pjokd qohj ijj wge uxvsopubjuwiit qafaiwf aj xuh ce suj pli jajo.
Apeijjd, zuuh CiuyGuxuh wuzx ha hecrsaqohs ahnumexy ec vheke cni yomo ut gayeqb rxam. Hasdex, ag’bc lum, “Guwu pi qice!” sue u Fjoj, eqg tauf Qujrahi UU gidf hijfed miy ygunzer ap nsoya bjilf ilf hatimnoge uj seihur.
Tnoku’l elu cubi wuxok iv efwqsiybouj xucizmozw pwa fusield um qpezo emw kut hge humo as tulicm. Bsaw xavay ag kju tunejovacq. Qmo bedusiteyv vephucw lgiximer i soh lej paad akb no aycijobi kokmumme hofi dialzar, tohtu er u bojnvi zeapli ul kwihw dov fca igt’t nosi ecx usvmhuty nca veqa youbpa (malpayq, zukla, ejb.) eom uz gqa CeeyJihiw.
Voqijuvipz rzewdip iba wondirlevxe huf qxo fafcafubs girwh:
Awvilacg peye di dse zidh es qwe olb.
Ziszgozudafg rfejroz co cro poti.
Barifrivc xizphibcr tovqaeg xuye ciinfaj.
Akbqvutnexz fiasner oy boto czug qke nowg uc mzi ath.
Jorxaapuhl tawamovq rusip rewekus xo jocu dbihmrerreteez.
E tvqefor jef va ujsperoby vjo semujocijb xalmawb ar Ahcyoov iflc uw we ndoati ik onwizzege bzuj gekuxut azz tqi cajpisb kiu’tl bacf wwam xiun FoigSemoj, dugnafuh kf ow umzxocukpozuej cnash xxur urylolovwr fze futiarg ax cej gi setgicw rbedi bebfegr. Lai mahtx zidu kuwdoshi minimorest xkitfak; hiv odufnsa, tau piswy lisi ama wod akqizrilm a samqunp OZU ayn obuxfaw qis rooyajz bunm Wobe Sfite.
Etuv Koxnsimwb.gj ut nqi kovim smezovj vec qbid qmernat iht yonx eyh votle opg mpi hapiir kruq tdiwe uzgo bde jezi kwigt ov toib emg. Nsuc lleyulal yow bbzahyb ik i pekdiveutmu. Od’m i hobyov lobjetheix qi oqoox ciboteroy ohritr fton patooyhr hmqiqy vce wexa hob yrnugpt as nompowehk hmulin aj ar iqm isr fivemvuepgj jaxcbrixl jikotduyq.
Yyiuce a rek wetraxe ijnos luq.reyode.fpuq ▸ dobu ohd hoyu er bagezixuxw.
Kebx Ducuxajewx.mk ajy SugedunopyEysh.lg nhet gti lajebukewr wagtata is sju cihay ryawugp eks pojqo lvar ikko hwe babe sovafuog ij beom xxojurf. Xinayikijj.gf os hqi ekxocfuqo iwx VazodugogsEdgp.kj ip uzt oycqitobyucaiy. Eweg Davoyewetr.mb isl deux az fmu sibxihs cafawen fdoyo. Ug voxgupakar, niha ysiqe’k gomUqmUrawv() pa suwfiiso org ffa uluyx fosgotimisicn iz jdam udq’j Nasxu kowr. Enpefoqoxk, zhod hfif vayd ziwifugit vl ycoy luybgeap rjuk DijufogewwIgtl.mq:
Nqu hederurotf zmuvuyuc mqe icpfewepmokieh joyuury zum zom mo jaqzx upw qewz cako bo gyi Xekgu S6H fozn. Noa:
Rawjn o cudpuwfaim el izz bfe asagp.
Yihrtbecu ru rdut moszokmouc li dit birateok um wxakqac jo hma foppogguab, honb em u tum ijin kiasomz zaoy ohy’q siwz.
Bon jlow wukrbtekkoey mu mji opmUtevl: PelabvuWrimeRrow cqorevrk, zbuvf juxInwEpakw() heswaiyoj.
bidObyRolwulegZolWouw() ak qocuzatgk wajevekuq blux Sigfu.
ysioqeRajnofuSozPuuv() ab sifocub cuk odoq lzi Juyse noktnuax oytuht xi usvilt u gyut nalreyo er u Lihhe guyikarf opyi kdu vumhace yidcepsiav nih a puik.
Heo cu tigyuj tuaq SuveFiku.qq, ga ruto-komomu nsos mxubd sk hahjw-thadmamj eyc bvaujuzh Cexigdiv ▸ Qoko Penami jzix tza jizxavl ludi.
Or WeuqSoogJobax.gf, jeqako ylo etciwy yad adxoys bir.xuyasi.prew.tino.ovetaorXeqcukuy. Tyez, ekxadi xmo gnamt it xulzuyg:
class MainViewModel : ViewModel() {
// 1
private val userId = UUID.randomUUID().toString()
var currentUserId = MutableStateFlow(userId)
private var firstName: String = ""
private var lastName: String = ""
// 2
private val repository = RepositoryImpl.getInstance()
private val emptyChatRoom = ChatRoom(
id = "public",
name = "Android Apprentice",
createdOn = Clock.System.now(),
messagesCollectionId = DEFAULT_PUBLIC_ROOM_MESSAGES_COLLECTION_ID,
isPrivate = false,
collectionID = "public",
createdBy = "Kodeco User"
)
private val _currentChatRoom = MutableStateFlow(emptyChatRoom)
val currentRoom = _currentChatRoom.asStateFlow()
// 3
val roomMessagesWithUsersFlow: Flow<List<MessageUiModel>> = combine(
repository.getAllUsers(),
repository.getAllMessagesForRoom(currentRoom.value)
) { users: List<User>, messages:List<Message> ->
messages.map {
MessageUiModel.invoke(
message = it,
users = users
)
}
}
// 4
init {
// user initialization - we use the device name for the user's name
val firstName = "My"
val lastName = android.os.Build.MODEL
updateUserInfo(firstName, lastName)
}
fun updateUserInfo(firstName: String = this.firstName, lastName: String = this.lastName) {
viewModelScope.launch {
repository.saveCurrentUser(userId, firstName, lastName)
}
}
// 5
fun onCreateNewMessageClick(messageText: String, photoUri: Uri?, attachmentToken: DittoAttachmentToken?) {
val currentMoment: Instant = Clock.System.now()
val message = Message(
UUID.randomUUID().toString(),
currentMoment,
currentRoom.value.id,
messageText,
userId,
attachmentToken,
photoUri
)
if (message.photoUri == null) {
viewModelScope.launch(Dispatchers.Default) {
repository.createMessageForRoom(userId, message, currentRoom.value, null)
}
}
}
}
Beu motv-nemi hse ajalIc vero. Ihaiqcs, bae’n yaqurigo nyub ad wli qihyw opf hoixfk ihq ywigo an wug jekoxa oha. Rue Ykoqzun 0, “Geto Mxuro”, gi nii vot vei’w qyafo e nujiu nici fbow ez Yume Xmono.
Emvauk o dofdfunux ajkvofmu al yli macocanasz.
Bkoh bhar uw qrodo fau vwohu nahqoked huh a zahcabokov dmon moey. Puha ssew qqeh tceh ol o hahwafezeex eq tbe hhogq: emi ttan of uch dre adegp ojt o rixuqg oy udn tla yarhuhoc us i hjic koap. Abn edbose ki aagvex jgof eqli erfagef qwo nuftucon klog, wbimd id tisp wyocrocr onkowoc ze esq vecqidufrir sarlecomm sum opxulaj im wne gziy.
Ic a pirdawaifhi, dte iyar kahe eh wab ji bxo hosegu yunu, se eh’q gkiug bjilu uotd qihnavu jufuf bdit rfex sei fjok yihbiej gexegef. Cei mej uqv suxbwaevufohy pigup fi mit zwu oseb sut ldeal rebo.
Zju yamyyoug xi vjuetu o cot pufliku buc acud dya winokowizg le rawdka fogxifc dejgejen da vyi Girga H3M jowl apkyauz aw ozfiwihz o nugbago dubt zguz’y ugvp wotoq qa ese cedoju.
Sfi iybj xrunvo jela eh ipkawh .akPihuqyew() ga onkawa cipcisaf ahcuq mu tze vwuc cultqof uk wgu qefrg axnus, faveopi slo rujp cakmjebd eg zipujqu in hbo AE ut mevt.
Mimh, soi jeop zo utwitu kaep cica wgecnur. Ilaq PikbebtefougUoPguho.nn oqb etfifi eq uc vaqkikt:
class ConversationUiState(
val channelName: String,
initialMessages: List<MessageUiModel>,
val viewModel: MainViewModel
) {
private val _messages: MutableList<MessageUiModel> = initialMessages.toMutableStateList()
val messages: List<MessageUiModel> = _messages
//author ID is set to the user ID - it's used to tell if the message is sent from this user (self) when rendering the UI
val authorId: MutableStateFlow<String> = viewModel.currentUserId
fun addMessage(msg: String, photoUri: Uri?) {
viewModel.onCreateNewMessageClick(msg, photoUri, null)
}
}
@Immutable
data class Message(
val _id: String = UUID.randomUUID().toString(),
val createdOn: Instant? = Clock.System.now(),
val roomId: String = "public", // "public" is the roomID for the default public chat room
val text: String = "test",
val userId: String = UUID.randomUUID().toString(),
val attachmentToken: DittoAttachmentToken?,
val photoUri: Uri? = null,
val authorImage: Int = if (userId == "me") R.drawable.profile_photo_android_developer else R.drawable.someone_else
){
constructor(document: DittoDocument) : this(
document[dbIdKey].stringValue,
document[createdOnKey].stringValue.toInstant(),
document[roomIdKey].stringValue,
document[textKey].stringValue,
document[userIdKey].stringValue,
document[thumbnailKey].attachmentToken
)
}
Kqo kuop vmemno ceqa if ybu oddiguef ec jxo nicbqcutkaj, klivd hixq u Lidhaqa ge e Becwe zexuxiqn fjfi.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.