Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import kotlinx.coroutines.flow.flow

class BookRepo() {
val remoteSource = RemoteSource()
fun books(): Flow<ApiResult<List<Item>>> = flow {
fun books(searchWord: String): Flow<ApiResult<List<Item>>> = flow {
emit(ApiResult.Loading)
emit(remoteSource.getBooks())
emit(remoteSource.getBooks(searchWord))
}

}
1 change: 1 addition & 0 deletions app/src/main/java/com/deelib/weatherapp/model/ApiResult.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ sealed class ApiResult<out T> {
data class Success<out T>(val data: T) : ApiResult<T>()
data class Error(val message: String) : ApiResult<Nothing>()
object Loading : ApiResult<Nothing>()
object Ideal : ApiResult<Nothing>()
}
12 changes: 6 additions & 6 deletions app/src/main/java/com/deelib/weatherapp/model/BooksList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.os.Parcelable
data class BooksList(
val items: List<Item>,
val kind: String,
val totalItems: Int
val totalItems: String
)

data class Item(
Expand Down Expand Up @@ -58,7 +58,7 @@ data class SearchInfo(
data class VolumeInfo(
val allowAnonLogging: Boolean,
val authors: List<String>,
val averageRating: Int,
val averageRating: String,
val canonicalVolumeLink: String,
val categories: List<String>,
val contentVersion: String,
Expand All @@ -74,7 +74,7 @@ data class VolumeInfo(
val printType: String,
val publishedDate: String,
val publisher: String,
val ratingsCount: Int,
val ratingsCount: String,
val readingModes: ReadingModes,
val subtitle: String,
val title: String
Expand All @@ -96,7 +96,7 @@ data class ListPrice(
)

data class Offer(
val finskyOfferType: Int,
val finskyOfferType: String,
val listPrice: ListPriceX,
val retailPrice: RetailPrice
)
Expand All @@ -107,12 +107,12 @@ data class RetailPriceX(
)

data class ListPriceX(
val amountInMicros: Int,
val amountInMicros: String,
val currencyCode: String
)

data class RetailPrice(
val amountInMicros: Int,
val amountInMicros: String,
val currencyCode: String
)

Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/com/deelib/weatherapp/network/ApiService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package com.deelib.weatherapp.network
import com.deelib.weatherapp.model.BooksList
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Query

interface ApiService {
@GET("volumes?q=quilting")
suspend fun getBooks(): Response<BooksList>
// @GET("volumes?q=quilting")
@GET("volumes")
suspend fun getBooks(@Query("q") query: String): Response<BooksList>
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import com.deelib.weatherapp.model.Item
class RemoteSource {
val apiService: ApiService = RetrofitClient.apiService

suspend fun getBooks(): ApiResult<List<Item>> {
val response = apiService.getBooks()
suspend fun getBooks(str: String = "quilting"): ApiResult<List<Item>> {
val response = apiService.getBooks(str)
try {
if (response.isSuccessful) {
val body = response.body()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class DetailsFragment : BottomSheetDialogFragment() {
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = item.volumeInfo.authors.firstOrNull() ?: "",
text = item.volumeInfo.authors?.firstOrNull() ?: "",
fontSize = 16.sp,
fontWeight = FontWeight.Normal,
color = Color.White
Expand Down
63 changes: 58 additions & 5 deletions app/src/main/java/com/deelib/weatherapp/view/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
Expand All @@ -33,7 +38,9 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.fragment.app.FragmentManager
Expand All @@ -53,10 +60,48 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
RepositoryHolder.bookRepo = BookRepo()
booksViewModel.getBooks()
setContent {
WeatherAppTheme {
HomePage(booksViewModel, supportFragmentManager)
Column {
SearchBar(booksViewModel)
HomePage(booksViewModel, supportFragmentManager)
}
}
}
}
}

@Composable
fun SearchBar(booksViewModel: BooksViewModel) {
var keyboardController = LocalSoftwareKeyboardController.current
var word by remember { mutableStateOf("") }
Column(
modifier = Modifier
.fillMaxWidth()
.padding(25.dp), horizontalAlignment = Alignment.CenterHorizontally
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(8.dp),
horizontalArrangement = Arrangement.SpaceEvenly,
verticalAlignment = Alignment.CenterVertically
) {
OutlinedTextField(
modifier = Modifier.weight(1f),
value = word,
onValueChange = { newText -> word = newText },
label = { Text("Search Books e.g. quilting") })


IconButton(onClick = {
booksViewModel.getBooks(word)
keyboardController?.hide()
}) {
Icon(
imageVector = Icons.Default.Search,
contentDescription = "Search"
)
}
}
}
Expand Down Expand Up @@ -91,15 +136,23 @@ fun HomePage(viewModel: BooksViewModel, supportFragmentManager: FragmentManager)
is ApiResult.Loading -> {
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
CircularProgressIndicator()

}
}

ApiResult.Ideal -> {
Text(
text = "Search for any book Series",
modifier = Modifier
.fillMaxWidth()
.padding(10.dp), fontSize = 16.sp, textAlign = TextAlign.Center
)
}
}
}

@Composable
fun BookList(items: List<Item>, onItemClick: (Item) -> Unit = {}) {
LazyColumn(contentPadding = PaddingValues(top = 60.dp)) {
LazyColumn(contentPadding = PaddingValues(top = 40.dp)) {
itemsIndexed(items) { index, item ->
BookItem(item, index, onItemClick)
if (index < items.size - 1) {
Expand Down Expand Up @@ -139,7 +192,7 @@ fun BookItem(item: Item, index: Int = 0, onItemClick: (Item) -> Unit) {
fontSize = 16.sp, fontWeight = FontWeight.Bold
)
Text(
text = item.volumeInfo.authors.firstOrNull() ?: "",
text = item.volumeInfo.authors?.firstOrNull() ?: "",
fontSize = 14.sp,
fontWeight = FontWeight.Normal
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.deelib.weatherapp.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.deelib.weatherapp.Repository.BookRepo
import com.deelib.weatherapp.RepositoryHolder
import com.deelib.weatherapp.model.ApiResult
import com.deelib.weatherapp.model.Item
Expand All @@ -12,7 +13,7 @@ import kotlinx.coroutines.launch
class BooksViewModel : ViewModel() {
val repo = RepositoryHolder.bookRepo

private val _booksData = MutableStateFlow<ApiResult<List<Item>>>(ApiResult.Loading)
private val _booksData = MutableStateFlow<ApiResult<List<Item>>>(ApiResult.Ideal)
val booksData: StateFlow<ApiResult<List<Item>>> = _booksData

private val _selectedBook = MutableStateFlow<Item?>(null)
Expand All @@ -26,9 +27,9 @@ class BooksViewModel : ViewModel() {
_selectedBook.value = null
}

fun getBooks() {
fun getBooks(word: String) {
viewModelScope.launch {
repo.books().collect {
repo.books(word).collect {
_booksData.value = it
}
}
Expand Down