How can I get all implementations of an interface at runtime in Kotlin - reflection

I know I can get all subclasses of a sealed class in Kotlin, but I'm looking for a way to get all implementations of an interface.
So instead of ...
sealed class HotDrinkFactory {
abstract fun prepare(amount: Int): HotDrink
}
class TeaFactory : HotDrinkFactory() {
override fun prepare(amount: Int): HotDrink {
...
}
}
class CoffeeFactory : HotDrinkFactory() {
override fun prepare(amount: Int): HotDrink {
...
}
}
fun main(args: Array<String>) {
val hotDrinkFactories = HotDrinkFactory::class.sealedSubclasses
hotDrinkFactories.forEach { println(it::class.qualifiedName) }
}
... I would like to have
interface HotDrinkFactory {
fun prepare(amount: Int): HotDrink
}
class TeaFactory : HotDrinkFactory {
override fun prepare(amount: Int): HotDrink {
...
}
}
class CoffeeFactory : HotDrinkFactory {
override fun prepare(amount: Int): HotDrink {
...
}
}
fun main(args: Array<String>) {
val hotDrinkFactories = HotDrinkFactory::class.<<< Something here? >>>
hotDrinkFactories.forEach { println(it::class.qualifiedName) }
}

Related

SetOnClickListener for Button within RecyclerView to access viewmodel and perform action on room database

I would like to add a "delete" button in a RecyclerView showing a list of "Users" present in a Room Database. The button should permit to delete the single user when clicking on the button. I have tried to insert a function in Myviewholder, but when I call it in OnBindViewHolder the error concerns the initialization of the mUserViewModel. Do you have any suggestion on it?
This is the adapter:
class ListAdapterUser: RecyclerView.Adapter<ListAdapterUser.MyViewHolder>() {
private var UserList = emptyList<User>()
private lateinit var mUserViewModel: UserViewModel
class MyViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
val button = itemView.findViewById<Button>(R.id.deleteoption)
fun deleteitem(item: User, viewModel: UserViewModel){
button.setOnClickListener{
viewModel.deleteUser(item)
}}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.custom_rowUser, parent, false))
}
override fun getItemCount(): Int {
return UserList.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val currentItem = UserList[position]
holder.itemView.textview_valueUser.text = currentItem.Uservalue.toString()
holder.deleteitem(currentItem, mUserViewModel)
}
fun setUserData(User: List<User>){
this.UserList = User
notifyDataSetChanged()
}
}
Thank you!
Solved obtaining the viewmodel initialized in the fragment passing it with setUserData function. Here the final code:
class ListAdapterUser: RecyclerView.Adapter<ListAdapterUser.MyViewHolder>() {
private var UserList = emptyList<User>()
private lateinit var mUserViewModel: UserViewModel
class MyViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
val button = itemView.findViewById<Button>(R.id.deleteoption)
fun deleteitem(item: User, viewModel: UserViewModel){
button.setOnClickListener{
viewModel.deleteUser(item)
}}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.custom_rowUser, parent, false))
}
override fun getItemCount(): Int {
return UserList.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val currentItem = UserList[position]
holder.itemView.textview_valueUser.text = currentItem.Uservalue.toString()
holder.deleteitem(currentItem, mUserViewModel)
}
fun setUserData(User: List<User>, viewModel: UserViewModel)){
this.UserList = User
this.mUserViewModel = viewModel
notifyDataSetChanged()
}}

Retrofit service works only once

