Skip to content

Commit 376d0a2

Browse files
committed
Convert initial lab1 to kotlin
1 parent 9e67eb9 commit 376d0a2

23 files changed

+299
-344
lines changed

app/build.gradle

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android'
23

34
android {
4-
compileSdkVersion 29
5+
compileSdkVersion 30
56
buildToolsVersion "29.0.3"
67

78
defaultConfig {
89
applicationId "com.codepath.uiandgitlab"
910
minSdkVersion 19
10-
targetSdkVersion 29
11+
targetSdkVersion 30
1112
versionCode 1
1213
versionName "1.0"
1314

@@ -20,16 +21,24 @@ android {
2021
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
2122
}
2223
}
23-
24+
compileOptions {
25+
sourceCompatibility JavaVersion.VERSION_1_8
26+
targetCompatibility JavaVersion.VERSION_1_8
27+
}
2428
}
2529

2630
dependencies {
2731
implementation fileTree(dir: 'libs', include: ['*.jar'])
2832

29-
implementation 'androidx.appcompat:appcompat:1.1.0'
30-
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
31-
implementation 'com.squareup.retrofit2:retrofit:2.1.0'
32-
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
33+
implementation 'androidx.appcompat:appcompat:1.2.0'
34+
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
35+
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
36+
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
3337
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
34-
implementation 'androidx.recyclerview:recyclerview:1.1.0'
38+
implementation 'androidx.recyclerview:recyclerview:1.2.0'
39+
implementation "androidx.core:core-ktx:1.3.2"
40+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
41+
}
42+
repositories {
43+
mavenCentral()
3544
}

app/src/main/java/com/codepath/bestsellerlistapp/BestSellerBooksFragment.java

Lines changed: 0 additions & 93 deletions
This file was deleted.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.codepath.bestsellerlistapp
2+
3+
import android.os.Bundle
4+
import android.util.Log
5+
import android.view.LayoutInflater
6+
import android.view.View
7+
import android.view.ViewGroup
8+
import android.widget.Toast
9+
import androidx.core.widget.ContentLoadingProgressBar
10+
import androidx.fragment.app.Fragment
11+
import androidx.recyclerview.widget.GridLayoutManager
12+
import androidx.recyclerview.widget.RecyclerView
13+
import com.codepath.bestsellerlistapp.R.layout
14+
import com.codepath.bestsellerlistapp.models.BestSellerBook
15+
import com.codepath.bestsellerlistapp.networking.CallbackResponse
16+
import com.codepath.bestsellerlistapp.networking.NYTimesApiClient
17+
18+
/**
19+
* A fragment representing a list of Items.
20+
*/
21+
class BestSellerBooksFragment : Fragment(), OnListFragmentInteractionListener {
22+
override fun onCreateView(
23+
inflater: LayoutInflater, container: ViewGroup?,
24+
savedInstanceState: Bundle?
25+
): View? {
26+
val view = inflater.inflate(layout.fragment_best_seller_books_list, container, false)
27+
val progressBar = view.findViewById<View>(R.id.progress) as ContentLoadingProgressBar
28+
val recyclerView = view.findViewById<View>(R.id.list) as RecyclerView
29+
val context = view.context
30+
recyclerView.layoutManager = GridLayoutManager(context, 2)
31+
updateAdapter(progressBar, recyclerView)
32+
return view
33+
}
34+
35+
private fun updateAdapter(progressBar: ContentLoadingProgressBar, recyclerView: RecyclerView) {
36+
progressBar.show()
37+
val nyTimesApiClient = NYTimesApiClient()
38+
nyTimesApiClient.getBestSellersList(object : CallbackResponse<List<BestSellerBook>> {
39+
override fun onSuccess(models: List<BestSellerBook>) {
40+
progressBar.hide()
41+
recyclerView.adapter = BestSellerBooksRecyclerViewAdapter(models, this@BestSellerBooksFragment)
42+
Log.d("BestSellerBooksFragment", "response successful")
43+
}
44+
45+
override fun onFailure(error: Throwable?) {
46+
progressBar.hide()
47+
error?.message?.let { message ->
48+
Log.e("BestSellerBooksFragment", message)
49+
}
50+
}
51+
})
52+
}
53+
54+
override fun onItemClick(item: BestSellerBook) {
55+
Toast.makeText(context, "test", Toast.LENGTH_LONG).show()
56+
}
57+
58+
companion object {
59+
fun newInstance(columnCount: Int): BestSellerBooksFragment {
60+
val fragment = BestSellerBooksFragment()
61+
val args = Bundle()
62+
fragment.arguments = args
63+
return fragment
64+
}
65+
}
66+
}

