Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

24 homework #27

Open
wants to merge 3 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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'androidx.navigation.safeargs.kotlin'
}

android {
compileSdk 31

viewBinding {
enabled = true
}

defaultConfig {
applicationId "com.github.krottv.tmstemp"
minSdk 24
Expand All @@ -32,6 +37,17 @@ android {
}

dependencies {
implementation "androidx.navigation:navigation-fragment-ktx:2.4.2"
implementation "androidx.navigation:navigation-ui-ktx:2.4.2"
implementation "androidx.fragment:fragment-ktx:1.4.1"
implementation "androidx.activity:activity-ktx:1.4.0"

implementation("io.coil-kt:coil:2.0.0-rc03")

implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1'

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.github.krottv.tmstemp">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application
android:allowBackup="true"
Expand All @@ -10,7 +12,7 @@
android:supportsRtl="true"
android:theme="@style/Theme.TmsTemp">
<activity
android:name=".MainActivity"
android:name=".view.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
11 changes: 0 additions & 11 deletions app/src/main/java/com/github/krottv/tmstemp/MainActivity.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.krottv.tmstemp.binder

interface MainActivityDataBinder {
fun bind()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.krottv.tmstemp.binder

import android.app.Activity
import android.view.LayoutInflater
import com.github.krottv.tmstemp.databinding.ActivityMainBinding


class MainActivityRecyclerScrollDataBinder(private val activity: Activity): MainActivityDataBinder {

override fun bind() {
val container = ActivityMainBinding.inflate(LayoutInflater.from(activity))
activity.setContentView(container.root)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.github.krottv.tmstemp.binder

import android.os.Bundle
import android.transition.*
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.github.krottv.tmstemp.R
import com.github.krottv.tmstemp.databinding.SceneContainerBinding
import com.github.krottv.tmstemp.domain.MessageModel
import com.github.krottv.tmstemp.view.MessageAdapter

class RecyclerFragmentBinder(private val fragment: Fragment, private val onItemClick: (View, MessageModel) -> Unit) {

private lateinit var binding: SceneContainerBinding

fun bindView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = SceneContainerBinding.inflate(inflater, container, false)

return binding.root
}


private val customTransition = TransitionSet().apply {
addTransition(ChangeBounds())
addTransition(Fade(Fade.IN))
}

fun onDataLoaded(data: List<MessageModel>) {

val scene = Scene.getSceneForLayout(binding.sceneRoot, R.layout.messages_fragment, fragment.requireContext())
TransitionManager.go(scene, customTransition)

val recyclerView = scene.sceneRoot.findViewById<RecyclerView>(R.id.container)
recyclerView.layoutManager = LinearLayoutManager(fragment.requireContext())

if (recyclerView.adapter == null) {
recyclerView.adapter = MessageAdapter(data, onItemClick)
} else {
(recyclerView.adapter as MessageAdapter).data = data
}
}

fun showError(exception: Throwable) {

val scene = Scene.getSceneForLayout(binding.sceneRoot, R.layout.show_error, fragment.requireContext())
TransitionManager.go(scene, customTransition)

scene.sceneRoot.findViewById<TextView>(R.id.errorText).apply {
text = exception.message
}
}

fun showProgress() {

val scene = Scene.getSceneForLayout(binding.sceneRoot, R.layout.loading, fragment.requireContext())
TransitionManager.go(scene, customTransition)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.krottv.tmstemp.data

import com.github.krottv.tmstemp.domain.MessageModel

interface MessagesRemoteDataSource {
suspend fun getMessages(): List<MessageModel>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.krottv.tmstemp.data

import com.github.krottv.tmstemp.domain.MessageModel
import kotlinx.coroutines.delay
import java.lang.IllegalStateException

class MessagesRemoteDataSourceError: MessagesRemoteDataSource {
override suspend fun getMessages(): List<MessageModel> {
delay(2000)
throw IllegalStateException("IllegalStateException")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.krottv.tmstemp.data

import com.github.krottv.tmstemp.domain.MessageModel
import kotlinx.coroutines.delay

class MessagesRemoteDataSourceFake: MessagesRemoteDataSource {
override suspend fun getMessages(): List<MessageModel> {
delay(2000)
val message = MessageModel(
"https://images.unsplash.com/photo-1505062351414-586330b076f2?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1080&q=80",
"Пункт такойто",
"Какой-то текст Какой-то текст Какой-то текст Какой-то текст"
)
val result = ArrayList<MessageModel>(20)
for (i in 0..20) {
result.add(message.copy(mainText = "Пункт $i"))
}
return result
}
}
36 changes: 36 additions & 0 deletions app/src/main/java/com/github/krottv/tmstemp/domain/MessageModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.github.krottv.tmstemp.domain

import android.os.Parcel
import android.os.Parcelable
import androidx.annotation.Keep

@Keep
data class MessageModel(val mainImage: String, val mainText: String, val littleText: String): Parcelable {
constructor(parcel: Parcel) : this(
parcel.readString()!!,
parcel.readString()!!,
parcel.readString()!!
)

fun transitionId() = mainText + "_"

override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeString(mainImage)
parcel.writeString(mainText)
parcel.writeString(littleText)
}

override fun describeContents(): Int {
return 0
}

companion object CREATOR : Parcelable.Creator<MessageModel> {
override fun createFromParcel(parcel: Parcel): MessageModel {
return MessageModel(parcel)
}

override fun newArray(size: Int): Array<MessageModel?> {
return arrayOfNulls(size)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.krottv.tmstemp.presentation

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.github.krottv.tmstemp.data.MessagesRemoteDataSource
import com.github.krottv.tmstemp.data.MessagesRemoteDataSourceError
import com.github.krottv.tmstemp.data.MessagesRemoteDataSourceFake
import com.github.krottv.tmstemp.domain.MessageModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

class MessageViewModel: ViewModel() {
private val fakeMessages: MessagesRemoteDataSource = MessagesRemoteDataSourceFake()

private val _state = MutableStateFlow<Result<List<MessageModel>>?>(null)
val state: StateFlow<Result<List<MessageModel>>?> = _state

fun loadData() {
viewModelScope.launch(Dispatchers.IO) {
val result = try {
Result.success(fakeMessages.getMessages())
} catch (exception: Throwable) {
Result.failure(exception)
}

_state.emit(result)
}
}
}
Loading