Skip to content

Commit

Permalink
cleanup with no explanation (MobileNativeFoundation#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalbuddha authored Nov 3, 2019
1 parent ba22d0c commit ca8c957
Show file tree
Hide file tree
Showing 124 changed files with 243 additions and 4,219 deletions.
2 changes: 0 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ dependencies {
kapt(libraries.roomCompiler)
implementation project(':store')
implementation project(':cache')
implementation project(':middleware')
implementation project(':middleware-moshi')
implementation project(':filesystem')
implementation project(':suspendCache')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$versions.kotlin"
Expand Down
78 changes: 78 additions & 0 deletions app/src/main/java/com/nytimes/android/sample/Graph.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.nytimes.android.sample

import android.text.Html
import androidx.room.Room
import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory
import com.nytimes.android.external.fs3.SourcePersisterFactory
import com.nytimes.android.external.store3.base.Persister
import com.nytimes.android.external.store4.FlowStore
import com.nytimes.android.external.store4.RealFlowStore
import com.nytimes.android.external.store4.legacy.BarCode
import com.nytimes.android.sample.data.model.Children
import com.nytimes.android.sample.data.model.Post
import com.nytimes.android.sample.data.model.RedditDb
import com.nytimes.android.sample.data.remote.Api
import com.squareup.moshi.Moshi
import okio.BufferedSource
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import java.io.File
import java.io.IOException

object Graph {
fun provideRoomPipeline(context: SampleApp): FlowStore<String, List<Post>> {
val db = provideRoom(context)
return RealFlowStore
.fromNonFlow<String, List<Post>, List<Post>> {
provideRetrofit().fetchSubreddit(it, "10")
.await().data.children.map(::toPosts)
}
.persister(reader = db.postDao()::loadPosts,
writer = db.postDao()::insertPosts,
delete = db.postDao()::clearFeed)
.build()
}

private fun provideRoom(context: SampleApp): RedditDb {
return Room.inMemoryDatabaseBuilder(context, RedditDb::class.java)
.build()
}


/**
* Returns a new Persister with the cache as the root.
*/
@Throws(IOException::class)
fun newPersister(cacheDir: File): Persister<BufferedSource, BarCode> {
return SourcePersisterFactory.create(cacheDir)
}

private fun provideRetrofit(): Api {
return Retrofit.Builder()
.baseUrl("https://reddit.com/")
.addConverterFactory(MoshiConverterFactory.create(provideMoshi()))
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.build()
.create(Api::class.java)
}

private fun toPosts(it: Children): Post {
return it.data.copy(
preview = it.data.preview?.let {
it.copy(
images = it.images.map {
@Suppress("DEPRECATION")
it.copy(
source = it.source.copy(
// preview urls are html encoded, need to escape
url = Html.fromHtml(it.source.url).toString()
)
)
}
)
}
)
}

fun provideMoshi() = Moshi.Builder().build()
}

This file was deleted.

18 changes: 7 additions & 11 deletions app/src/main/java/com/nytimes/android/sample/RoomActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,18 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.lifecycleScope
import com.google.android.material.snackbar.Snackbar
import com.nytimes.android.external.store3.pipeline.PipelineStore
import com.nytimes.android.external.store3.pipeline.ResponseOrigin
import com.nytimes.android.external.store3.pipeline.StoreRequest
import com.nytimes.android.external.store3.pipeline.StoreResponse
import com.nytimes.android.external.store4.FlowStore
import com.nytimes.android.external.store4.ResponseOrigin
import com.nytimes.android.external.store4.StoreRequest
import com.nytimes.android.external.store4.StoreResponse
import com.nytimes.android.sample.reddit.PostAdapter
import kotlinx.android.synthetic.main.activity_room_store.*
import kotlinx.android.synthetic.main.activity_store.postRecyclerView
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.ObsoleteCoroutinesApi
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.transform
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch

@FlowPreview
Expand All @@ -45,7 +41,7 @@ class RoomActivity : AppCompatActivity() {
postRecyclerView.adapter = adapter
}
}
val storeState = StoreState((application as SampleApp).roomPipeline)
val storeState = StoreState((application as SampleApp).roomFlowStore)
lifecycleScope.launchWhenStarted {
fun refresh() {
launch {
Expand Down Expand Up @@ -91,7 +87,7 @@ class RoomActivity : AppCompatActivity() {
* This class should possibly be moved to a helper library but needs more API work before that.
*/
internal class StoreState<Key, Output>(
private val store : PipelineStore<Key, Output>
private val store : FlowStore<Key, Output>
) {
private val keyFlow = Channel<Key>(capacity = Channel.CONFLATED)
private val _errors = Channel<String>(capacity = Channel.CONFLATED)
Expand Down
Loading

0 comments on commit ca8c957

Please sign in to comment.