Skip to content

Commit

Permalink
Merge pull request #5 from jedlix/feature/1.4.0
Browse files Browse the repository at this point in the history
Version 1.4.0
  • Loading branch information
Daeda88 authored Sep 2, 2022
2 parents 66e11bf + addb407 commit 42f386d
Show file tree
Hide file tree
Showing 31 changed files with 705 additions and 185 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Add the following to your `build.gradle`:

```groovy
dependencies {
implementation("com.jedlix:sdk:1.2.0")
implementation("com.jedlix:sdk:1.4.0")
}
```

Expand Down
2 changes: 2 additions & 0 deletions example/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id("com.android.application")
kotlin("android")
kotlin("kapt")
id("com.github.triplet.play") version "3.7.0"
}

Expand Down Expand Up @@ -46,6 +47,7 @@ android {
}

dataBinding {
isEnabled = true
addKtx = true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.jedlix.sdk.connectSession.registerConnectSessionManager
import com.jedlix.sdk.example.adapter.ButtonListAdapter
import com.jedlix.sdk.example.adapter.ConnectSessionViewModelAdapter
import com.jedlix.sdk.example.adapter.GroupedConnectSessionViewModelAdapter
import com.jedlix.sdk.example.databinding.ActivityConnectionsBinding
import com.jedlix.sdk.example.viewModel.ConnectionsViewModel
import kotlinx.coroutines.flow.collect
Expand All @@ -47,6 +50,9 @@ class ConnectionsActivity : AppCompatActivity() {
binding.viewModel = viewModel
binding.lifecycleOwner = this

binding.vehicles.adapter = ConnectSessionViewModelAdapter(this)
binding.chargers.adapter = GroupedConnectSessionViewModelAdapter(this)

val connectSessionManager = registerConnectSessionManager {
viewModel.reloadData()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2022 Jedlix B.V. The Netherlands
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.jedlix.sdk.example.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.RecyclerView
import com.jedlix.sdk.example.databinding.ViewButtonRowBinding
import com.jedlix.sdk.example.model.Button

class ButtonListAdapter : RecyclerView.Adapter<ButtonListAdapter.ButtonViewHolder>() {

companion object {
@BindingAdapter("modelButtons")
@JvmStatic
fun bindModelButtons(recyclerView: RecyclerView, buttons: List<Button>?) {
(recyclerView.adapter as? ButtonListAdapter)?.buttons = buttons ?: emptyList()
}
}

class ButtonViewHolder(val binding: ViewButtonRowBinding) : RecyclerView.ViewHolder(binding.root)

private var buttons: List<Button> = emptyList()
@SuppressLint("NotifyDataSetChanged")
set(value) {
field = value
notifyDataSetChanged()
}

override fun getItemCount(): Int = buttons.size

override fun onBindViewHolder(holder: ButtonViewHolder, position: Int) {
holder.binding.button.apply {
val modelButton = buttons.getOrNull(position)
text = modelButton?.title
setOnClickListener { modelButton?.action?.invoke() }
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ButtonViewHolder(
ViewButtonRowBinding.inflate(LayoutInflater.from(parent.context), parent, false)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2022 Jedlix B.V. The Netherlands
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.jedlix.sdk.example.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.BindingAdapter
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.RecyclerView
import com.jedlix.sdk.example.databinding.ViewConnectSessionViewModelRowBinding
import com.jedlix.sdk.example.viewModel.ConnectSessionViewModel

class ConnectSessionViewModelAdapter(val lifecycleOwner: LifecycleOwner) : RecyclerView.Adapter<ConnectSessionViewModelAdapter.ViewHolder>() {

companion object {
@BindingAdapter("connectSessionViewModels")
@JvmStatic
fun bindModelButtons(recyclerView: RecyclerView, connectSessionViewModels: List<ConnectSessionViewModel>?) {
(recyclerView.adapter as? ConnectSessionViewModelAdapter)?.connectSessionViewModels = connectSessionViewModels ?: emptyList()
}
}

class ViewHolder(val binding: ViewConnectSessionViewModelRowBinding) : RecyclerView.ViewHolder(binding.root)

private var connectSessionViewModels: List<ConnectSessionViewModel> = emptyList()
@SuppressLint("NotifyDataSetChanged")
set(value) {
field = value
notifyDataSetChanged()
}

override fun getItemCount(): Int = connectSessionViewModels.size

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.binding.apply {
viewModel = connectSessionViewModels[position]
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder(
ViewConnectSessionViewModelRowBinding.inflate(LayoutInflater.from(parent.context), parent, false).apply {
lifecycleOwner = this@ConnectSessionViewModelAdapter.lifecycleOwner
buttons.adapter = ButtonListAdapter()
}
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2022 Jedlix B.V. The Netherlands
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.jedlix.sdk.example.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.BindingAdapter
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.RecyclerView
import com.jedlix.sdk.example.databinding.ViewConnectSessionViewModelRowBinding
import com.jedlix.sdk.example.databinding.ViewGroupedConnectSessionViewModelRowBinding
import com.jedlix.sdk.example.viewModel.GroupedConnectSessionViewModel

class GroupedConnectSessionViewModelAdapter(private val lifecycleOwner: LifecycleOwner) : RecyclerView.Adapter<GroupedConnectSessionViewModelAdapter.ViewHolder>() {

companion object {
@BindingAdapter("groupedConnectSessionViewModels")
@JvmStatic
fun bindModelButtons(recyclerView: RecyclerView, connectSessionViewModels: List<GroupedConnectSessionViewModel>?) {
(recyclerView.adapter as? GroupedConnectSessionViewModelAdapter)?.groupedConnectSessionViewModels = connectSessionViewModels ?: emptyList()
}
}

class ViewHolder(val binding: ViewGroupedConnectSessionViewModelRowBinding) : RecyclerView.ViewHolder(binding.root)

private var groupedConnectSessionViewModels: List<GroupedConnectSessionViewModel> = emptyList()
@SuppressLint("NotifyDataSetChanged")
set(value) {
field = value
notifyDataSetChanged()
}

override fun getItemCount(): Int = groupedConnectSessionViewModels.size

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.binding.apply {
viewModel = groupedConnectSessionViewModels[position]

}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder(
ViewGroupedConnectSessionViewModelRowBinding.inflate(LayoutInflater.from(parent.context), parent, false).apply {
lifecycleOwner = this@GroupedConnectSessionViewModelAdapter.lifecycleOwner
connectSessions.adapter = ConnectSessionViewModelAdapter(this@GroupedConnectSessionViewModelAdapter.lifecycleOwner)
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

package com.jedlix.sdk.model

import kotlinx.serialization.Serializable
package com.jedlix.sdk.example.model

data class Button(val title: String, val action: () -> Unit)
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2022 Jedlix B.V. The Netherlands
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.jedlix.sdk.example.viewModel

import com.jedlix.sdk.example.model.Button
import com.jedlix.sdk.model.Charger
import com.jedlix.sdk.model.ChargerConnectSession

class ChargerViewModel(
charger: Charger,
connectSession: ChargerConnectSession?,
private val resumeConnectSession: (ChargerConnectSession) -> Unit,
private val removeCharger: (Charger) -> Unit
) : ConnectSessionViewModel {

override val title = charger.detail.fullName
override val buttons = listOfNotNull(
connectSession?.let { Button("Resume") { resumeConnectSession(it) } },
Button("Remove") { removeCharger(charger) }
)

private val Charger.Detail.fullName: String get() = listOfNotNull(brand, model).joinToString(" ")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2022 Jedlix B.V. The Netherlands
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.jedlix.sdk.example.viewModel

import com.jedlix.sdk.example.model.Button
import com.jedlix.sdk.model.Charger
import com.jedlix.sdk.model.ChargerConnectSession
import com.jedlix.sdk.model.ChargingLocation

class ChargingLocationViewModel(
private val chargingLocation: ChargingLocation,
chargers: List<Charger>,
private val chargerSessions: List<ChargerConnectSession>,
private val startChargerConnectSession: (ChargingLocation) -> Unit,
private val resumeConnectSession: (ChargerConnectSession) -> Unit,
private val removeCharger: (Charger) -> Unit
) : GroupedConnectSessionViewModel {

override val title = chargingLocation.address.let { listOfNotNull(it.street, it.houseNumber, it.city).joinToString(", ") }
override val connectSessionViewModels = chargers.map { charger ->
ChargerViewModel(charger, chargerSessions.firstOrNull { it.chargerId == charger.id }, resumeConnectSession, removeCharger)
}.ifEmpty {
val connectSessionWithoutVehicle = chargerSessions.firstOrNull { it.chargingLocationId == chargingLocation.id && it.chargerId.isNullOrEmpty() }
listOf(
object : ConnectSessionViewModel {
override val title: String = if (connectSessionWithoutVehicle != null) "Unfinished connect session" else "No Chargers found"
override val buttons: List<Button> = listOf(
connectSessionWithoutVehicle?.let { Button("Resume") { resumeConnectSession(it) } } ?: Button("Connect") { startChargerConnectSession(chargingLocation) }
)
}
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2022 Jedlix B.V. The Netherlands
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.jedlix.sdk.example.viewModel

import com.jedlix.sdk.example.model.Button

interface ConnectSessionViewModel {
val title: String
val buttons: List<Button>
}

interface GroupedConnectSessionViewModel {
val title: String
val connectSessionViewModels: List<ConnectSessionViewModel>
}
Loading

0 comments on commit 42f386d

Please sign in to comment.