Skip to content

Commit

Permalink
Merge pull request Tribler#12 from Tribler/view-binding
Browse files Browse the repository at this point in the history
Update Gradle, enable view binding
  • Loading branch information
MattSkala authored Feb 25, 2020
2 parents d3b4f3d + 60acaa1 commit b81e4de
Show file tree
Hide file tree
Showing 20 changed files with 171 additions and 117 deletions.
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ buildscript {
ext.ktlint_version = '0.36.0'
ext.ktlint_gradle_version = '9.1.1'
ext.sqldelight_version = '1.2.2'
ext.nav_version = "2.2.0"
ext.nav_version = "2.2.1"
ext.fragment_version = "1.2.2"
ext.lifecycle_version = "2.2.0"
ext.dokka_version = "0.10.1"
repositories {
google()
jcenter()
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.android.tools.build:gradle:3.6.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jlleitschuh.gradle:ktlint-gradle:$ktlint_gradle_version"
classpath "com.squareup.sqldelight:gradle-plugin:$sqldelight_version"
Expand Down
28 changes: 23 additions & 5 deletions demo-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "androidx.navigation.safeargs"
apply plugin: 'org.jlleitschuh.gradle.ktlint'

ktlint {
version = "$ktlint_version"
android = true
outputToConsole = true
ignoreFailures = true
}

android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
Expand Down Expand Up @@ -33,34 +42,43 @@ android {
jvmTarget = "1.8"
}

viewBinding {
enabled = true
}
}

dependencies {
def fragment_version = "1.2.0"

implementation project(':ipv8-android')

// AndroidX
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "androidx.recyclerview:recyclerview:1.1.0"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
implementation "androidx.fragment:fragment-ktx:$fragment_version"
implementation "androidx.preference:preference:1.1.0"
implementation 'com.google.android.material:material:1.0.0'
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"

// Material
implementation 'com.google.android.material:material:1.1.0'

// Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3'
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

implementation 'com.mattskala:itemadapter:0.2'
// Logging
implementation 'io.github.microutils:kotlin-logging:1.7.7'
implementation 'com.github.tony19:logback-android:2.0.0'

implementation 'com.mattskala:itemadapter:0.2'

// Testing
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.app.Application
import android.util.Log
import androidx.preference.PreferenceManager
import com.squareup.sqldelight.android.AndroidSqliteDriver
import com.squareup.sqldelight.db.SqlDriver
import nl.tudelft.ipv8.*
import nl.tudelft.ipv8.android.IPv8Android
import nl.tudelft.ipv8.android.demo.service.DemoService
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package nl.tudelft.ipv8.android.demo.ui

import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment
import nl.tudelft.ipv8.IPv8
import nl.tudelft.ipv8.android.IPv8Android
Expand All @@ -8,7 +9,7 @@ import nl.tudelft.ipv8.android.demo.DemoCommunity
import nl.tudelft.ipv8.android.demo.TrustChainHelper
import nl.tudelft.ipv8.attestation.trustchain.TrustChainCommunity

abstract class BaseFragment : Fragment() {
abstract class BaseFragment(@LayoutRes contentLayoutId: Int = 0) : Fragment(contentLayoutId) {
protected val trustchain: TrustChainHelper by lazy {
TrustChainHelper(getTrustChainCommunity())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ data class BlockItem(
var isExpanded: Boolean = false,
val canSign: Boolean = false,
val status: BlockStatus? = null
): Item() {
) : Item() {
override fun areItemsTheSame(other: Item): Boolean {
return other is BlockItem && other.block.blockId == block.blockId
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@ import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.mattskala.itemadapter.Item
import com.mattskala.itemadapter.ItemAdapter
import kotlinx.android.synthetic.main.fragment_blocks.*
import kotlinx.android.synthetic.main.fragment_peers.recyclerView
import kotlinx.coroutines.*
import nl.tudelft.ipv8.android.demo.R
import nl.tudelft.ipv8.android.demo.databinding.FragmentBlocksBinding
import nl.tudelft.ipv8.android.demo.ui.BaseFragment
import nl.tudelft.ipv8.android.demo.util.viewBinding
import nl.tudelft.ipv8.attestation.trustchain.ANY_COUNTERPARTY_PK
import nl.tudelft.ipv8.attestation.trustchain.TrustChainBlock
import nl.tudelft.ipv8.attestation.trustchain.UNKNOWN_SEQ
import nl.tudelft.ipv8.util.hexToBytes


@UseExperimental(ExperimentalUnsignedTypes::class)
open class BlocksFragment : BaseFragment() {
open class BlocksFragment : BaseFragment(R.layout.fragment_blocks) {
private val adapter = ItemAdapter()

private lateinit var publicKey: ByteArray
Expand All @@ -35,6 +34,8 @@ open class BlocksFragment : BaseFragment() {

protected open val isNewBlockAllowed = true

protected val binding: FragmentBlocksBinding by viewBinding(FragmentBlocksBinding::bind)

init {
lifecycleScope.launchWhenStarted {
while (isActive) {
Expand Down Expand Up @@ -74,23 +75,15 @@ open class BlocksFragment : BaseFragment() {
publicKey = getPublicKey()
}

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

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(context)
binding.recyclerView.adapter = adapter
binding.recyclerView.layoutManager = LinearLayoutManager(context)
val dividerItemDecoration = DividerItemDecoration(context, LinearLayout.VERTICAL)
dividerItemDecoration.setDrawable(ResourcesCompat.getDrawable(resources,
R.drawable.list_divider, null)!!)
recyclerView.addItemDecoration(dividerItemDecoration)
binding.recyclerView.addItemDecoration(dividerItemDecoration)
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
Expand All @@ -103,14 +96,14 @@ open class BlocksFragment : BaseFragment() {
return when (item.itemId) {
R.id.item_new_block -> {
showNewBlockDialog()
true
true
}
else -> super.onOptionsItemSelected(item)
}
}

protected open fun getPublicKey(): ByteArray {
val args = BlocksFragmentArgs.fromBundle(arguments!!)
val args = BlocksFragmentArgs.fromBundle(requireArguments())
return args.publicKey.hexToBytes()
}

Expand All @@ -125,8 +118,8 @@ open class BlocksFragment : BaseFragment() {
protected open suspend fun updateView() {
val items = createItems(blocks)
adapter.updateItems(items)
imgNoBlocks.isVisible = items.isEmpty()
progress.isVisible = false
binding.imgNoBlocks.isVisible = items.isEmpty()
binding.progress.isVisible = false
}

private suspend fun createItems(blocks: List<TrustChainBlock>): List<Item> =
Expand Down Expand Up @@ -170,7 +163,7 @@ open class BlocksFragment : BaseFragment() {
lifecycleScope.launch {
refreshBlocks()
updateView()
recyclerView.smoothScrollToPosition(0)
binding.recyclerView.smoothScrollToPosition(0)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nl.tudelft.ipv8.android.demo.ui.blocks

import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.fragment_blocks.*
import nl.tudelft.ipv8.attestation.trustchain.TrustChainBlock
import kotlin.math.min

Expand All @@ -19,13 +18,13 @@ class LatestBlocksFragment : BlocksFragment() {
}

override suspend fun updateView() {
val layoutManager = recyclerView.layoutManager as LinearLayoutManager
val layoutManager = binding.recyclerView.layoutManager as LinearLayoutManager
val firstVisibleItem = layoutManager.findFirstCompletelyVisibleItemPosition()

super.updateView()

if (firstVisibleItem == 0) {
recyclerView.smoothScrollToPosition(0)
binding.recyclerView.smoothScrollToPosition(0)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
package nl.tudelft.ipv8.android.demo.ui.debug

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import kotlinx.android.synthetic.main.fragment_debug.*
import kotlinx.coroutines.*
import nl.tudelft.ipv8.Community
import nl.tudelft.ipv8.android.demo.R
import nl.tudelft.ipv8.android.demo.databinding.FragmentDebugBinding
import nl.tudelft.ipv8.android.demo.ui.BaseFragment
import nl.tudelft.ipv8.android.demo.util.viewBinding
import nl.tudelft.ipv8.util.toHex

class DebugFragment : BaseFragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_debug, container, false)
}
class DebugFragment : BaseFragment(R.layout.fragment_debug) {
private val binding by viewBinding(FragmentDebugBinding::bind)

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Expand All @@ -35,27 +28,27 @@ class DebugFragment : BaseFragment() {
private fun updateView() {
val ipv8 = getIpv8()
val demo = getDemoCommunity()
txtBootstrap.text = Community.DEFAULT_ADDRESSES.joinToString("\n")
txtLanAddress.text = demo.myEstimatedLan.toString()
txtWanAddress.text = demo.myEstimatedWan.toString()
txtPeerId.text = ipv8.myPeer.mid
txtPublicKey.text = ipv8.myPeer.publicKey.keyToBin().toHex()
txtOverlays.text = ipv8.overlays.values.toList().joinToString("\n") {
binding.txtBootstrap.text = Community.DEFAULT_ADDRESSES.joinToString("\n")
binding.txtLanAddress.text = demo.myEstimatedLan.toString()
binding.txtWanAddress.text = demo.myEstimatedWan.toString()
binding.txtPeerId.text = ipv8.myPeer.mid
binding.txtPublicKey.text = ipv8.myPeer.publicKey.keyToBin().toHex()
binding.txtOverlays.text = ipv8.overlays.values.toList().joinToString("\n") {
it.javaClass.simpleName + " (" + it.getPeers().size + " peers)"
}

lifecycleScope.launch {
val blockCount = withContext(Dispatchers.IO) {
getTrustChainCommunity().database.getAllBlocks().size
}
txtBlockCount.text = blockCount.toString()
binding.txtBlockCount.text = blockCount.toString()
}

lifecycleScope.launch {
val chainLength = withContext(Dispatchers.IO) {
getTrustChainCommunity().getChainLength()
}
txtChainLength.text = chainLength.toString()
binding.txtChainLength.text = chainLength.toString()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class AddressItem(
val address: Address,
val discovered: Date?,
val contacted: Date?
): Item() {
) : Item() {
override fun areItemsTheSame(other: Item): Boolean {
return other is AddressItem && other.address == address
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package nl.tudelft.ipv8.android.demo.ui.peers

import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import mu.KotlinLogging
import nl.tudelft.ipv8.android.demo.R
import nl.tudelft.ipv8.android.demo.databinding.ActivityMainBinding
import nl.tudelft.ipv8.android.demo.util.viewBinding

val logger = KotlinLogging.logger {}

class MainActivity : AppCompatActivity() {
private val binding by viewBinding(ActivityMainBinding::inflate)

private val navController by lazy {
findNavController(R.id.navHostFragment)
}
Expand All @@ -35,13 +34,13 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)
setContentView(binding.root)

// Setup ActionBar
setupActionBarWithNavController(navController, appBarConfiguration)

// Setup bottom navigation
bottomNavigation.setupWithNavController(navController)
binding.bottomNavigation.setupWithNavController(navController)

lifecycleScope.launch {
while (isActive) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package nl.tudelft.ipv8.android.demo.ui.peers
import com.mattskala.itemadapter.Item
import nl.tudelft.ipv8.Peer

class PeerItem(val peer: Peer): Item() {
class PeerItem(val peer: Peer) : Item() {
override fun areItemsTheSame(other: Item): Boolean {
return other is PeerItem && other.peer.mid == peer.mid
}
Expand Down
Loading

0 comments on commit b81e4de

Please sign in to comment.