Skip to content

Commit

Permalink
chore: Reorganized gradle setup to separate versions. Added architect…
Browse files Browse the repository at this point in the history
…ure components and ktx libraries.

feat: Initial ViewModel setup for main screen. Initial data model setup. Main list adapter setup to use data model.
  • Loading branch information
baruckis committed May 19, 2018
1 parent 1e6dc34 commit 622cf98
Show file tree
Hide file tree
Showing 15 changed files with 245 additions and 69 deletions.
6 changes: 6 additions & 0 deletions .idea/copyright/Apache_V2.xml

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

3 changes: 3 additions & 0 deletions .idea/copyright/profiles_settings.xml

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

56 changes: 39 additions & 17 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
*/

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

// Version values at Project level versions.gradle.

android {
compileSdkVersion 27
compileSdkVersion versions.compile_sdk
defaultConfig {
applicationId "com.baruckis.mycryptocoins"
minSdkVersion 23
targetSdkVersion 27
minSdkVersion versions.min_sdk
targetSdkVersion versions.target_sdk
versionCode 1
versionName "@string/app_version"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand All @@ -40,21 +40,43 @@ android {

dependencies {

// Version values at Project level build.gradle.

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

// Android
implementation "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
implementation "com.android.support:cardview-v7:$rootProject.supportLibraryVersion"
implementation "com.android.support:design:$rootProject.supportLibraryVersion"
implementation "com.android.support.constraint:constraint-layout:$rootProject.constraintLayoutVersion"

testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
// Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$versions.kotlin"


/* Android */

// Support libraries
implementation "com.android.support:appcompat-v7:$versions.support"
implementation "com.android.support:cardview-v7:$versions.support"
implementation "com.android.support:design:$versions.support"

// Constraint layout
implementation "com.android.support.constraint:constraint-layout:$versions.constraint_layout"

// Android KTX
implementation "androidx.core:core-ktx:$versions.core"

// Architecture components
// LiveData + ViewModel
implementation "android.arch.lifecycle:extensions:$versions.lifecycle"

// Android Testing Support Library's runner and rules
androidTestImplementation "com.android.support.test:runner:$versions.atsl_runner"

// Espresso UI Testing
androidTestImplementation "com.android.support.test.espresso:espresso-core:$versions.espresso"


/* Other */

// Dependencies for local unit tests
testImplementation "junit:junit:$versions.junit"

// 3rd party
implementation "eu.davidea:flipview:$rootProject.flipViewVersion"
// UI
implementation "eu.davidea:flipview:$versions.flip_view"
}
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
~ 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.
-->
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.baruckis.mycryptocoins">

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2018 Andrius Baruckis www.baruckis.com | mycryptocoins.baruckis.com
*
* 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.baruckis.mycryptocoins.data

data class Cryptocurrency(val name: String,
val rank: Short,
val amount: Double,
val symbol: String,
val price: Double,
val amountFiat: Double,
val pricePercentChange1h: Double,
val pricePercentChange7d: Double,
val pricePercentChange24h: Double,
val amountFiatChange24h: Double)
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.baruckis.mycryptocoins.mainlist

import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
Expand All @@ -28,11 +30,13 @@ import com.baruckis.mycryptocoins.R
/**
* A placeholder fragment containing a simple view.
*/
class MainActivityListFragment : Fragment() {
class MainListFragment : Fragment() {

private lateinit var recyclerView: RecyclerView
private lateinit var recyclerAdapter: MainRecyclerViewAdapter

private lateinit var viewModel: MainViewModel

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val v: View = inflater.inflate(R.layout.fragment_main_list, container, false)
Expand All @@ -47,33 +51,22 @@ class MainActivityListFragment : Fragment() {
super.onActivityCreated(savedInstanceState)

setupList()
}

private fun setupList() {
// Obtain ViewModel from ViewModelProviders, using this fragment as LifecycleOwner.
viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)

val data = ArrayList<String>()
data.add("Bitcoin")
data.add("Etherium")
data.add("Ripple")
data.add("Bitcoin Cash")
data.add("Litecoin")
data.add("NEO")
data.add("Stellar")
data.add("EOS")
data.add("Cardano")
data.add("Stellar")
data.add("IOTA")
data.add("Dash")
data.add("Monero")
data.add("TRON")
data.add("NEM")
data.add("ICON")
data.add("Bitcoin Gold")
data.add("Zcash")
data.add("Verge")
// Observe data on the ViewModel, exposed as a LiveData
viewModel.data.observe(this, Observer { data ->
// Set the data exposed by the LiveData
if (data != null) {
recyclerAdapter.setData(data)
}
})
}

private fun setupList() {
recyclerView.layoutManager = LinearLayoutManager(activity)
recyclerAdapter = MainRecyclerViewAdapter(data)
recyclerAdapter = MainRecyclerViewAdapter()
recyclerView.adapter = recyclerAdapter
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,86 @@

package com.baruckis.mycryptocoins.mainlist

import android.content.Context
import android.support.v4.content.ContextCompat
import android.support.v7.widget.RecyclerView
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.text.plusAssign
import androidx.core.text.toSpannable
import com.baruckis.mycryptocoins.R
import com.baruckis.mycryptocoins.data.Cryptocurrency


class MainRecyclerViewAdapter(val dataList: ArrayList<String>) : RecyclerView.Adapter<MainRecyclerViewAdapter.CustomViewHolder>() {
class MainRecyclerViewAdapter() : RecyclerView.Adapter<MainRecyclerViewAdapter.CustomViewHolder>() {

private lateinit var dataList: ArrayList<Cryptocurrency>

fun setData(newDataList: ArrayList<Cryptocurrency>) {
dataList = newDataList
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.fragment_main_list_item, parent, false)
return CustomViewHolder(v)
}

override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
holder.txtName?.text = dataList[position]
holder.txtName?.text = dataList[position].name
holder.txtRanking?.text = String.format("${dataList[position].rank}")
holder.txtAmountAndSymbol?.text = String.format("${dataList[position].amount} ${dataList[position].symbol}")
holder.txtPrice?.text = String.format("${dataList[position].price} ${holder.context.getString(R.string.pref_default_fiat_currency_value)}")
holder.txtAmountFiat?.text = String.format("${dataList[position].amountFiat} ${holder.context.getString(R.string.pref_default_fiat_currency_value)}")
holder.txtPricePercentChange1hAnd7d?.text =
SpannableStringBuilder(getChangeValueStyled(dataList[position].pricePercentChange1h, holder.context))
.append(holder.context.getString(R.string.string_column_coin_separator_change))
.append(getChangeValueStyled(dataList[position].pricePercentChange7d, holder.context))
holder.txtPricePercentChange24h?.text = getChangeValueStyled(dataList[position].pricePercentChange24h, holder.context)
holder.txtAmountFiatChange24h?.text = getChangeValueStyled(dataList[position].amountFiatChange24h, holder.context, true)
}

override fun getItemCount(): Int {
return dataList.size
}

private fun getChangeValueStyled(value:Double, context:Context, isFiat:Boolean = false): Spannable {
val valueString:String = String.format("$value").plus(if (isFiat) " ${context.getString(R.string.pref_default_fiat_currency_value)}" else "%")
val valueSpannable:Spannable

when {
value > 0 -> {
valueSpannable = "+$valueString".toSpannable()
valueSpannable.plusAssign(ForegroundColorSpan(ContextCompat.getColor(context, R.color.colorForValueChangePositive)))
}
value < 0 -> {
valueSpannable = valueString.toSpannable()
valueSpannable.plusAssign(ForegroundColorSpan(ContextCompat.getColor(context, R.color.colorForValueChangeNegative)))
}
else -> {
valueSpannable = valueString.toSpannable()
valueSpannable.plusAssign(ForegroundColorSpan(ContextCompat.getColor(context, R.color.colorForMainListItemText)))
}
}
return valueSpannable
}


inner class CustomViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

val context:Context = itemView.context

val txtName = itemView.findViewById<TextView>(R.id.item_name)
val txtRanking = itemView.findViewById<TextView>(R.id.item_ranking)
val txtAmountAndSymbol = itemView.findViewById<TextView>(R.id.item_amount_symbol)
val txtPrice = itemView.findViewById<TextView>(R.id.item_price)
val txtAmountFiat = itemView.findViewById<TextView>(R.id.item_amount_fiat)
val txtPricePercentChange1hAnd7d = itemView.findViewById<TextView>(R.id.item_price_percent_change_1h_7d)
val txtPricePercentChange24h = itemView.findViewById<TextView>(R.id.item_price_percent_change_24h)
val txtAmountFiatChange24h = itemView.findViewById<TextView>(R.id.item_amount_fiat_change_24h)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2018 Andrius Baruckis www.baruckis.com | mycryptocoins.baruckis.com
*
* 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.baruckis.mycryptocoins.mainlist

import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MutableLiveData
import android.arch.lifecycle.ViewModel
import com.baruckis.mycryptocoins.data.Cryptocurrency

class MainViewModel : ViewModel() {

private val liveData = MutableLiveData<ArrayList<Cryptocurrency>>()
val data: LiveData<ArrayList<Cryptocurrency>>
get() = liveData

init {
val tempData = ArrayList<Cryptocurrency>()

val btc:Cryptocurrency = Cryptocurrency("Bitcoin", 1, 0.56822348, "BTC", 8328.77, 4732.60, 0.19, -10.60, 0.44, 20.82)
val eth:Cryptocurrency = Cryptocurrency("Etherium", 2, 6.0, "ETH", 702.99, 4217.94, 0.13, -7.38, 0.79, 33.32)

tempData.add(btc)
tempData.add(eth)

liveData.value = tempData
}
}
3 changes: 1 addition & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@
android:id="@+id/textview_total_value_change_24h"
style="@style/AutoSizeText"
android:layout_width="wrap_content"
android:background="@color/colorForValueChangePositive"
android:gravity="center"
android:text="@string/sample_text_total_value_change_24h"
android:text="@string/string_total_value_change_24h"
android:textAlignment="center"
android:textColor="@color/colorWhite"
android:textDirection="firstStrongLtr"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment"
android:name="com.baruckis.mycryptocoins.mainlist.MainActivityListFragment"
android:name="com.baruckis.mycryptocoins.mainlist.MainListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_main_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.baruckis.mycryptocoins.mainlist.MainActivityListFragment"
tools:context="com.baruckis.mycryptocoins.mainlist.MainListFragment"
tools:showIn="@layout/activity_main">

<android.support.v7.widget.CardView
Expand Down
Loading

0 comments on commit 622cf98

Please sign in to comment.