Skip to content

Commit

Permalink
Initial setup
Browse files Browse the repository at this point in the history
  • Loading branch information
abhiwatt committed Aug 12, 2024
1 parent e64ed4b commit bcebb8d
Show file tree
Hide file tree
Showing 43 changed files with 1,127 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
76 changes: 76 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.jetbrains.kotlin.android)
}

android {
namespace = "wp.fetchstories"
compileSdk = 34

defaultConfig {
applicationId = "wp.fetchstories"
minSdk = 24
targetSdk = 34
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.1"
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}

dependencies {

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.activity.compose)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.ui)
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
implementation(libs.androidx.recyclerview)
implementation(libs.androidx.constraintlayout)
testImplementation(libs.junit)
implementation(libs.gson)
implementation(libs.retrofit)
implementation(libs.converter.gson)
implementation(libs.okhttp)
implementation(libs.glide)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.ui.test.junit4)
debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest)
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package wp.fetchstories

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("wp.fetchstories", appContext.packageName)
}
}
29 changes: 29 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.WPstories"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.WPstories">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
73 changes: 73 additions & 0 deletions app/src/main/java/wp/fetchstories/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package wp.fetchstories

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import wp.fetchstories.ui.theme.WPstoriesTheme
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

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

val recyclerView = findViewById<RecyclerView>(R.id.recyclerview)
recyclerView.layoutManager = LinearLayoutManager(this)
val adapter = RecyclerViewAdapter()
recyclerView.adapter = adapter

val apiInterface =
RetrofitClient().getApiClient()?.create(RetrofitService::class.java) ?: return

apiInterface.getAllStories().enqueue(object : Callback<StoriesResponse> {
override fun onResponse(
call: Call<StoriesResponse>,
response: Response<StoriesResponse>
) {
adapter.submitList(response.body()?.stories)
}

override fun onFailure(call: Call<StoriesResponse>, t: Throwable) {
Toast.makeText(this@MainActivity, "Failed to load data", Toast.LENGTH_SHORT).show()
}
})

//enableEdgeToEdge()
// setContent {
// WPstoriesTheme {
// Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
// Greeting(
// name = "Android",
// modifier = Modifier.padding(innerPadding)
// )
// }
// }
// }


}
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!",
modifier = modifier
)
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
WPstoriesTheme {
Greeting("Android")
}
}
49 changes: 49 additions & 0 deletions app/src/main/java/wp/fetchstories/RecyclerViewAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package wp.fetchstories

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide

class RecyclerViewAdapter :
ListAdapter<Story, RecyclerViewAdapter.RecyclerViewHolder>(DiffCallback()) {

override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
val item = getItem(position)
holder.bindData(item)
}

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

class RecyclerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val storyTitle = itemView.findViewById<TextView>(R.id.story_title)
val storyAuthor = itemView.findViewById<TextView>(R.id.story_author)
val storyCover = itemView.findViewById<ImageView>(R.id.story_cover)

fun bindData(story: Story) {
storyTitle.text = story.title
storyAuthor.text = story.user.name
Glide.with(itemView.context)
.load(story.cover)
.into(storyCover)
}
}
}

class DiffCallback : DiffUtil.ItemCallback<Story>() {
override fun areItemsTheSame(oldItem: Story, newItem: Story): Boolean {
return oldItem.id == newItem.id
}

override fun areContentsTheSame(oldItem: Story, newItem: Story): Boolean {
return oldItem == newItem
}
}
18 changes: 18 additions & 0 deletions app/src/main/java/wp/fetchstories/RetrofitClient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package wp.fetchstories

import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class RetrofitClient {
var BASE_URL: String = "https://www.wattpad.com/api/v3/"
var retrofit: Retrofit? = null

fun getApiClient(): Retrofit? {
if (retrofit == null) {
retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create()).build()
}
return retrofit
}
}
9 changes: 9 additions & 0 deletions app/src/main/java/wp/fetchstories/RetrofitService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package wp.fetchstories

import retrofit2.Call
import retrofit2.http.GET

interface RetrofitService {
@GET("stories?offset=0&limit=10&fields=stories(id,title,cover,user)&filter=new")
fun getAllStories(): Call<StoriesResponse>
}
21 changes: 21 additions & 0 deletions app/src/main/java/wp/fetchstories/StoriesResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package wp.fetchstories

import com.google.gson.annotations.SerializedName

data class StoriesResponse(
@SerializedName("stories") val stories: List<Story>,
@SerializedName("nextUrl") val nextUrl: String
)

data class Story(
@SerializedName("id") val id: Int,
@SerializedName("title") val title: String,
@SerializedName("user") val user: User,
@SerializedName("cover") val cover: String
)

data class User(
@SerializedName("name") val name: String,
@SerializedName("avatar") val avatar: String,
@SerializedName("fullname") val fullname: String
)
11 changes: 11 additions & 0 deletions app/src/main/java/wp/fetchstories/ui/theme/Color.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package wp.fetchstories.ui.theme

import androidx.compose.ui.graphics.Color

val Purple80 = Color(0xFFD0BCFF)
val PurpleGrey80 = Color(0xFFCCC2DC)
val Pink80 = Color(0xFFEFB8C8)

val Purple40 = Color(0xFF6650a4)
val PurpleGrey40 = Color(0xFF625b71)
val Pink40 = Color(0xFF7D5260)
Loading

0 comments on commit bcebb8d

Please sign in to comment.