I'm using retrofit to login to my API, but if, for example, I insert a wrong password and try to login again, the service is not called again.
It seems that the second call gets "stuck" in the repository, never reaches the service.
This is my first android app and i'm struggling with this situation.
Tks in advance for any help you can provide on this.
This is the code for my app.
DI
class Fiscalizacao : Application(), KodeinAware {
override val kodein = Kodein.lazy {
import(androidModule(this#Fiscalizacao))
bind() from singleton {
Autenticacao(
instance()
)
}
bind<IAutenticacaoDataSource>() with singleton {
AutenticacaoDataSourceImpl(
instance()
)
}
bind<IAuthenticationRepository>() with singleton {
AuthenticationRepositoryImpl(
instance()
)
}
bind() from provider { AutenticacaoViewModelFactory(instance(), instance()) }
}
override fun onCreate() {
super.onCreate()
AndroidThreeTen.init(this)
}
}
Service
interface Autenticacao {
#POST("auth")
fun authAsync(#Body user: RequestBody): Deferred<AutenticacaoResponseResource>
companion object {
operator fun invoke(connectivityInterceptor: IConnectivityInterceptor): Autenticacao {
val okHttpClient = OkHttpClient.Builder()
.build()
return Retrofit
.Builder()
.client(okHttpClient)
.baseUrl("http://10.110.100.216/identity/")
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(Autenticacao::class.java)
}
}
}
DataSource
class AutenticacaoDataSourceImpl(
private val autenticacao: Autenticacao
) :
IAutenticacaoDataSource {
private val _authResponse = MutableLiveData<AutenticacaoResponseResource>()
override val authResponse: LiveData<AutenticacaoResponseResource>
get() = _authResponse
override suspend fun auth(user: CredentialsResource?): LiveData<AutenticacaoResponseResource> {
try {
val userJsonObject = JsonObject()
userJsonObject.addProperty(Parameters.USERNAME.value, user?.utilizador)
userJsonObject.addProperty(Parameters.PASSWORD.value, user?.password)
val result = autenticacao
.authAsync(
userJsonObject.toString()
.toRequestBody(contentType = "application/json; charset=utf8".toMediaTypeOrNull())
)
.await()
_authResponse.postValue(result)
} catch (e: Exception) {
Log.e(e.cause.toString(), e.message, e)
}
return authResponse
}
}
Repository
class AuthenticationRepositoryImpl(
private val autenticacaoDataSource: IAutenticacaoDataSource
) : IAuthenticationRepository {
override suspend fun auth(user: CredentialsResource?): LiveData<AutenticacaoResponseResource> {
return withContext(Dispatchers.IO) {
return#withContext autenticacaoDataSource.auth(user)
}
}
}
ViewModel
class AutenticacaoViewModel(
private val authenticationRepository: IAuthenticationRepository,
) : ViewModel() {
lateinit var user:CredentialsResource
val login by lazyDeferred {
authenticationRepository.auth(user)
}
}
View Model Factory
class AutenticacaoViewModelFactory(private val authenticationRepository: IAuthenticationRepository, ) :
ViewModelProvider.NewInstanceFactory() {
#Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return AutenticacaoViewModel(authenticationRepository) as T
}
}
Coroutines
fun <T> lazyDeferred(block: suspend CoroutineScope.() -> T): Lazy<Deferred<T>>{
return lazy {
GlobalScope.async(start = CoroutineStart.LAZY) {
block.invoke(this)
}
}
}
class AutenticacaoFragment : ScopedFragment(), KodeinAware {
override val kodein by closestKodein()
private val authViewModelFactory: AutenticacaoViewModelFactory by instance()
private lateinit var viewModel: AutenticacaoViewModel
val gson = Gson()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding: AutenticacaoFragmentBinding =
DataBindingUtil.inflate(inflater, R.layout.autenticacao_fragment, container, false)
binding.model =
AppGlobalObject
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = ViewModelProvider(this, authViewModelFactory)
.get(AutenticacaoViewModel::class.java)
mAutenticacao.setOnClickListener(listenerService)
}
private val listenerService =
View.OnClickListener {
when (it.id) {
R.id.mAutenticacao -> {
login(this.requireContext())
}
}
}
private fun login(context: Context) = launch {
viewModel.user = gson.fromJson(
gson.toJson(AppGlobalObject.autenticacao.user),
CredentialsResource::class.java
)
val result = viewModel.login.await()
result.observe(viewLifecycleOwner, Observer { response ->
if (response == null) return#Observer
val login = Utilities().validateStatusCodeOK(response.error)
when {
login -> {
Utilities().setLoginStatus(login, context)
}
else -> {
mPassword.error = "Erro no Login"
}
}
})
}

Initiating a flow session from a flow that is annoted with InitiatedBy to a flow which is also InitiatedBy

Is it possible to initiate a flow session from a flow that is annoted with InitiatedBy to a flow which is also annoted with InitiatedBy?
For example:
#InitiatingFlow
Class FlowA
#InitiatedBy(FlowA.class)
Class FlowB
#InitiatedBy(FlowB.class)
Class FlowC
is it possible to achieve sequence of flow session like:
A->B->C ?
Yes, this is possible, as follows:
#InitiatingFlow
#StartableByRPC
class Initiator(val firstCounterparty: Party, val secondCounterparty: Party) : FlowLogic<Int>() {
override val progressTracker = ProgressTracker()
#Suspendable
override fun call(): Int {
val flowSession = initiateFlow(firstCounterparty)
flowSession.send(secondCounterparty)
return flowSession.receive<Int>().unwrap { it }
}
}
#InitiatingFlow
#InitiatedBy(Initiator::class)
class Responder(val flowSession: FlowSession) : FlowLogic<Unit>() {
#Suspendable
override fun call() {
val secondCounterparty = flowSession.receive<Party>().unwrap { it }
val newFlowSession = initiateFlow(secondCounterparty)
val int = newFlowSession.receive<Int>().unwrap { it }
flowSession.send(int)
}
}
#InitiatingFlow
#InitiatedBy(Responder::class)
class ResponderResponder(val flowSession: FlowSession) : FlowLogic<Unit>() {
#Suspendable
override fun call() {
flowSession.send(3)
}
}
However, there is one major caveat. In Corda 3.x, you can't have two FlowSessions with the same counterparty in the same flow. So either you need to disallow the case where A -> B -> A, as follows:
#InitiatingFlow
#StartableByRPC
class Initiator(val firstCounterparty: Party, val secondCounterparty: Party) : FlowLogic<Int>() {
override val progressTracker = ProgressTracker()
#Suspendable
override fun call(): Int {
if (secondCounterparty == ourIdentity) {
throw FlowException("In Corda 3.x, you can't have two flow sessions with the same party.")
}
val flowSession = initiateFlow(firstCounterparty)
flowSession.send(secondCounterparty)
return flowSession.receive<Int>().unwrap { it }
}
}
#InitiatingFlow
#InitiatedBy(Initiator::class)
class Responder(val flowSession: FlowSession) : FlowLogic<Unit>() {
#Suspendable
override fun call() {
val secondCounterparty = flowSession.receive<Party>().unwrap { it }
if (secondCounterparty == flowSession.counterparty) {
throw FlowException("In Corda 3.x, you can't have two flow sessions with the same party.")
}
val newFlowSession = initiateFlow(secondCounterparty)
val int = newFlowSession.receive<Int>().unwrap { it }
flowSession.send(int)
}
}
#InitiatingFlow
#InitiatedBy(Responder::class)
class ResponderResponder(val flowSession: FlowSession) : FlowLogic<Unit>() {
#Suspendable
override fun call() {
flowSession.send(3)
}
}
Or you need to drop into an InitiatingFlow subflow in Responder before starting the flow that starts ResponderResponder, as follows:
#InitiatingFlow
#StartableByRPC
class Initiator(val firstCounterparty: Party, val secondCounterparty: Party) : FlowLogic<Int>() {
override val progressTracker = ProgressTracker()
#Suspendable
override fun call(): Int {
val flowSession = initiateFlow(firstCounterparty)
flowSession.send(secondCounterparty)
return flowSession.receive<Int>().unwrap { it }
}
}
#InitiatingFlow
#InitiatedBy(Initiator::class)
class Responder(val flowSession: FlowSession) : FlowLogic<Unit>() {
#Suspendable
override fun call() {
val secondCounterparty = flowSession.receive<Party>().unwrap { it }
val int = subFlow(ResponderInitiator(secondCounterparty))
flowSession.send(int)
}
}
#InitiatingFlow
class ResponderInitiator(val counterparty: Party) : FlowLogic<Int>() {
#Suspendable
override fun call(): Int {
val flowSession = initiateFlow(counterparty)
return flowSession.receive<Int>().unwrap { it }
}
}
#InitiatingFlow
#InitiatedBy(ResponderInitiator::class)
class ResponderResponder(val flowSession: FlowSession) : FlowLogic<Unit>() {
#Suspendable
override fun call() {
flowSession.send(3)
}
}

I am unable to use searchView with multiple fragments

In my application I have two fragments Fragment Card, Fragment Note. I am using ViewPager and TabLayout to sliding between two fragments. I want to implement a search function in my application.
This is my MainActivity class in Kotlin
class MainActivity : AppCompatActivity() {
private lateinit var viewPager:ViewPager
private lateinit var tabLayout:TabLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
//toolbar.title = "Cards"
initComponent()
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_main, menu)
val sv: SearchView = menu!!.findItem(R.id.action_bar_search).actionView as SearchView
val sm = getSystemService(Context.SEARCH_SERVICE) as SearchManager
sv.setSearchableInfo(sm.getSearchableInfo(componentName))
sv.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(p0: String?): Boolean {
//loadQuery("%"+p0+"%")
return false
}
override fun onQueryTextChange(p0: String?): Boolean {
//loadQuery("%"+p0+"%")
return false
}
})
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
if (item != null) {
when (item.itemId) {
R.id.sort -> {
Toast.makeText(context, "sort note", Toast.LENGTH_SHORT).show()
}
R.id.settings -> {
Toast.makeText(context, "settings note", Toast.LENGTH_SHORT).show()
}
}
}
return super.onOptionsItemSelected(item)
}
private fun initComponent() {
viewPager = findViewById(R.id.view_pager)
setupViewPager(viewPager)
tabLayout = findViewById(R.id.tab_layout)
tabLayout.setupWithViewPager(viewPager)
viewPager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
override fun onPageSelected(position: Int) {
changeFabIcon(position)
toolbar.title = tabLayout.getTabAt(position)!!.text.toString()
super.onPageSelected(position)
}
})
fabT.setOnClickListener {
//var text = ""
when (viewPager.currentItem) {
0 -> {
// open add new card page
startActivity(Intent(this, AddCardActivity::class.java))
//text = "Add Card"
}
1 -> {
// open add new note page
startActivity(Intent(this, AddNoteActivity::class.java))
//text = "Add note"
}
}
//Toast.makeText(applicationContext, text, Toast.LENGTH_SHORT).show()
}
}
private fun changeFabIcon(index: Int) {
fabT.hide()
Handler().postDelayed({
when(index) {
0 -> {
fabT.setImageResource(R.drawable.ic_card)
}
1 -> {
fabT.setImageResource(R.drawable.ic_note)
}
}
fabT.show()
}, 400)
}
private fun setupViewPager(view_pager: ViewPager) {
val mAdapter = ViewPagerAdapter(supportFragmentManager)
mAdapter.addFragment(FragmentCard(), "Cards")
mAdapter.addFragment(FragmentNote(), "Notes")
view_pager.adapter = mAdapter
}
internal inner class ViewPagerAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager) {
private val mFragmentList = ArrayList<Fragment>()
private val mFragmentTitleList = ArrayList<String>()
override fun getItem(position: Int): Fragment {
return mFragmentList[position]
}
override fun getCount(): Int {
return mFragmentList.size
}
fun addFragment(fragment: Fragment, title: String) {
mFragmentList.add(fragment)
mFragmentTitleList.add(title)
//fragment.arguments = args
}
override fun getPageTitle(position: Int): CharSequence? {
return mFragmentTitleList[position]
}
}
}
This is my FragmentNote class in Kotlin
class FragmentNote: Fragment() {
private var listNotes = ArrayList<Note> ()
private lateinit var mRecyclerViewNote: RecyclerView
private lateinit var mAdapterNote: RecyclerViewAdapterNote
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val v = inflater.inflate(R.layout.note_fragment, container, false)
mAdapterNote = RecyclerViewAdapterNote(context!!, listNotes, this)
mRecyclerViewNote = v.findViewById(R.id.noteRecView) as RecyclerView
mRecyclerViewNote.layoutManager = LinearLayoutManager(context)
mRecyclerViewNote.addItemDecoration(DividerItemDecoration(context, LinearLayoutManager.VERTICAL))
mRecyclerViewNote.adapter = mAdapterNote
setHasOptionsMenu(true)
NoteLoadQuery("%")
return v
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.menu_main, menu)
super.onCreateOptionsMenu(menu, inflater)
}
private fun NoteLoadQuery(title: String) {
var dbManager = DbManagerNote(context!!)
val projections = arrayOf("ID", "Title", "Description")
val selectionArgs = arrayOf(title)
// sort by title
val cursor = dbManager.Query(projections, "ID like ?", selectionArgs, "ID")
listNotes.clear()
// ascending
if (cursor.moveToLast()) {
do {
val ID = cursor.getInt(cursor.getColumnIndex("ID"))
val Title = cursor.getString(cursor.getColumnIndex("Title"))
val Description = cursor.getString(cursor.getColumnIndex("Description"))
listNotes.add(Note(ID, Title, Description))
} while (cursor.moveToPrevious())
}
mAdapterNote.notifyDataSetChanged()
}
}
and finally this is my FragmentCard class
class FragmentCard: Fragment() {
private var listCards = ArrayList<Card>()
private lateinit var myRecyclerViewCard: RecyclerView
private lateinit var mAdapter: RecyclerViewAdapterCard
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val v = inflater.inflate(R.layout.card_fragment, container, false)
mAdapter = RecyclerViewAdapterCard(context!!,listCards, this)
myRecyclerViewCard = v.findViewById(R.id.cardRecView) as RecyclerView
myRecyclerViewCard.layoutManager = LinearLayoutManager(context)
myRecyclerViewCard.addItemDecoration(DividerItemDecoration(context, LinearLayoutManager.VERTICAL))
myRecyclerViewCard.adapter = mAdapter
setHasOptionsMenu(true)
CardLoadQueryAscending("%")
return v
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.menu_main, menu)
super.onCreateOptionsMenu(menu, inflater)
}
private fun CardLoadQueryAscending(title: String) {
var dbManager = DbManagerCard(context!!)
val projections = arrayOf("ID", "CardName", "CardNum")
val selectionArgs = arrayOf(title)
// sort by title
val cursor = dbManager.Query(projections, "CardName like ?", selectionArgs, "CardName")
listCards.clear()
// ascending
if (cursor.moveToFirst()) {
do {
val ID = cursor.getInt(cursor.getColumnIndex("ID"))
val CardName = cursor.getString(cursor.getColumnIndex("CardName"))
val CardNum = cursor.getString(cursor.getColumnIndex("CardNum"))
listCards.add(Card(ID, CardName, CardNum))
} while (cursor.moveToNext())
}
mAdapter.notifyDataSetChanged()
}
}
I am trying to implement my search view in MainActivity, but I am unable to do. can anyone please give me your valuable suggestions to get my problem solve. thanks in advance.
I have done this, it is quite simple.
Just make your MainActivity own the SearchView. As you already did.
Then make a simple interface
ISearchChangedListener
onTextChanged(newString: String?)
onSearchSubmitted()
Setup your BaseFragment to implement this interface.
Override the implementation in your child fragments of the BaseFragment
Then keep track of mSelectedFragment on PageChanged.
Then you simply call
mSelectedFragment?.onTextChanged(newString)
whenever the parent gets a text changed and handle appropriately.

