-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #300 from wealthfront/takehome-random
Compose support for take-home challenges
- Loading branch information
Showing
16 changed files
with
248 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
34 changes: 34 additions & 0 deletions
34
...-migration/src/compose/java/com/wealthfront/magellan/sample/migration/tide/ComposeStep.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.wealthfront.magellan.sample.migration.tide | ||
|
||
import androidx.annotation.VisibleForTesting | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.saveable.SaveableStateHolder | ||
import androidx.compose.runtime.saveable.rememberSaveableStateHolder | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.ComposeView | ||
import com.wealthfront.magellan.core.Navigable | ||
import com.wealthfront.magellan.lifecycle.LifecycleAwareComponent | ||
import com.wealthfront.magellan.lifecycle.createAndAttachFieldToLifecycleWhenShown | ||
import java.util.UUID | ||
|
||
abstract class ComposeStep : Navigable, LifecycleAwareComponent() { | ||
|
||
private var state: SaveableStateHolder? = null | ||
|
||
final override var view: ComposeView? by createAndAttachFieldToLifecycleWhenShown { ComposeView(it) } | ||
@VisibleForTesting set | ||
|
||
fun setContent(content: @Composable () -> Unit) { | ||
view?.setContent { | ||
if (state == null) { | ||
state = rememberSaveableStateHolder() | ||
} | ||
Box(modifier = Modifier) { | ||
state!!.SaveableStateProvider(UUID.randomUUID()) { | ||
content() | ||
} | ||
} | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...ation/src/compose/java/com/wealthfront/magellan/sample/migration/tide/DogBreedListItem.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.wealthfront.magellan.sample.migration.tide | ||
|
||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.Typography | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.text.style.TextAlign | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
|
||
@Composable | ||
fun DogBreedListItem(breedName: String, goToDogDetails: (name: String) -> Unit) { | ||
Text( | ||
text = breedName, | ||
textAlign = TextAlign.Center, | ||
style = Typography().bodyMedium.copy( | ||
fontWeight = FontWeight.Bold, | ||
fontSize = 20.sp, | ||
color = MaterialTheme.colorScheme.primary | ||
), | ||
modifier = Modifier | ||
.clickable { | ||
goToDogDetails(breedName) | ||
} | ||
.fillMaxWidth() | ||
.background(MaterialTheme.colorScheme.surface) | ||
.padding( | ||
horizontal = 16.dp, | ||
vertical = 16.dp | ||
) | ||
) | ||
} |
20 changes: 20 additions & 0 deletions
20
...le-migration/src/compose/java/com/wealthfront/magellan/sample/migration/tide/DogBreeds.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.wealthfront.magellan.sample.migration.tide | ||
|
||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.foundation.lazy.itemsIndexed | ||
import androidx.compose.material3.HorizontalDivider | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.testTag | ||
|
||
@Composable | ||
fun DogBreeds(dogBreeds: List<String>, onBreedClick: (name: String) -> Unit) { | ||
LazyColumn(modifier = Modifier.testTag("DogBreeds")) { | ||
itemsIndexed(dogBreeds) { index, item -> | ||
DogBreedListItem(item, onBreedClick) | ||
if (index != (dogBreeds.size - 1)) { | ||
HorizontalDivider() | ||
} | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...-migration/src/compose/java/com/wealthfront/magellan/sample/migration/tide/DogListStep.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.wealthfront.magellan.sample.migration.tide | ||
|
||
import android.content.Context | ||
import android.widget.Toast | ||
import android.widget.Toast.LENGTH_SHORT | ||
import androidx.compose.runtime.mutableStateListOf | ||
import com.wealthfront.magellan.coroutines.ShownLifecycleScope | ||
import com.wealthfront.magellan.lifecycle.attachFieldToLifecycle | ||
import com.wealthfront.magellan.sample.migration.api.DogApi | ||
import dagger.assisted.Assisted | ||
import dagger.assisted.AssistedFactory | ||
import dagger.assisted.AssistedInject | ||
import kotlinx.coroutines.launch | ||
|
||
@AssistedFactory | ||
fun interface DogListStepFactory { | ||
fun create(goToDogDetails: (name: String) -> Unit): DogListStep | ||
} | ||
|
||
class DogListStep @AssistedInject constructor( | ||
private val api: DogApi, | ||
@Assisted private val goToDogDetails: (name: String) -> Unit | ||
) : ComposeStep() { | ||
|
||
private val scope by attachFieldToLifecycle(ShownLifecycleScope()) | ||
private val dogBreedsData = mutableStateListOf<String>() | ||
|
||
override fun onShow(context: Context) { | ||
setContent { | ||
DogBreeds(dogBreeds = dogBreedsData, onBreedClick = goToDogDetails) | ||
} | ||
|
||
scope.launch { | ||
// show loading | ||
val breeds = runCatching { api.getAllBreeds() } | ||
breeds.onSuccess { response -> | ||
dogBreedsData.clear() | ||
dogBreedsData.addAll(response.message.keys) | ||
}.onFailure { | ||
Toast.makeText(context, it.message, LENGTH_SHORT).show() | ||
} | ||
// hide loading | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
2 changes: 2 additions & 0 deletions
2
magellan-sample-migration/src/testAndroidViews/resources/robolectric.properties
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
sdk=28 | ||
application=com.wealthfront.magellan.sample.migration.TestSampleApplication |
30 changes: 30 additions & 0 deletions
30
...tion/src/testCompose/java/com/wealthfront/magellan/sample/migration/tide/DogBreedsTest.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.wealthfront.magellan.sample.migration.tide | ||
|
||
import androidx.compose.ui.test.junit4.createComposeRule | ||
import androidx.compose.ui.test.onChildAt | ||
import androidx.compose.ui.test.onNodeWithTag | ||
import androidx.compose.ui.test.performClick | ||
import com.google.common.truth.Truth.assertThat | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class DogBreedsTest { | ||
|
||
@get:Rule | ||
val composeTestRule = createComposeRule() | ||
|
||
@Test | ||
fun goesToSelectedDogBreed() { | ||
var clicked = false | ||
composeTestRule.setContent { | ||
DogBreeds(dogBreeds = listOf("akita"), onBreedClick = { clicked = true }) | ||
} | ||
|
||
composeTestRule.onNodeWithTag("DogBreeds").onChildAt(0).performClick() | ||
|
||
assertThat(clicked).isTrue() | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...on/src/testCompose/java/com/wealthfront/magellan/sample/migration/tide/DogListStepTest.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.wealthfront.magellan.sample.migration.tide | ||
|
||
import android.app.Application | ||
import android.os.Looper.getMainLooper | ||
import androidx.activity.ComponentActivity | ||
import androidx.compose.ui.test.junit4.createComposeRule | ||
import androidx.compose.ui.test.onChildAt | ||
import androidx.compose.ui.test.onNodeWithTag | ||
import androidx.compose.ui.test.performClick | ||
import androidx.test.core.app.ApplicationProvider | ||
import com.google.common.truth.Truth.assertThat | ||
import com.wealthfront.magellan.lifecycle.setContentScreen | ||
import com.wealthfront.magellan.sample.migration.AppComponentContainer | ||
import com.wealthfront.magellan.sample.migration.TestAppComponent | ||
import com.wealthfront.magellan.sample.migration.api.DogBreedsResponse | ||
import io.mockk.coEvery | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.Robolectric | ||
import org.robolectric.RobolectricTestRunner | ||
import org.robolectric.Shadows.shadowOf | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class DogListStepTest { | ||
|
||
@get:Rule | ||
val composeTestRule = createComposeRule() | ||
|
||
private lateinit var dogListStep: DogListStep | ||
private val activityController = Robolectric.buildActivity(ComponentActivity::class.java) | ||
|
||
private var chosenBreed: String? = null | ||
|
||
@Before | ||
fun setUp() { | ||
val context = ApplicationProvider.getApplicationContext<Application>() | ||
val component = ((context as AppComponentContainer).injector() as TestAppComponent) | ||
dogListStep = component.dogListStepFactory.create { chosenBreed = it } | ||
coEvery { component.api.getAllBreeds() } returns | ||
DogBreedsResponse(message = mapOf("akita" to emptyList()), status = "success") | ||
} | ||
|
||
@Test | ||
fun goesToSelectedDogBreed() { | ||
activityController.get().setContentScreen(dogListStep) | ||
activityController.setup() | ||
shadowOf(getMainLooper()).idle() | ||
|
||
composeTestRule.onNodeWithTag("DogBreeds").onChildAt(0).performClick() | ||
assertThat(chosenBreed).isEqualTo("akita") | ||
} | ||
} |