Skip to content

Commit

Permalink
Network & API (Github)
Browse files Browse the repository at this point in the history
retrofit
  • Loading branch information
whyfoo committed Jan 4, 2022
1 parent 678e134 commit c420acd
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 41 deletions.
6 changes: 6 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ android {

dependencies {

implementation "com.squareup.retrofit2:retrofit:2.6.2"
implementation "com.squareup.retrofit2:converter-gson:2.6.2"
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation "com.github.bumptech.glide:glide:4.11.0"
implementation 'com.google.android.material:material:1.0.0'

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.5.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

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

<application
android:allowBackup="true"
Expand Down
55 changes: 28 additions & 27 deletions app/src/main/java/com/trolle/trolleapp/PayFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.trolle.trolleapp.data.Item
import com.trolle.trolleapp.data.adapter.ItemAdapter
import com.trolle.trolleapp.data.adapter.ListItemAdapter
import com.trolle.trolleapp.data.viewmodel.MainViewModel
import com.trolle.trolleapp.databinding.FragmentPayBinding
import com.trolle.trolleapp.ui.pay.CheckoutActivity
import com.trolle.trolleapp.ui.side_menu.EditProfileActivity
Expand All @@ -20,6 +25,9 @@ import java.util.*

class PayFragment : Fragment() {

private lateinit var viewModel: MainViewModel
private lateinit var adapter: ItemAdapter

private lateinit var rvItems: RecyclerView
private val list = ArrayList<Item>()
private var _binding: FragmentPayBinding? = null
Expand All @@ -41,18 +49,24 @@ class PayFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

rvItems = binding.rvItems
rvItems.setHasFixedSize(true)
adapter = ItemAdapter()
adapter.notifyDataSetChanged()
viewModel = ViewModelProvider(this, ViewModelProvider.NewInstanceFactory()).get(MainViewModel::class.java)

Handler(Looper.getMainLooper()).postDelayed({
showLoading(true)
Handler(Looper.getMainLooper()).postDelayed({
showLoading(false)
}, 2000)
}, 2000)
binding.rvItems.layoutManager = LinearLayoutManager(context)
binding.rvItems.setHasFixedSize(true)
binding.rvItems.adapter = adapter

list.addAll(listItems)
showRecyclerList()
searchItem()
Toast.makeText(context, "Searching items", Toast.LENGTH_SHORT).show()

viewModel.getSearchItems().observe(viewLifecycleOwner, {
if (it!=null){
showLoading(true)
adapter.setList(it)
showLoading(false)
}
})

binding.buttonCheckout.setOnClickListener {
requireActivity().run{
Expand All @@ -61,24 +75,10 @@ class PayFragment : Fragment() {
}
}

private val listItems: ArrayList<Item>
get() {
val dataName = resources.getStringArray(R.array.data_name)
val dataCount = resources.getIntArray(R.array.data_count)
val dataPrice = resources.getIntArray(R.array.data_price)
val listItem = ArrayList<Item>()

for (i in 1 until (dataName.size-1)) {
val item = Item(dataName.get(i),dataCount.get(i), dataPrice.get(i))
listItem.add(item)
}
return listItem
}
private fun searchItem(){
val query = "wondrouss"
viewModel.setSearchItems(query)

private fun showRecyclerList() {
rvItems.layoutManager = LinearLayoutManager(context)
val listItemAdapter = ListItemAdapter(list)
rvItems.adapter = listItemAdapter
}

private fun showLoading(state: Boolean) {
Expand All @@ -88,6 +88,7 @@ class PayFragment : Fragment() {
} else {
binding.progressBar.visibility = View.GONE
binding.rvItems.visibility = View.VISIBLE
binding.layoutSubtotal.visibility = View.VISIBLE
}
}

Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/trolle/trolleapp/data/Item.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.trolle.trolleapp.data

data class Item(
var name: String,
var count: Int,
var price: Int
val login: String,
val type: String,
val id: Int
)
7 changes: 7 additions & 0 deletions app/src/main/java/com/trolle/trolleapp/data/ItemResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.trolle.trolleapp.data

import java.util.*

data class ItemResponse (
val items : ArrayList<Item>
)
43 changes: 43 additions & 0 deletions app/src/main/java/com/trolle/trolleapp/data/adapter/ItemAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.trolle.trolleapp.data.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.trolle.trolleapp.data.Item
import com.trolle.trolleapp.databinding.ItemRowListBinding
import java.util.*

class ItemAdapter: RecyclerView.Adapter<ItemAdapter.ItemViewHolder>() {
private val list = ArrayList<Item>()

fun setList(items: ArrayList<Item>){
list.clear()
list.addAll(items)
notifyDataSetChanged()
}

inner class ItemViewHolder(val binding: ItemRowListBinding) : RecyclerView.ViewHolder(binding.root){
fun bind(item: Item){
binding.tvItemName.text = item.login
binding.tvItemCount.text = item.type
binding.tvItemPrice.text = item.id.toString()

}

}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
val view = ItemRowListBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ItemViewHolder(view)

}

override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
holder.bind(list[position])
}

override fun getItemCount(): Int = list.size


}
15 changes: 15 additions & 0 deletions app/src/main/java/com/trolle/trolleapp/data/network/api/Api.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.trolle.trolleapp.data.network.api

import com.trolle.trolleapp.data.ItemResponse
import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Headers
import retrofit2.http.Query

interface Api {
@GET("search/users")
@Headers("Authorization: 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", "User-Agent: request")
fun getSearchItems(
@Query("q") query: String?
): Call<ItemResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.trolle.trolleapp.data.network.api

import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.create
import java.util.concurrent.TimeUnit

object RetrofitClient {
private const val BASE_URL = "https://api.github.com/"

val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()

val apiInstance: Api = retrofit.create(Api::class.java)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.trolle.trolleapp.data.viewmodel

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.trolle.trolleapp.data.Item
import com.trolle.trolleapp.data.ItemResponse
import com.trolle.trolleapp.data.network.api.RetrofitClient
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.util.*

class MainViewModel: ViewModel() {

val listItems = MutableLiveData<ArrayList<Item>>()

fun setSearchItems(query: String){
RetrofitClient.apiInstance
.getSearchItems(query)
.enqueue(object: Callback<ItemResponse>{
override fun onResponse(call: Call<ItemResponse>, response: Response<ItemResponse>) {
if (response.isSuccessful){
listItems.postValue(response.body()?.items)
}
}

override fun onFailure(call: Call<ItemResponse>, t: Throwable) {
Log.d("Failure", "ada error mainviewmodel")
}

})
}

fun getSearchItems(): LiveData<ArrayList<Item>>{
return listItems
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="10dp">
<solid android:color="@color/pink"/> <!-- this one is ths color of the Rounded Button -->
<solid android:color="@color/cardview_dark_background"/> <!-- this one is ths color of the Rounded Button -->
<corners
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/layout/fragment_pay.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@
android:layout_below="@id/rv_items">

<LinearLayout
android:id="@+id/layout_subtotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="15dp">
android:layout_margin="15dp"
android:visibility="gone">

<TextView
android:id="@+id/text_view_sub_total"
Expand Down
18 changes: 9 additions & 9 deletions app/src/main/res/layout/item_row_list.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_margin="20dp"
android:padding="20dp"
android:background="@color/cardview_dark_background">
android:layout_marginHorizontal="20dp"
android:layout_marginVertical="12dp"
android:padding="18dp"
android:background="@drawable/rounded_items">

<TextView
android:id="@+id/tv_item_name"
android:textColor="@color/white"
android:layout_width="180dp"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
Expand All @@ -27,7 +28,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginEnd="2dp"
android:textSize="16sp"
android:textStyle="bold"
android:gravity="end"
Expand All @@ -36,9 +37,8 @@
<TextView
android:id="@+id/tv_item_price"
android:textColor="@color/white"
android:layout_width="100dp"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:textSize="16sp"
android:textStyle="bold"
Expand Down

0 comments on commit c420acd

Please sign in to comment.