Skip to content

Commit

Permalink
simplified lib, removed realm and rx
Browse files Browse the repository at this point in the history
  • Loading branch information
Luteoos committed Apr 16, 2019
1 parent 780fbf4 commit 4d63b60
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 126 deletions.
19 changes: 3 additions & 16 deletions mvvmBaseLib/build.gradle
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
apply plugin: 'kotlin-kapt'
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'realm-android'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
versionCode 2
versionName "2.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
ext {
rxjava2_ver = "2.1.7"
}
productFlavors {
}
Expand All @@ -30,15 +26,6 @@ dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
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'
//rxJava
implementation 'io.reactivex.rxjava2:rxjava:2.1.12'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation 'com.jakewharton.rxbinding2:rxbinding:2.0.0'
implementation 'com.eightbitlab:rxbus:1.0.2'
implementation "android.arch.lifecycle:extensions:1.1.1"
implementation "android.arch.lifecycle:viewmodel:1.1.1"
}

21 changes: 0 additions & 21 deletions mvvmBaseLib/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -1,21 +0,0 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,35 @@ abstract class BaseActivityMVVM<T: BaseViewModel> : AppCompatActivity() {
*/
lateinit var viewModel: T

protected abstract fun getLayoutID(): Int
abstract fun getLayoutID(): Int

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
hideKeyboard()
setContentView(getLayoutID())
}

protected fun connectToVMMessage(){
/**
* invoke this after creating viewmodel to observe message and use onVMMessage
*/
/**
* invoke when VM is assigned
*/
fun connectToVMMessage(){
viewModel.VMMessage().observe(this, Observer { value -> onVMMessage(value) })
}

/**
* override it to handle message from ViewModel
* 'null' or '0' skips method body
*/
open fun onVMMessage(msg: String?){

open fun onVMMessage(msg: Int?){
if(msg == null || msg == 0)
return
}

override fun onBackPressed() {
hideKeyboard()
super.onBackPressed()
}

override fun onStop() {
viewModel.detachBus()
super.onStop()
}

override fun onDestroy() {
viewModel.detachDisposable()
super.onDestroy()
}

fun setPortraitOrientation(isPortrait: Boolean) {
requestedOrientation = when(isPortrait){
true -> ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
Expand All @@ -79,7 +71,7 @@ abstract class BaseActivityMVVM<T: BaseViewModel> : AppCompatActivity() {
}

companion object {
inline fun <reified T : BaseViewModel?> getViewModel(fragment: FragmentActivity): T {
inline fun <reified T : BaseViewModel> getViewModel(fragment: FragmentActivity): T {
return ViewModelProviders.of(fragment).get(T::class.java)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,42 @@ package com.luteoos.kotlin.mvvmbaselib

import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.content.Context
import android.net.ConnectivityManager
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager

/**
* Created by Luteoos on 17.09.2018
*/
abstract class BaseFragmentMVVM<T: BaseViewModel> : Fragment(){
abstract class BaseFragmentMVVM<T: BaseViewModel> : BaseFragmentMVVMWithoutVM(){
/**
* init it with getViewModel<T>(this)
*/
lateinit var viewModel: T

protected abstract fun getLayoutID(): Int

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

protected fun connectToVMMessage(){
/**
* invoke this after creating viewmodel to observe message and use onVMMessage
*/
/**
* invoke when VM is assigned
*/
fun connectToVMMessage(){
viewModel.VMMessage().observe(this, Observer { value -> onVMMessage(value) })
}

/**
* override it to handle message from ViewModel
* 'null' or '0' skips method body
*/
open fun onVMMessage(msg: String?){

open fun onVMMessage(msg: Int?){
if(msg == null || msg == 0)
return
}

override fun onStop() {
super.onStop()
viewModel.detachBus()
}

override fun onDestroyView() {
super.onDestroyView()
viewModel.detachDisposable()
}

fun hideKeyboard(){
if(activity!!.currentFocus != null){
val inputMng = activity!!.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMng.hideSoftInputFromWindow(activity!!.currentFocus!!.windowToken, 0)
}
}

val isNetworkOnLine: Boolean
get(){
val activeNetInf = (activity?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager)
.activeNetworkInfo
return activeNetInf != null && activeNetInf.isConnected
}

companion object {
inline fun <reified T : BaseViewModel?> getViewModel(fragment: Fragment): T {
return ViewModelProviders.of(fragment).get(T::class.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.luteoos.kotlin.mvvmbaselib

import android.content.Context
import android.net.ConnectivityManager
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
Expand All @@ -13,7 +14,7 @@ import android.view.inputmethod.InputMethodManager
*/
abstract class BaseFragmentMVVMWithoutVM : Fragment() {

protected abstract fun getLayoutID(): Int
abstract fun getLayoutID(): Int

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
hideKeyboard()
Expand All @@ -26,4 +27,11 @@ abstract class BaseFragmentMVVMWithoutVM : Fragment() {
inputMng.hideSoftInputFromWindow(activity!!.currentFocus!!.windowToken, 0)
}
}

val isNetworkOnLine: Boolean
get(){
val activeNetInf = (activity?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager)
.activeNetworkInfo
return activeNetInf != null && activeNetInf.isConnected
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,32 @@ package com.luteoos.kotlin.mvvmbaselib
import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MutableLiveData
import android.arch.lifecycle.ViewModel
import com.eightbitlab.rxbus.Bus
import io.reactivex.disposables.CompositeDisposable
import io.realm.Realm

/**
* Created by Luteoos on 13.09.2018
* uses android.arch.lifecycle.MutableLiveData and ViewModel
*/

abstract class BaseViewModel : ViewModel(){

/**
*use to mark if ViewModel is already initialized
* Serves as quick bus between VM and owner-View
*/
var isInitialized: Boolean = false
val disposable : CompositeDisposable = CompositeDisposable()
val realm = Realm.getDefaultInstance()
private val message : MutableLiveData<String> = MutableLiveData()
private val message : MutableLiveData<Int> = MutableLiveData()

init {
message.value = 1.toString()
isInitialized = true
message.value = 0
}

/**
* use it to assign message:LiveData
* after sets LD to "" to avoid multiple calls
* do not use 0 value, its considered default empty
*/
protected fun send(msg: String){
protected fun send(msg: Int){
message.value = msg
message.value = ""
message.value = 0
}

fun VMMessage(): LiveData<String> = message
fun VMMessage(): LiveData<Int> = message

fun detachBus(){
Bus.unregister(this)
}
fun detachDisposable(){
disposable.clear()
}

override fun onCleared() {
detachDisposable()
detachBus()
realm?.close()
super.onCleared()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,16 @@ import android.annotation.SuppressLint

@SuppressLint("ValidFragment")
private class Test() {
private val vm = object : BaseViewModel(){}
private val vm = @SuppressLint("StaticFieldLeak")
object : BaseViewModel(){}
private val a = object : BaseActivityMVVM<BaseViewModel>(){
override fun onVMMessage(msg: String?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

override fun getLayoutID(): Int {
viewModel = getViewModel(this)
return 0
}
}
private var b = object : BaseFragmentMVVM<BaseViewModel>(){
override fun onVMMessage(msg: String?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

override fun getLayoutID(): Int {
viewModel = getViewModel(this)
Expand Down

0 comments on commit 4d63b60

Please sign in to comment.