Skip to content

Commit

Permalink
shared view model implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
shuza committed Aug 9, 2018
1 parent e423267 commit 7b17ade
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 43 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
2 changes: 1 addition & 1 deletion .idea/misc.xml

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

20 changes: 16 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ android {
compileSdkVersion 28
defaultConfig {
applicationId "ninja.shuza.sharedviewmodel"
minSdkVersion 16
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

javaCompileOptions {
annotationProcessorOptions {
includeCompileClasspath false
}
}
}

buildTypes {
release {
minifyEnabled false
Expand All @@ -25,12 +32,17 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0-rc01'
implementation "org.jetbrains.anko:anko:$anko_version"
implementation 'com.android.support:appcompat-v7:28.0.0-beta01'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:support-v4:28.0.0-rc01'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

implementation "org.jetbrains.anko:anko:$anko_version"
implementation 'android.arch.work:work-runtime:1.0.0-alpha06'
implementation 'android.arch.lifecycle:extensions:1.0.0'
implementation 'android.arch.lifecycle:compiler:1.0.0'

annotationProcessor 'android.arch.persistence.room:runtime:1.0.0'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'
}
16 changes: 0 additions & 16 deletions app/src/main/java/ninja/shuza/sharedviewmodel/Communicator.kt

This file was deleted.

14 changes: 11 additions & 3 deletions app/src/main/java/ninja/shuza/sharedviewmodel/InputFragment.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ninja.shuza.sharedviewmodel

import android.arch.lifecycle.ViewModelProviders
import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.text.Editable
Expand All @@ -8,7 +10,6 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.fragment_input.*
import org.jetbrains.anko.db.INTEGER

/**
*
Expand All @@ -23,13 +24,20 @@ import org.jetbrains.anko.db.INTEGER

class InputFragment : Fragment() {

var activityListener: Communicator? = null
private var sharedViewModel: SharedViewModel? = null

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_input, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
activity?.let {
/**
* create view model in activity scope
*/
sharedViewModel = ViewModelProviders.of(it).get(SharedViewModel::class.java)
}

et_input.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {}

Expand All @@ -42,7 +50,7 @@ class InputFragment : Fragment() {
input = txt.toString().toInt()
}

activityListener?.passData(input)
sharedViewModel?.inputNumber?.postValue(input)
}
}
})
Expand Down
27 changes: 12 additions & 15 deletions app/src/main/java/ninja/shuza/sharedviewmodel/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
package ninja.shuza.sharedviewmodel

import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.properties.Delegates

class MainActivity : AppCompatActivity(), Communicator {

private var inputFrgament: InputFragment by Delegates.notNull()
private var outputFragment: OutputFragment by Delegates.notNull()
class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

inputFrgament = InputFragment()
inputFrgament.activityListener = this
outputFragment = OutputFragment()

supportFragmentManager.beginTransaction().add(R.id.layout_top, InputFragment()).commit()
supportFragmentManager.beginTransaction().add(R.id.layout_bottom, OutputFragment()).commit()

supportFragmentManager.beginTransaction().add(R.id.layout_top, inputFrgament).commit()
supportFragmentManager.beginTransaction().add(R.id.layout_bottom, outputFragment).commit()
}

override fun passData(number: Int) {
val message = resources.getString(R.string.show_input)
tv_show_input.text = "$message $number"

outputFragment.passDataToAnotherFragment(number)
val sharedViewModel = ViewModelProviders.of(this).get(SharedViewModel::class.java)

sharedViewModel.inputNumber.observe(this, Observer {
it?.let {
tv_show_input.text = "$message $it"
}
})
}
}
18 changes: 15 additions & 3 deletions app/src/main/java/ninja/shuza/sharedviewmodel/OutputFragment.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ninja.shuza.sharedviewmodel

import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
Expand All @@ -24,12 +26,22 @@ class OutputFragment : Fragment() {
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
/**
* create view model in activity scope
*/
activity?.let {
val sharedViewModel = ViewModelProviders.of(it).get(SharedViewModel::class.java)

observeInput(sharedViewModel)
}
}

fun passDataToAnotherFragment(input: Int) {
val message = resources.getString(R.string.result)
tv_output.text = "2 x $input = ${input*2}"
private fun observeInput(sharedViewModel: SharedViewModel) {
sharedViewModel.inputNumber.observe(this, Observer {
it?.let {
tv_output.text = "2 x $it = ${2 * it}"
}
})
}

}
19 changes: 19 additions & 0 deletions app/src/main/java/ninja/shuza/sharedviewmodel/SharedViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ninja.shuza.sharedviewmodel

import android.arch.lifecycle.MutableLiveData
import android.arch.lifecycle.ViewModel

/**
*
* := created by: Shuza
* := create date: 09-Aug-18
* := (C) CopyRight Shuza
* := www.shuza.ninja
* := shuza.sa@gmail.com
* := Fun : Coffee : Code
*
**/

class SharedViewModel:ViewModel(){
val inputNumber = MutableLiveData<Int>()
}
3 changes: 2 additions & 1 deletion app/src/main/res/layout/fragment_input.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/input_hint"
android:inputType="numberDecimal"
android:inputType="numberSigned"
android:maxLength="4"
android:maxLines="1" />

</LinearLayout>

0 comments on commit 7b17ade

Please sign in to comment.