Skip to content
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

ViewModel refactoring #220

Merged
merged 4 commits into from
Feb 5, 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
31 changes: 20 additions & 11 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
buildscript {
ext {
kotlinVersion = '1.3.61'
androidGradleVersion = '3.5.3'
androidMavenGradleVersion = '2.1'

// Google libraries
appCompatVersion = '1.1.0'
constraintLayoutVersion = '1.1.3'
materialComponentsVersion = '1.1.0-rc02'
roomVersion = '2.2.3'
lifecycleVersion = '2.2.0'

// Publishing
androidMavenGradleVersion = '2.1'

// Networking
gsonVersion = '2.8.6'
okhttp3Version = '3.12.6'
retrofitVersion = '2.6.4'

// Debug and quality control
detektVersion = '1.4.0'
dokkaVersion = '0.10.0'
gradleBintrayVersion = '1.8.4'
gsonVersion = '2.8.6'
junitGradlePluignVersion = '1.3.1.1'
junitVersion = '5.4.2'
kotlinVersion = '1.3.61'
ktLintVersion = '9.1.1'
leakcanaryVersion = '2.1'
materialComponentsVersion = '1.1.0-rc02'

// Testing
junitGradlePluignVersion = '1.3.1.1'
junitVersion = '5.4.2'
mockkVersion = '1.9.3'
okhttp3Version = '3.12.6'
retrofitVersion = '2.6.4'
roomVersion = '2.2.3'
viewModelVersion = '2.1.0'
}

repositories {
Expand Down
14 changes: 8 additions & 6 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,21 @@ artifacts {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"

implementation "com.google.code.gson:gson:$gsonVersion"
implementation "com.squareup.okhttp3:okhttp:$okhttp3Version"
implementation "com.google.android.material:material:$materialComponentsVersion"
implementation "androidx.constraintlayout:constraintlayout:$constraintLayoutVersion"

implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"
implementation "androidx.room:room-runtime:$roomVersion"
kapt "androidx.room:room-compiler:$roomVersion"

implementation "com.google.code.gson:gson:$gsonVersion"
implementation "com.squareup.okhttp3:okhttp:$okhttp3Version"

testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
testImplementation "org.junit.jupiter:junit-jupiter-params:$junitVersion"
testImplementation "io.mockk:mockk:$mockkVersion"

implementation "androidx.lifecycle:lifecycle-extensions:$viewModelVersion"
implementation "androidx.room:room-runtime:$roomVersion"
kapt "androidx.room:room-compiler:$roomVersion"
}

apply from: rootProject.file('gradle/gradle-mvn-push.gradle')
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.chuckerteam.chucker.internal.ui
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.ViewModelProvider
import androidx.viewpager.widget.ViewPager
import com.chuckerteam.chucker.R
import com.chuckerteam.chucker.api.Chucker
Expand All @@ -16,6 +17,8 @@ internal class MainActivity :
BaseChuckerActivity(),
TransactionAdapter.TransactionClickListListener,
ErrorAdapter.ErrorClickListListener {

private lateinit var viewModel: MainViewModel
private lateinit var viewPager: ViewPager

private val applicationName: CharSequence
Expand All @@ -29,6 +32,8 @@ internal class MainActivity :
setSupportActionBar(toolbar)
toolbar.subtitle = applicationName

viewModel = ViewModelProvider(this).get(MainViewModel::class.java)

viewPager = findViewById(R.id.viewPager)
viewPager.adapter = HomePageAdapter(this, supportFragmentManager)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.chuckerteam.chucker.internal.ui

import android.text.TextUtils
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import com.chuckerteam.chucker.internal.data.entity.HttpTransactionTuple
import com.chuckerteam.chucker.internal.data.entity.RecordedThrowableTuple
import com.chuckerteam.chucker.internal.data.repository.RepositoryProvider
import com.chuckerteam.chucker.internal.support.NotificationHelper

internal class MainViewModel : ViewModel() {

private val currentFilter = MutableLiveData<String>("")

val transactions: LiveData<List<HttpTransactionTuple>> = Transformations.switchMap(currentFilter) { searchQuery ->
with(RepositoryProvider.transaction()) {
when {
searchQuery.isNullOrBlank() -> {
getSortedTransactionTuples()
}
TextUtils.isDigitsOnly(searchQuery) -> {
getFilteredTransactionTuples(searchQuery, "")
}
else -> {
getFilteredTransactionTuples("", searchQuery)
}
}
}
}

val errors: LiveData<List<RecordedThrowableTuple>> =
Transformations.map(
RepositoryProvider.throwable().getSortedThrowablesTuples()
) {
it
}

fun updateItemsFilter(searchQuery: String) {
currentFilter.value = searchQuery
}

fun clearTransactions() {
RepositoryProvider.transaction().deleteAllTransactions()
NotificationHelper.clearBuffer()
}

fun clearErrors() {
RepositoryProvider.throwable().deleteAllThrowables()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,24 @@ import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.DividerItemDecoration.VERTICAL
import androidx.recyclerview.widget.RecyclerView
import com.chuckerteam.chucker.R
import com.chuckerteam.chucker.internal.data.repository.RepositoryProvider
import com.chuckerteam.chucker.internal.ui.MainViewModel

internal class ErrorListFragment : Fragment() {

private lateinit var viewModel: MainViewModel
private lateinit var adapter: ErrorAdapter
private lateinit var listener: ErrorAdapter.ErrorClickListListener
private lateinit var tutorialView: View

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
viewModel = ViewModelProvider(requireActivity())[MainViewModel::class.java]
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
Expand All @@ -42,27 +45,28 @@ internal class ErrorListFragment : Fragment() {
}
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.errors.observe(
viewLifecycleOwner,
Observer { errors ->
adapter.setData(errors)
tutorialView.visibility = if (errors.isNullOrEmpty()) {
View.VISIBLE
} else {
View.GONE
}
}
)
}

override fun onAttach(context: Context) {
super.onAttach(context)

require(context is ErrorAdapter.ErrorClickListListener) {
"Context must implement the listener."
}
listener = context

RepositoryProvider.throwable()
.getSortedThrowablesTuples()
.observe(
this,
Observer { tuples ->
adapter.setData(tuples)
if (tuples.isNullOrEmpty()) {
tutorialView.visibility = View.VISIBLE
} else {
tutorialView.visibility = View.GONE
}
}
)
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
Expand All @@ -80,16 +84,14 @@ internal class ErrorListFragment : Fragment() {
}

private fun askForConfirmation() {
context?.let {
AlertDialog.Builder(it)
.setTitle(R.string.clear)
.setMessage(R.string.clear_error_confirmation)
.setPositiveButton(R.string.clear) { _, _ ->
RepositoryProvider.throwable().deleteAllThrowables()
}
.setNegativeButton(R.string.cancel, null)
.show()
}
AlertDialog.Builder(requireContext())
.setTitle(R.string.clear)
.setMessage(R.string.clear_error_confirmation)
.setPositiveButton(R.string.clear) { _, _ ->
viewModel.clearErrors()
}
.setNegativeButton(R.string.cancel, null)
.show()
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/*
* Copyright (C) 2017 Jeff Gilfelt.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.chuckerteam.chucker.internal.ui.transaction

import android.content.Context
Expand All @@ -24,7 +9,7 @@ import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.core.app.ShareCompat
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.lifecycle.ViewModelProvider
import androidx.viewpager.widget.ViewPager
import com.chuckerteam.chucker.R
import com.chuckerteam.chucker.internal.support.FormatUtils.getShareCurlCommand
Expand All @@ -45,10 +30,8 @@ internal class TransactionActivity : BaseChuckerActivity() {

// Create the instance now, so it can be shared by the
// various fragments in the view pager later.
viewModel = ViewModelProviders
.of(this, TransactionViewModelFactory(transactionId))
viewModel = ViewModelProvider(this, TransactionViewModelFactory(transactionId))
.get(TransactionViewModel::class.java)
viewModel.loadTransaction()

val toolbar = findViewById<Toolbar>(R.id.toolbar)
setSupportActionBar(toolbar)
Expand Down
Loading