app/src/main/java/com/codepath/bestsellerlistapp/BestSellerBooksRecyclerViewAdapter.java

Lines changed: 0 additions & 76 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.codepath.bestsellerlistapp
2+
3+
import android.view.LayoutInflater
4+
import android.view.View
5+
import android.view.ViewGroup
6+
import android.widget.TextView
7+
import androidx.recyclerview.widget.RecyclerView
8+
import androidx.recyclerview.widget.RecyclerView.Adapter
9+
import androidx.recyclerview.widget.RecyclerView.ViewHolder
10+
import com.codepath.bestsellerlistapp.BestSellerBooksRecyclerViewAdapter.BookViewHolder
11+
import com.codepath.bestsellerlistapp.R.id
12+
import com.codepath.bestsellerlistapp.R.layout
13+
import com.codepath.bestsellerlistapp.models.BestSellerBook
14+
15+
/**
16+
* [RecyclerView.Adapter] that can display a [BestSellerBook] and makes a call to the
17+
* specified [OnListFragmentInteractionListener].
18+
*/
19+
class BestSellerBooksRecyclerViewAdapter(
20+
private val books: List<BestSellerBook>,
21+
private val mListener: OnListFragmentInteractionListener?
22+
) : Adapter<BookViewHolder>() {
23+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BookViewHolder {
24+
val view = LayoutInflater.from(parent.context)
25+
.inflate(layout.fragment_best_seller_book, parent, false)
26+
return BookViewHolder(view)
27+
}
28+
29+
override fun onBindViewHolder(holder: BookViewHolder, position: Int) {
30+
holder.mItem = books[position]
31+
holder.mBookTitle.text = books[position].title
32+
holder.mBookAuthor.text = books[position].author
33+
holder.mView.setOnClickListener {
34+
holder.mItem?.let { book ->
35+
mListener?.onItemClick(book)
36+
}
37+
}
38+
}
39+
40+
override fun getItemCount(): Int {
41+
return books.size
42+
}
43+
44+
inner class BookViewHolder(val mView: View) : ViewHolder(mView) {
45+
val mBookTitle: TextView = mView.findViewById<View>(id.book_title) as TextView
46+
val mBookAuthor: TextView = mView.findViewById<View>(id.book_author) as TextView
47+
var mItem: BestSellerBook? = null
48+
49+
override fun toString(): String {
50+
return mBookTitle.toString() + " '" + mBookAuthor.text + "'"
51+
}
52+
}
53+
}

app/src/main/java/com/codepath/bestsellerlistapp/MainActivity.java

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.codepath.bestsellerlistapp
2+
3+
import android.os.Bundle
4+
import androidx.appcompat.app.AppCompatActivity
5+
import com.codepath.bestsellerlistapp.R.id
6+
import com.codepath.bestsellerlistapp.R.layout
7+
8+
class MainActivity : AppCompatActivity() {
9+
override fun onCreate(savedInstanceState: Bundle?) {
10+
super.onCreate(savedInstanceState)
11+
setContentView(layout.activity_main)
12+
val supportFragmentManager = supportFragmentManager
13+
val fragmentTransaction = supportFragmentManager.beginTransaction()
14+
fragmentTransaction.replace(id.content, BestSellerBooksFragment(), null).commit()
15+
}
16+
}

app/src/main/java/com/codepath/bestsellerlistapp/OnListFragmentInteractionListener.java

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codepath.bestsellerlistapp
2+
3+
import com.codepath.bestsellerlistapp.models.BestSellerBook
4+
5+
interface OnListFragmentInteractionListener {
6+
fun onItemClick(item: BestSellerBook)
7+
}

0 commit comments

Comments
 (0)