Skip to content

Commit

Permalink
added Event<T> with possibility of one-time use, added ActivityWithou…
Browse files Browse the repository at this point in the history
…tVM,
  • Loading branch information
Luteoos committed Feb 4, 2020
1 parent 5ae1101 commit 3b3ddb5
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 83 deletions.
5 changes: 0 additions & 5 deletions mvvmBaseLib/src/main/AndroidManifest.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,70 +9,36 @@ import android.os.Bundle
import androidx.fragment.app.FragmentActivity
import androidx.appcompat.app.AppCompatActivity
import android.view.inputmethod.InputMethodManager
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.OnLifecycleEvent

/**
* Created by Luteoos on 13.09.2018
*/
abstract class BaseActivityMVVM<T: BaseViewModel> : AppCompatActivity() {
abstract class BaseActivityMVVM<T: BaseViewModel> : BaseActivityMVVMWithoutVM() {

/**
* init it with getViewModel<T>(this)
*/
lateinit var viewModel: T

abstract fun getLayoutID(): Int

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

/**
* invoke when VM is assigned
* To invoke when VM is assigned
*/
fun connectToVMMessage(){
viewModel.VMMessage().observe(this, Observer { value -> onVMMessage(value) })
viewModel.message().observe(this, Observer { onVMMessage(it) })
}

/**
* override it to handle message from ViewModel
* 'null' or '0' skips method body
*
*/
open fun onVMMessage(msg: Int?){
if(msg == null || msg == 0)
return
open fun onVMMessage(msg: Event<Int>){
}

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

fun setPortraitOrientation(isPortrait: Boolean) {
requestedOrientation = when(isPortrait){
true -> ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
false -> ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}
}

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

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

companion object {
inline fun <reified T : BaseViewModel> getViewModel(fragment: FragmentActivity): T {
return ViewModelProviders.of(fragment).get(T::class.java)
inline fun <reified T : BaseViewModel> getViewModel(activity: FragmentActivity): T {
return ViewModelProviders.of(activity).get(T::class.java)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.github.luteoos.mvvmbaselib

import android.content.Context
import android.content.pm.ActivityInfo
import android.net.ConnectivityManager
import android.os.Bundle
import android.view.inputmethod.InputMethodManager
import androidx.appcompat.app.AppCompatActivity

abstract class BaseActivityMVVMWithoutVM : AppCompatActivity() {

/**
* override and set layoutId here
*/
abstract fun getLayoutID(): Int

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

/**
* true to portrait <-> default
* false to landscape
*/
fun setPortraitOrientation(isPortrait: Boolean = true) {
requestedOrientation = when(isPortrait){
true -> ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
false -> ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}
}

/**
* cal for hide keyboard from this activity
*/
fun hideKeyboard(){
if(this.currentFocus != null){
val inputMng = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMng.hideSoftInputFromWindow(this.currentFocus!!.windowToken, 0)
}
}

/**
* check if network connection is possible
*/
val isNetworkOnLine: Boolean
get(){
val activeNetInf = (getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager)
.activeNetworkInfo
return activeNetInf != null && activeNetInf.isConnected
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package io.github.luteoos.mvvmbaselib

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders

/**
* Created by Luteoos on 17.09.2018
Expand All @@ -18,34 +14,27 @@ abstract class BaseFragmentMVVM<T: BaseViewModel> : BaseFragmentMVVMWithoutVM(){
*/
lateinit var viewModel: T

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

/**
* invoke when VM is assigned
*/
fun connectToVMMessage(){
viewModel.VMMessage().observe(this, Observer { value -> onVMMessage(value) })
viewModel.message().observe(this, Observer { onVMMessage(it) })
}

/**
* override it to handle message from ViewModel
* 'null' or '0' skips method body
*/
open fun onVMMessage(msg: Int?){
if(msg == null || msg == 0)
return
open fun onVMMessage(msg: Event<Int>){

}

companion object {
inline fun <reified T : BaseViewModel?> getViewModel(fragment: Fragment): T {
return ViewModelProviders.of(fragment).get(T::class.java)
}

inline fun <reified T : BaseViewModel?> getViewModel(fragment: FragmentActivity): T {
return ViewModelProviders.of(fragment).get(T::class.java)
inline fun <reified T : BaseViewModel?> getViewModel(activity: FragmentActivity): T {
return ViewModelProviders.of(activity).get(T::class.java)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,36 @@ package io.github.luteoos.mvvmbaselib
import android.content.Context
import android.net.ConnectivityManager
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.view.WindowManager
import androidx.fragment.app.Fragment

/**
* Created by Luteoos on 17.09.2018
*/
abstract class BaseFragmentMVVMWithoutVM : Fragment() {

/**
* override and set layoutId here
*/
abstract fun getLayoutID(): Int

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

/**
* call to hide keyboard
*/
fun hideKeyboard(){
if(activity!!.currentFocus != null){
val inputMng = activity!!.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMng.hideSoftInputFromWindow(activity!!.currentFocus!!.windowToken, 0)
}
activity?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
}

/**
* check if network connection is possible
*/
val isNetworkOnLine: Boolean
get(){
val activeNetInf = (activity?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,18 @@ abstract class BaseViewModel : ViewModel(){
/**
* Serves as quick bus between VM and owner-View
*/
private val message : MutableLiveData<Int> = MutableLiveData()

init {
message.value = 0
}
private val message : MutableLiveData<Event<Int>> = MutableLiveData()

/**
* use it to assign message:LiveData
* do not use 0 value, its considered default empty
* posts value only if hs activeObservers
*/
protected fun send(msg: Int){
message.value = msg
message.value = 0
message.let {
if(it.hasActiveObservers())
it.postValue(Event(msg))
}
}

fun VMMessage(): LiveData<Int> = message

fun message(): LiveData<Event<Int>> = message
}
29 changes: 29 additions & 0 deletions mvvmBaseLib/src/main/java/io/github/luteoos/mvvmbaselib/Event.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.github.luteoos.mvvmbaselib

/**
* Base class for single use Events
*
* Created by Luteoos on 24.01.2020
*/
open class Event<out T>(private val payload: T) {

/**
* lock external write
*/
var isUsed = false
private set

/**
* return payload and marks as used
*/
fun get(): T? =
when(isUsed){
true -> null
false -> {isUsed = true; payload}
}

/**
* return payload ignoring isUsed
*/
fun peek() = payload
}

0 comments on commit 3b3ddb5

Please sign in to comment.