TornadoFX - Creating a MVP Design

Hello I'm new to using TornadoFX and I was wondering what the best design for a MVP structure would be using TornadoFX?
In MVP the view:
-> would delegate all events such as button clicking to a function in the presenter
-> does not interact with the model
Here are some of the rough prototype ideas:
abstract class AbstractPresenter<View : tornadofx.View> : Controller() {
var view: View by Delegates.notNull()
fun attachView(view: View) {
this.view = view;
}
}
I create a presenter which attaches itself to a AbstractView:
abstract class AbstractView<out Presenter : AbstractPresenter<*>> : View() {
abstract val presenter: Presenter
}
Now using it in a example:
class SampleTestView: AbstractView<SampleTestPresenter>() {
override val presenter: SampleTestPresenter by inject()
override val root: AnchorPane by fxml()
val testButton: Button by fxid()
init {
presenter.attachView(this)
testButton.setOnAction { presenter.doSomething() }
}
}
The Sample Presenter:
class SampleTestPresenter: AbstractPresenter<SampleWindowView>() {
fun doSomething() {
println("did it")
}
}
Is this a decent implementation of the MVP pattern using TornadoFX?
EDIT
Made some changes:
class SampleWindowView : View() {
override val root: AnchorPane by fxml()
val presenter : SampleWindowViewPresenter by inject()
val button:Button by fxid()
init {
button.setOnAction { presenter.handleButtonClick() }
}
}
class SampleWindowViewPresenter : Controller() {
val sampleView: SampleWindowView by inject()
fun handleButtonClick() {
println("clicked")
}
}
To sum up the discussion above, you can simply do:
class SampleTestView : View() {
val presenter: SampleTestPresenter by inject()
override val root: AnchorPane by fxml()
val testButton: Button by fxid()
init {
testButton.setOnAction { presenter.doSomething() }
}
}
class SampleTestPresenter : Controller() {
val view: SampleTestView by inject()
fun doSomething() {
println("Did the thing")
}
}
If you want to ensure the view has a presenter, you could create an abstract view and have all your views extend from it:
abstract class AbstractView<Presenter : Controller> : View() {
abstract val presenter: Presenter
}

Resources