Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3. Add news screen #12

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
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
48 changes: 45 additions & 3 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
@Suppress("DSL_SCOPE_VIOLATION")
plugins {
id("dagger.hilt.android.plugin")
kotlin("kapt")
alias(libs.plugins.androidApplication)
alias(libs.plugins.kotlinAndroid)
}
true

android {
namespace = "ke.droidcon.tujenge"
compileSdk = 33
compileSdk = 34

defaultConfig {
applicationId = "ke.droidcon.tujenge"
Expand Down Expand Up @@ -35,13 +37,53 @@ android {
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.2"
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}

dependencies {

implementation(libs.androidx.coreKtx)
implementation(libs.androidx.appcompat)
implementation(libs.android.material)
//implementation(libs.android.material)
//material 3
implementation(libs.material3)
//material-icons
implementation ("androidx.compose.material:material-icons-extended:1.5.4")


implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")
implementation("androidx.activity:activity-compose:1.8.0")
implementation(platform("androidx.compose:compose-bom:2023.03.00"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
// implementation("androidx.appcompat:appcompat:1.6.1")#removed when moving to compose


//Dagger - Hilt
implementation("com.google.dagger:hilt-android:2.48")
kapt("com.google.dagger:hilt-android-compiler:2.45")
kapt("androidx.hilt:hilt-compiler:1.1.0")
implementation("androidx.hilt:hilt-navigation-compose:1.1.0")

//Retrofit
//retrofit and kotlinx dependencies
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
implementation("com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:1.0.0")
implementation("com.squareup.okhttp3:okhttp:4.11.0")
implementation("com.squareup.okhttp3:logging-interceptor:4.10.0")



testImplementation(libs.junit4)
androidTestImplementation(libs.androidx.test.junit)
Expand Down
20 changes: 18 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,31 @@
<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:name=".TujengeApp"
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.Tujenge"
tools:targetApi="31" />
android:theme="@style/Theme.TujengeApp"
tools:targetApi="31" >
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.TujengeApp">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

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

import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import dagger.hilt.android.AndroidEntryPoint
import ke.droidcon.tujenge.presentation.news_list_page.NewsListScreen
import ke.droidcon.tujenge.ui.theme.TujengeAppTheme


@AndroidEntryPoint
class MainActivity : ComponentActivity() {

@RequiresApi(Build.VERSION_CODES.O)
@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)


setContent {

TujengeAppTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
NewsListScreen()
}
}

}
}
}

11 changes: 11 additions & 0 deletions app/src/main/java/ke/droidcon/tujenge/TujengeApp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ke.droidcon.tujenge

import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class TujengeApp : Application() {
override fun onCreate() {
super.onCreate()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ke.droidcon.tujenge.data.remote.dto

data class NewsResponse(
val category: String,
val data: List<NewsItem>,
val success: Boolean
)

data class NewsItem(
val author: String,
val content: String,
val date: String,
val imageUrl: String,
val readMoreUrl: String,
val time: String,
val title: String,
val url: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ke.droidcon.tujenge.data.remote.retrofit

import okhttp3.Interceptor

object HeaderInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): okhttp3.Response {
val request = chain.request()
val newRequest = request.newBuilder()
.addHeader("Authorization", "Bearer ")
.build()
return chain.proceed(newRequest)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ke.droidcon.tujenge.data.remote.retrofit

import ke.droidcon.tujenge.data.remote.dto.NewsResponse
import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Path

interface NewsFetchingService {

@GET("news?category={category_name}")
fun getNewsByCategory(@Path("category_name") categoryName: String): Call<NewsResponse>

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ke.droidcon.tujenge.data.remote.retrofit

import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit

object RetrofitProvider {
private const val BASE_URL = "https://inshorts.deta.dev/"

private fun provide(): Retrofit {
val json = Json { ignoreUnknownKeys = true }//to ignore unkown keys

return Retrofit.Builder()
.baseUrl(BASE_URL)
.client(provideOkhttpClient())
.addConverterFactory(json.asConverterFactory("application/json".toMediaType()))
.build()
}

private fun provideOkhttpClient(): OkHttpClient =
OkHttpClient.Builder()
.addInterceptor(HttpLoggingInterceptor().also {
it.level = HttpLoggingInterceptor.Level.BODY
})
.addInterceptor(HeaderInterceptor)
.build()


fun createNewsFetchingService(): NewsFetchingService {
return provide().create(NewsFetchingService::class.java)
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ke.droidcon.tujenge.presentation.article_page

import android.annotation.SuppressLint
import android.view.ViewGroup
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.compose.foundation.background
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.viewinterop.AndroidView

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ArticleScreen(){
Scaffold(
topBar = { TopAppBar(
title = { Text("Tujenge DroidCon News", color = Color.White)},
modifier = Modifier.background(Color(0xff0f9d58))
)},
content = { MyContent() }
)

}

@Composable
fun MyContent(){

val articleUrl = "https://www.geeksforgeeks.org"


AndroidView(factory = {
WebView(it).apply {
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
webViewClient = WebViewClient()
loadUrl(articleUrl)
}
}, update = {
it.loadUrl(articleUrl)
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ke.droidcon.tujenge.presentation.article_page

import androidx.lifecycle.ViewModel

class ArticleScreenViewModel : ViewModel() {
}
Loading