Skip to content

update #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,27 @@ android {
jvmTarget = "1.8"
}

viewBinding {
enabled = true
buildFeatures {
viewBinding = true
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

implementation 'androidx.appcompat:appcompat:1.2.0-beta01'
implementation 'androidx.core:core-ktx:1.3.0-rc01'
implementation 'androidx.appcompat:appcompat:1.3.0-alpha01'
implementation 'androidx.core:core-ktx:1.4.0-alpha01'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.recyclerview:recyclerview:1.2.0-alpha03'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0-rc01'
implementation 'com.google.android.material:material:1.2.0-alpha06'
implementation 'com.google.android.material:material:1.3.0-alpha01'

// viewModelScope
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0-alpha02'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0-alpha03'

// lifecycleScope
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.0-alpha02'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.0-alpha03'

// retrofit2
implementation 'com.squareup.retrofit2:retrofit:2.8.1'
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
</intent-filter>
</activity>

<activity android:name=".ui.add.AddActivity" />
<activity
android:name=".ui.add.AddActivity"
android:label="Add user" />

<meta-data
android:name="preloaded_fonts"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/hoc/flowmvi/data/UserRepositoryImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.withContext

Expand Down Expand Up @@ -73,6 +74,7 @@ class UserRepositoryImpl(
val body = domainToBody(user).copy(avatar = avatarUrls.random())
val response = userApiService.add(body)
changesChannel.send(Change.Added(responseToDomain(response)))
delay(400)
}
}

Expand Down
19 changes: 13 additions & 6 deletions app/src/main/java/com/hoc/flowmvi/ui/add/AddActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isInvisible
import androidx.lifecycle.lifecycleScope
import androidx.transition.AutoTransition
import androidx.transition.TransitionManager
import com.hoc.flowmvi.clicks
import com.hoc.flowmvi.databinding.ActivityAddBinding
import com.hoc.flowmvi.textChanges
Expand All @@ -28,7 +30,7 @@ class AddActivity : AppCompatActivity(), View {
supportActionBar?.setDisplayHomeAsUpEnabled(true)

setupViews()
bindVM()
bindVM(addVM)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
Expand All @@ -38,7 +40,7 @@ class AddActivity : AppCompatActivity(), View {
}
}

private fun bindVM() {
private fun bindVM(addVM: AddVM) {
// observe view model
lifecycleScope.launchWhenStarted {
addVM.viewState
Expand Down Expand Up @@ -101,13 +103,18 @@ class AddActivity : AppCompatActivity(), View {
addBinding.lastNameEditText.error = lastNameErrorMessage
}

TransitionManager.beginDelayedTransition(
addBinding.root,
AutoTransition()
.addTarget(addBinding.progressBar)
.addTarget(addBinding.addButton)
.setDuration(200)
)
addBinding.progressBar.isInvisible = !viewState.isLoading
addBinding.addButton.isInvisible = viewState.isLoading
}

private fun setupViews() {

}

private fun setupViews() = Unit

override fun intents(): Flow<ViewIntent> {
return merge(
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/java/com/hoc/flowmvi/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.hoc.flowmvi.ui.add.AddActivity
import com.hoc.flowmvi.ui.main.MainContract.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.*
import org.koin.androidx.viewmodel.ext.android.viewModel
Expand All @@ -31,14 +32,14 @@ class MainActivity : AppCompatActivity(), View {
private val userAdapter = UserAdapter()
private val mainBinding by lazy(NONE) { ActivityMainBinding.inflate(layoutInflater) }

private val removeChannel = Channel<UserItem>(Channel.UNLIMITED)
private val removeChannel = BroadcastChannel<UserItem>(Channel.BUFFERED)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(mainBinding.root)

setupViews()
bindVM()
bindVM(mainVM)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
Expand Down Expand Up @@ -72,7 +73,7 @@ class MainActivity : AppCompatActivity(), View {
}
}

private fun bindVM() {
private fun bindVM(mainVM: MainVM) {
// observe view model
lifecycleScope.launchWhenStarted {
mainVM.viewState
Expand All @@ -97,7 +98,7 @@ class MainActivity : AppCompatActivity(), View {
flowOf(ViewIntent.Initial),
mainBinding.swipeRefreshLayout.refreshes().map { ViewIntent.Refresh },
mainBinding.retryButton.clicks().map { ViewIntent.Retry },
removeChannel.consumeAsFlow().map { ViewIntent.RemoveUser(it) }
removeChannel.asFlow().map { ViewIntent.RemoveUser(it) }
)

private fun handleSingleEvent(event: SingleEvent) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/hoc/flowmvi/ui/main/MainVM.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MainVM(
private val removeUser: RemoveUserUseCase,
) : ViewModel() {
private val _eventChannel = BroadcastChannel<SingleEvent>(capacity = Channel.BUFFERED)
private val _intentChannel = BroadcastChannel<ViewIntent>(capacity = Channel.CONFLATED)
private val _intentChannel = BroadcastChannel<ViewIntent>(capacity = Channel.BUFFERED)

val viewState: StateFlow<ViewState>

Expand Down
19 changes: 9 additions & 10 deletions app/src/main/res/layout/activity_add.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="16dp"
android:hint="First name"
app:errorEnabled="true"
Expand Down Expand Up @@ -70,13 +70,13 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="16dp"
android:hint="Last name"
app:errorEnabled="true"
app:hintAnimationEnabled="true"
app:hintEnabled="true"
app:layout_constraintBottom_toTopOf="@+id/progressBar"
app:layout_constraintBottom_toTopOf="@+id/addButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
Expand All @@ -90,6 +90,7 @@
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:fontFamily="@font/noto_sans"
android:imeOptions="actionDone"
android:inputType="textPersonName"
android:singleLine="true"
android:textSize="16sp" />
Expand All @@ -104,9 +105,9 @@
android:elevation="12dp"
android:fontFamily="@font/noto_sans"
android:paddingStart="32dp"
android:paddingTop="16dp"
android:paddingTop="12dp"
android:paddingEnd="32dp"
android:paddingBottom="16dp"
android:paddingBottom="12dp"
android:text="Add"
android:textColor="@android:color/white"
android:textSize="16sp"
Expand All @@ -115,17 +116,15 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressBar" />
app:layout_constraintTop_toBottomOf="@+id/lastNameEditText" />

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="@+id/addButton"
app:layout_constraintBottom_toBottomOf="@+id/addButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/lastNameEditText" />
app:layout_constraintTop_toTopOf="@id/addButton" />
</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {
maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.3'
classpath 'com.android.tools.build:gradle:4.0.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sat Feb 08 09:17:34 ICT 2020
#Sat Jun 13 00:43:17 ICT 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1-milestone-2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip