├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ └── styles.xml │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── layout │ │ │ │ ├── activity_greeter.xml │ │ │ │ ├── fragment_greeter_fragment_two.xml │ │ │ │ └── fragment_greeter_fragment_one.xml │ │ │ ├── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ │ └── drawable │ │ │ │ └── ic_launcher_background.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── android │ │ │ │ └── grpcmvvm │ │ │ │ ├── view │ │ │ │ ├── GreeterViewModelFactory.kt │ │ │ │ ├── GreeterViewModel.kt │ │ │ │ ├── GreeterActivity.kt │ │ │ │ ├── GreeterFragmentOne.kt │ │ │ │ └── GreeterFragmentTwo.kt │ │ │ │ ├── grpc │ │ │ │ └── GrpcService.kt │ │ │ │ ├── data │ │ │ │ ├── GreeterRepository.kt │ │ │ │ └── GreeterRemoteDataSource.kt │ │ │ │ ├── adapters │ │ │ │ └── GreeterFragmentsViewPagerAdapter.kt │ │ │ │ └── di │ │ │ │ └── Injector.kt │ │ ├── AndroidManifest.xml │ │ └── proto │ │ │ └── helloworld.proto │ ├── androidTest │ │ └── java │ │ │ └── com │ │ │ └── android │ │ │ └── grpcmvvm │ │ │ └── ExampleInstrumentedTest.kt │ └── test │ │ └── java │ │ └── com │ │ └── android │ │ └── grpcmvvm │ │ ├── testrules │ │ └── CoroutinesTestRule.kt │ │ └── view │ │ └── GreeterViewModelUnitTest.kt ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .idea ├── codeStyles │ ├── codeStyleConfig.xml │ └── Project.xml └── runConfigurations.xml ├── gradle.properties ├── .gitignore ├── README.md ├── gradlew.bat └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | rootProject.name='GrpcMvvm' 3 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benz93chung/GrpcMvvm/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benz93chung/GrpcMvvm/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benz93chung/GrpcMvvm/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benz93chung/GrpcMvvm/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benz93chung/GrpcMvvm/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benz93chung/GrpcMvvm/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benz93chung/GrpcMvvm/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benz93chung/GrpcMvvm/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benz93chung/GrpcMvvm/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benz93chung/GrpcMvvm/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benz93chung/GrpcMvvm/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | GrpcMvvm 3 | 4 | 5 | Hello blank fragment 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | 7 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Sep 03 20:43:56 MYT 2019 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/java/com/android/grpcmvvm/view/GreeterViewModelFactory.kt: -------------------------------------------------------------------------------- 1 | package com.android.grpcmvvm.view 2 | 3 | import androidx.lifecycle.ViewModel 4 | import androidx.lifecycle.ViewModelProvider 5 | import com.android.grpcmvvm.data.GreeterRepository 6 | 7 | class GreeterViewModelFactory(private val greeterRepository: GreeterRepository): ViewModelProvider.NewInstanceFactory() { 8 | @Suppress("UNCHECKED_CAST") 9 | override fun create(modelClass: Class): T { 10 | return GreeterViewModel(greeterRepository) as T 11 | } 12 | } -------------------------------------------------------------------------------- /app/src/main/java/com/android/grpcmvvm/grpc/GrpcService.kt: -------------------------------------------------------------------------------- 1 | package com.android.grpcmvvm.grpc 2 | 3 | import io.grpc.ManagedChannel 4 | import io.grpc.ManagedChannelBuilder 5 | import java.util.concurrent.Executors 6 | 7 | class GrpcService(private val host: String, private val port: Int) { 8 | 9 | fun createManagedChannel(): ManagedChannel { 10 | return ManagedChannelBuilder 11 | .forAddress(host, port) 12 | .executor(Executors.newSingleThreadExecutor()) 13 | .usePlaintext() 14 | .build() 15 | } 16 | } -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/java/com/android/grpcmvvm/view/GreeterViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.android.grpcmvvm.view 2 | 3 | import androidx.lifecycle.LiveData 4 | import androidx.lifecycle.ViewModel 5 | import androidx.lifecycle.viewModelScope 6 | import com.android.grpcmvvm.data.GreeterRepository 7 | import kotlinx.coroutines.launch 8 | 9 | class GreeterViewModel constructor(private val greeterRepository: GreeterRepository) : ViewModel() { 10 | 11 | val greeting: LiveData 12 | get() = greeterRepository.greeting 13 | 14 | fun sayHello(message: String) { 15 | viewModelScope.launch { 16 | greeterRepository.sayHello(message) 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /app/src/main/java/com/android/grpcmvvm/data/GreeterRepository.kt: -------------------------------------------------------------------------------- 1 | package com.android.grpcmvvm.data 2 | 3 | import androidx.lifecycle.LiveData 4 | import androidx.lifecycle.MutableLiveData 5 | 6 | class GreeterRepository constructor(private val greeterRemoteDataSource: GreeterRemoteDataSource) { 7 | private val response = MutableLiveData() 8 | 9 | val greeting: LiveData 10 | get() { 11 | if (response.value == null) { 12 | response.value = "Hello world!" 13 | } 14 | 15 | return response 16 | } 17 | 18 | suspend fun sayHello(message: String) { 19 | response.value = greeterRemoteDataSource.sayHello(message) 20 | } 21 | } -------------------------------------------------------------------------------- /app/src/androidTest/java/com/android/grpcmvvm/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.android.grpcmvvm 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.android.grpcmvvm", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/android/grpcmvvm/view/GreeterActivity.kt: -------------------------------------------------------------------------------- 1 | package com.android.grpcmvvm.view 2 | 3 | import androidx.appcompat.app.AppCompatActivity 4 | import android.os.Bundle 5 | import com.android.grpcmvvm.R 6 | import com.android.grpcmvvm.adapters.GreeterFragmentsViewPagerAdapter 7 | import kotlinx.android.synthetic.main.activity_greeter.* 8 | 9 | class GreeterActivity : AppCompatActivity() { 10 | 11 | override fun onCreate(savedInstanceState: Bundle?) { 12 | super.onCreate(savedInstanceState) 13 | setContentView(R.layout.activity_greeter) 14 | 15 | val greeterFragmentsViewPagerAdapter = GreeterFragmentsViewPagerAdapter(supportFragmentManager) 16 | greeterViewPager.adapter = greeterFragmentsViewPagerAdapter 17 | 18 | greeterTabLayout.setupWithViewPager(greeterViewPager) 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/android/grpcmvvm/adapters/GreeterFragmentsViewPagerAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.android.grpcmvvm.adapters 2 | 3 | import androidx.fragment.app.Fragment 4 | import androidx.fragment.app.FragmentManager 5 | import androidx.fragment.app.FragmentStatePagerAdapter 6 | import com.android.grpcmvvm.view.GreeterFragmentOne 7 | import com.android.grpcmvvm.view.GreeterFragmentTwo 8 | 9 | class GreeterFragmentsViewPagerAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm) { 10 | override fun getItem(position: Int): Fragment { 11 | return when (position) { 12 | 0 -> GreeterFragmentOne() 13 | else -> GreeterFragmentTwo() 14 | } 15 | } 16 | 17 | override fun getCount(): Int = 2 18 | 19 | override fun getPageTitle(position: Int): CharSequence? { 20 | return when (position) { 21 | 0 -> "Fragment 1" 22 | else -> "Fragment 2" 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /app/src/test/java/com/android/grpcmvvm/testrules/CoroutinesTestRule.kt: -------------------------------------------------------------------------------- 1 | package com.android.grpcmvvm.testrules 2 | 3 | import kotlinx.coroutines.Dispatchers 4 | import kotlinx.coroutines.ExperimentalCoroutinesApi 5 | import kotlinx.coroutines.test.TestCoroutineDispatcher 6 | import kotlinx.coroutines.test.resetMain 7 | import kotlinx.coroutines.test.setMain 8 | import org.junit.rules.TestWatcher 9 | import org.junit.runner.Description 10 | 11 | @ExperimentalCoroutinesApi 12 | class CoroutinesTestRule(private val dispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()) : TestWatcher() { 13 | override fun starting(description: Description?) { 14 | super.starting(description) 15 | Dispatchers.setMain(dispatcher) 16 | } 17 | 18 | override fun finished(description: Description?) { 19 | super.finished(description) 20 | Dispatchers.resetMain() 21 | dispatcher.cleanupTestCoroutines() 22 | } 23 | } -------------------------------------------------------------------------------- /app/src/main/java/com/android/grpcmvvm/di/Injector.kt: -------------------------------------------------------------------------------- 1 | package com.android.grpcmvvm.di 2 | 3 | import com.android.grpcmvvm.data.GreeterRemoteDataSource 4 | import com.android.grpcmvvm.data.GreeterRepository 5 | import com.android.grpcmvvm.grpc.GrpcService 6 | import com.android.grpcmvvm.view.GreeterViewModelFactory 7 | 8 | object Injector { 9 | // Provide GrpcService 10 | private fun provideGrpcService() = GrpcService( 11 | "192.168.1.107", 12 | 50051 13 | ) 14 | 15 | // Provide DataSource 16 | private fun provideGreeterRemoteDataSource(grpcService: GrpcService) = GreeterRemoteDataSource(grpcService) 17 | 18 | // Provide Repository 19 | private fun provideGreeterRepository(greeterRemoteDataSource: GreeterRemoteDataSource) = GreeterRepository(greeterRemoteDataSource) 20 | 21 | // Provide GreeterViewModel factory 22 | fun provideGreeterViewModelFactory() = GreeterViewModelFactory( 23 | provideGreeterRepository( 24 | provideGreeterRemoteDataSource( 25 | provideGrpcService() 26 | ) 27 | ) 28 | ) 29 | } -------------------------------------------------------------------------------- /app/src/main/java/com/android/grpcmvvm/data/GreeterRemoteDataSource.kt: -------------------------------------------------------------------------------- 1 | package com.android.grpcmvvm.data 2 | 3 | import com.android.grpcmvvm.grpc.GrpcService 4 | import io.grpc.ManagedChannel 5 | import io.grpc.StatusRuntimeException 6 | import io.grpc.examples.helloworld.GreeterGrpc 7 | import io.grpc.examples.helloworld.HelloRequest 8 | import kotlinx.coroutines.Dispatchers 9 | import kotlinx.coroutines.withContext 10 | 11 | class GreeterRemoteDataSource constructor(private val grpcService: GrpcService) { 12 | private lateinit var channel: ManagedChannel 13 | 14 | suspend fun sayHello(message: String): String = 15 | withContext(Dispatchers.IO) { 16 | channel = grpcService.createManagedChannel() 17 | 18 | val stub = GreeterGrpc.newBlockingStub(channel) 19 | val request = HelloRequest.newBuilder().setName(message).build() 20 | 21 | return@withContext try { 22 | stub.sayHello(request).message 23 | } catch (e: StatusRuntimeException) { 24 | e.printStackTrace() 25 | e.status.code.toString() + "\n" + e.status.cause.toString() 26 | } finally { 27 | channel.shutdown() 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app's APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Automatically convert third-party libraries to use AndroidX 19 | android.enableJetifier=true 20 | # Kotlin code style for this project: "official" or "obsolete": 21 | kotlin.code.style=official 22 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_greeter.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 17 | 18 | 26 | -------------------------------------------------------------------------------- /app/src/main/proto/helloworld.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2015 The gRPC Authors 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | syntax = "proto3"; 15 | 16 | option java_multiple_files = true; 17 | option java_package = "io.grpc.examples.helloworld"; 18 | option java_outer_classname = "HelloWorldProto"; 19 | option objc_class_prefix = "HLW"; 20 | 21 | package helloworld; 22 | 23 | // The getThatGreeting service definition. 24 | service Greeter { 25 | // Sends a getThatGreeting 26 | rpc SayHello (HelloRequest) returns (HelloReply) {} 27 | } 28 | 29 | // The request message containing the user's name. 30 | message HelloRequest { 31 | string name = 1; 32 | } 33 | 34 | // The response message containing the greetings 35 | message HelloReply { 36 | string message = 1; 37 | } -------------------------------------------------------------------------------- /app/src/main/java/com/android/grpcmvvm/view/GreeterFragmentOne.kt: -------------------------------------------------------------------------------- 1 | package com.android.grpcmvvm.view 2 | 3 | import android.os.Bundle 4 | import androidx.fragment.app.Fragment 5 | import android.view.LayoutInflater 6 | import android.view.View 7 | import android.view.ViewGroup 8 | import androidx.lifecycle.Observer 9 | import androidx.lifecycle.ViewModelProvider 10 | 11 | import com.android.grpcmvvm.R 12 | import com.android.grpcmvvm.di.Injector 13 | import kotlinx.android.synthetic.main.fragment_greeter_fragment_one.* 14 | 15 | class GreeterFragmentOne : Fragment() { 16 | 17 | private lateinit var greeterViewModel: GreeterViewModel 18 | 19 | override fun onCreateView( 20 | inflater: LayoutInflater, container: ViewGroup?, 21 | savedInstanceState: Bundle? 22 | ): View? { 23 | // Inflate the layout for this fragment 24 | return inflater.inflate(R.layout.fragment_greeter_fragment_one, container, false) 25 | } 26 | 27 | override fun onActivityCreated(savedInstanceState: Bundle?) { 28 | super.onActivityCreated(savedInstanceState) 29 | 30 | greeterViewModel = activity?.run { 31 | ViewModelProvider(this, Injector.provideGreeterViewModelFactory()).get(GreeterViewModel::class.java) 32 | } ?: throw Exception("Activity not found") 33 | 34 | greeterViewModel.greeting.observe(viewLifecycleOwner, Observer { greeting -> 35 | fragment1ResultView.text = greeting 36 | }) 37 | 38 | fragment1SendBtn.setOnClickListener { 39 | greeterViewModel.sayHello(fragment1InputField.text.toString()) 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /app/src/main/java/com/android/grpcmvvm/view/GreeterFragmentTwo.kt: -------------------------------------------------------------------------------- 1 | package com.android.grpcmvvm.view 2 | 3 | import android.os.Bundle 4 | import androidx.fragment.app.Fragment 5 | import android.view.LayoutInflater 6 | import android.view.View 7 | import android.view.ViewGroup 8 | import androidx.lifecycle.Observer 9 | import androidx.lifecycle.ViewModelProvider 10 | 11 | import com.android.grpcmvvm.R 12 | import com.android.grpcmvvm.di.Injector 13 | import kotlinx.android.synthetic.main.fragment_greeter_fragment_two.* 14 | 15 | class GreeterFragmentTwo : Fragment() { 16 | 17 | private lateinit var greeterViewModel: GreeterViewModel 18 | 19 | override fun onCreateView( 20 | inflater: LayoutInflater, container: ViewGroup?, 21 | savedInstanceState: Bundle? 22 | ): View? { 23 | // Inflate the layout for this fragment 24 | return inflater.inflate(R.layout.fragment_greeter_fragment_two, container, false) 25 | } 26 | 27 | override fun onActivityCreated(savedInstanceState: Bundle?) { 28 | super.onActivityCreated(savedInstanceState) 29 | 30 | greeterViewModel = activity?.run { 31 | ViewModelProvider(this, Injector.provideGreeterViewModelFactory()).get(GreeterViewModel::class.java) 32 | } ?: throw Exception("Activity not found") 33 | 34 | greeterViewModel.greeting.observe(viewLifecycleOwner, Observer { greeting -> 35 | fragment2ResultView.text = greeting 36 | }) 37 | 38 | fragment2SendBtn.setOnClickListener { 39 | greeterViewModel.sayHello(fragment2InputField.text.toString()) 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | *.aab 5 | 6 | # Files for the ART/Dalvik VM 7 | *.dex 8 | 9 | # Java class files 10 | *.class 11 | 12 | # Generated files 13 | bin/ 14 | gen/ 15 | out/ 16 | release/ 17 | 18 | # Gradle files 19 | .gradle/ 20 | build/ 21 | 22 | # Local configuration file (sdk path, etc) 23 | local.properties 24 | 25 | # Proguard folder generated by Eclipse 26 | proguard/ 27 | 28 | # Log Files 29 | *.log 30 | 31 | # Android Studio Navigation editor temp files 32 | .navigation/ 33 | 34 | # Android Studio captures folder 35 | captures/ 36 | 37 | # IntelliJ 38 | *.iml 39 | .idea/workspace.xml 40 | .idea/tasks.xml 41 | .idea/gradle.xml 42 | .idea/assetWizardSettings.xml 43 | .idea/dictionaries 44 | .idea/libraries 45 | # Android Studio 3 in .gitignore file. 46 | .idea/caches 47 | .idea/modules.xml 48 | .idea/misc.xml 49 | 50 | # Keystore files 51 | # Uncomment the following lines if you do not want to check your keystore files in. 52 | #*.jks 53 | #*.keystore 54 | 55 | # External native build folder generated in Android Studio 2.2 and later 56 | .externalNativeBuild 57 | 58 | # Google Services (e.g. APIs or Firebase) 59 | # google-services.json 60 | 61 | # Freeline 62 | freeline.py 63 | freeline/ 64 | freeline_project_description.json 65 | 66 | # fastlane 67 | fastlane/report.xml 68 | fastlane/Preview.html 69 | fastlane/screenshots 70 | fastlane/test_output 71 | fastlane/readme.md 72 | 73 | # Version control 74 | vcs.xml 75 | 76 | # lint 77 | lint/intermediates/ 78 | lint/generated/ 79 | lint/outputs/ 80 | lint/tmp/ 81 | # lint/reports/ 82 | 83 | # DS_Store 84 | .DS_Store 85 | 86 | # .settings 87 | .settings 88 | app/.settings 89 | 90 | #.project 91 | .project 92 | app/.project 93 | 94 | #.classpath 95 | app/.classpath -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_greeter_fragment_two.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 25 | 26 | 36 | 37 |