Skip to content

Commit

Permalink
Merge pull request #3 from SamiraVaezi/mary_data_binding
Browse files Browse the repository at this point in the history
Mary data binding
  • Loading branch information
SamiraVaezi authored Sep 8, 2020
2 parents f3f1056 + 46ea323 commit c2cd8a7
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 20 deletions.
10 changes: 10 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
}

android {
Expand Down Expand Up @@ -30,10 +31,12 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}
dataBinding {
enabled = true
}
}

dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
Expand All @@ -42,4 +45,8 @@ dependencies {
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

kapt "com.android.databinding:compiler:$gradle_version"

implementation 'com.squareup.picasso:picasso:2.5.2'
}
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.angels.pooyeshgaran">

<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
14 changes: 13 additions & 1 deletion app/src/main/java/com/angels/pooyeshgaran/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@ package com.angels.pooyeshgaran

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import com.angels.pooyeshgaran.databinding.ActivityMainBinding
import com.angels.pooyeshgaran.databinding.UserModel

class MainActivity : AppCompatActivity() {

lateinit var mainDataBinding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

mainDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)

val model = UserModel()
model.userName = "sami"
mainDataBinding.userModel = model
mainDataBinding.testUrl = "https://i.redd.it/g5acbfi5hkm01.jpg";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.angels.pooyeshgaran.databinding;

import android.content.Context;
import android.widget.ImageView;
import androidx.databinding.BindingAdapter;
import com.angels.pooyeshgaran.R;
import com.squareup.picasso.Picasso;

public class PicassoBindingAdapters {

@BindingAdapter("imageResource")
public static void setImageResource(ImageView view, String imageUrl){

Context context = view.getContext();

Picasso.with(context)
.load(imageUrl)
.placeholder(R.drawable.ic_launcher_background)
.error(R.drawable.ic_launcher_background)
.into(view);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.angels.pooyeshgaran.databinding

class UserModel {

lateinit var userName: String
lateinit var userImage: String
}
59 changes: 44 additions & 15 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<layout >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<data>
<variable
name="userModel"
type="com.angels.pooyeshgaran.databinding.UserModel" />
<variable
name="testUrl"
type="String"/>
</data>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageView
android:id="@+id/userImage"
android:layout_width="0dp"
android:layout_height="0dp"
app:imageResource="@{testUrl}"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/tvUserName"/>

<TextView
android:id="@+id/tvUserName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAlignment="center"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/userImage"
app:layout_constraintBottom_toBottomOf="parent"
android:text="@{userModel.userName}"/>

</androidx.constraintlayout.widget.ConstraintLayout>

</layout>

</androidx.constraintlayout.widget.ConstraintLayout>
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.4.0"
ext.kotlin_version = "1.3.31"
ext.gradle_version = "3.4.1"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.0-beta03"
classpath "com.android.tools.build:gradle:$gradle_version"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip

0 comments on commit c2cd8a7

Please sign in to comment.