Skip to content

Commit

Permalink
Used a more concise syntax for observing a repository.
Browse files Browse the repository at this point in the history
  • Loading branch information
syrop committed Apr 29, 2019
1 parent 649b090 commit 0343b11
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ class CommDetailsFragment : Fragment(R.layout.fr_comm_details) {

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.comm_details, menu)
(vm.isAdmin + this) { menu.findItem(R.id.action_add_admin).isVisible = it }
(vm.isAdmin + this) { menu.findItem(R.id.action_add_admin)?.isVisible = it }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ package pl.org.seva.events.comm

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch

class CommViewModel : ViewModel() {

init { viewModelScope.launch { comms { comm = comms[comm.name] } } }
init { (comms vm this) { comm = comms[comm.name] } }

var comm = Comm.DUMMY
set(value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ package pl.org.seva.events.main.model
import android.os.Looper
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import pl.org.seva.events.main.model.livedata.DefaultHotData
import pl.org.seva.events.main.model.livedata.HotData
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.sendBlocking
import kotlinx.coroutines.launch

abstract class LiveRepository {

Expand All @@ -42,6 +45,10 @@ abstract class LiveRepository {
channel.sendBlocking(Unit)
}

infix fun vm(vm: ViewModel) = { block: () -> Unit ->
vm.viewModelScope.launch { this@LiveRepository(block) }
}

operator fun plus(owner: LifecycleOwner): HotData<Unit> = DefaultHotData(liveData, owner)

suspend operator fun invoke(block: () -> Unit) {
Expand Down
2 changes: 1 addition & 1 deletion events/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<!-- Events fragment. -->
<string name="events_activity_dismiss_event">Do you want to dismiss event data?</string>
<string name="events_action_login">Login</string>
<string name="events_action_login">Log in</string>
<string name="events_action_log_out">Log out</string>
<string name="events_action_comms">Communities</string>
<string name="events_action_add_comm">Add a community</string>
Expand Down

0 comments on commit 0343b11

Please sign in to comment.