Skip to content

Commit

Permalink
Merge pull request #64 from ku-ring/task/kuring-46
Browse files Browse the repository at this point in the history
kuring-46 공지 웹뷰 화면용 데이터 객체 생성
  • Loading branch information
mwy3055 authored Aug 21, 2023
2 parents 31e5cb6 + c86f987 commit 955a29a
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 106 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.ku_stacks.ku_ring

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.media.RingtoneManager
import android.os.Build
import androidx.core.app.NotificationCompat
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
Expand Down Expand Up @@ -95,8 +93,6 @@ class MyFireBaseMessagingService : FirebaseMessagingService() {
url,
articleId,
category,
DateUtil.getToday(),
title
)
val pendingIntent = PendingIntent.getActivity(
this,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.ku_stacks.ku_ring.data.mapper

import com.ku_stacks.ku_ring.data.model.Notice
import com.ku_stacks.ku_ring.data.model.WebViewNotice

fun Notice.toWebViewNotice() = WebViewNotice(
url = url,
articleId = articleId,
category = category
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.ku_stacks.ku_ring.data.mapper

import com.ku_stacks.ku_ring.data.model.WebViewNotice
import com.ku_stacks.ku_ring.ui.my_notification.ui_model.PushContentUiModel
import com.ku_stacks.ku_ring.util.WordConverter

fun PushContentUiModel.toWebViewNotice() = WebViewNotice(
url = fullUrl,
articleId = articleId,
category = WordConverter.convertKoreanToEnglish(categoryKor),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.ku_stacks.ku_ring.data.model

import java.io.Serializable

data class WebViewNotice(
val url: String,
val articleId: String,
val category: String,
): Serializable
52 changes: 12 additions & 40 deletions app/src/main/java/com/ku_stacks/ku_ring/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf
import androidx.viewpager2.widget.ViewPager2
import com.ku_stacks.ku_ring.R
import com.ku_stacks.ku_ring.data.model.WebViewNotice
import com.ku_stacks.ku_ring.databinding.ActivityMainBinding
import com.ku_stacks.ku_ring.ui.notice_webview.NoticeWebActivity
import com.ku_stacks.ku_ring.util.showToast
Expand Down Expand Up @@ -36,25 +36,16 @@ class MainActivity : AppCompatActivity() {
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)

intent?.getStringExtra(NoticeWebActivity.NOTICE_URL)?.let {
val articleId = intent.getStringExtra(NoticeWebActivity.NOTICE_ARTICLE_ID)
val category = intent.getStringExtra(NoticeWebActivity.NOTICE_CATEGORY)
val postedDate = intent.getStringExtra(NoticeWebActivity.NOTICE_POSTED_DATE)
val subject = intent.getStringExtra(NoticeWebActivity.NOTICE_SUBJECT)
navToNoticeActivity(it, articleId, category, postedDate, subject)
(intent?.getSerializableExtra(NoticeWebActivity.WEB_VIEW_NOTICE) as? WebViewNotice)?.let { webViewNotice ->
navToNoticeActivity(webViewNotice)
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

intent?.getStringExtra(NoticeWebActivity.NOTICE_URL)?.let {
val articleId = intent.getStringExtra(NoticeWebActivity.NOTICE_ARTICLE_ID)
val category = intent.getStringExtra(NoticeWebActivity.NOTICE_CATEGORY)
val postedDate = intent.getStringExtra(NoticeWebActivity.NOTICE_POSTED_DATE)
val subject = intent.getStringExtra(NoticeWebActivity.NOTICE_SUBJECT)
Timber.d("Notification: received $articleId, $category, $postedDate, $subject, $it")
navToNoticeActivity(it, articleId, category, postedDate, subject)
(intent?.getSerializableExtra(NoticeWebActivity.WEB_VIEW_NOTICE) as? WebViewNotice)?.let { webViewNotice ->
navToNoticeActivity(webViewNotice)
}

setupBinding()
Expand Down Expand Up @@ -93,22 +84,10 @@ class MainActivity : AppCompatActivity() {
return true
}

private fun navToNoticeActivity(
noticeUrl: String?,
articleId: String?,
category: String?,
postedDate: String?,
subject: String?,
) {
val newIntent = NoticeWebActivity.createIntent(
this,
noticeUrl,
articleId,
category,
postedDate,
subject
)
startActivity(newIntent)
private fun navToNoticeActivity(webViewNotice: WebViewNotice) {
Timber.d("Notification received: $webViewNotice")
val intent = NoticeWebActivity.createIntent(this, webViewNotice)
startActivity(intent)
overridePendingTransition(R.anim.anim_slide_right_enter, R.anim.anim_stay_exit)
}

Expand All @@ -132,18 +111,11 @@ class MainActivity : AppCompatActivity() {
url: String,
articleId: String,
category: String,
postedDate: String,
subject: String
) {
val intent = Intent(activity, MainActivity::class.java).apply {
putExtras(
bundleOf(
NoticeWebActivity.NOTICE_URL to url,
NoticeWebActivity.NOTICE_ARTICLE_ID to articleId,
NoticeWebActivity.NOTICE_CATEGORY to category,
NoticeWebActivity.NOTICE_POSTED_DATE to postedDate,
NoticeWebActivity.NOTICE_SUBJECT to subject
)
putExtra(
NoticeWebActivity.WEB_VIEW_NOTICE,
WebViewNotice(url, articleId, category),
)
}
activity.startActivity(intent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ShareCompat
import androidx.lifecycle.lifecycleScope
import com.ku_stacks.ku_ring.R
import com.ku_stacks.ku_ring.data.mapper.concatSubjectAndTag
import com.ku_stacks.ku_ring.data.mapper.toWebViewNotice
import com.ku_stacks.ku_ring.data.model.Notice
import com.ku_stacks.ku_ring.data.model.WebViewNotice
import com.ku_stacks.ku_ring.databinding.ActivityNoticeWebBinding
import com.ku_stacks.ku_ring.ui.my_notification.ui_model.PushContentUiModel
import com.ku_stacks.ku_ring.util.WordConverter
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import timber.log.Timber
Expand All @@ -32,16 +32,15 @@ class NoticeWebActivity : AppCompatActivity() {
binding = ActivityNoticeWebBinding.inflate(layoutInflater)
setContentView(binding.root)

val url = intent.getStringExtra(NOTICE_URL)
?: throw IllegalStateException("Web Link should not be null.")
val articleId = intent.getStringExtra(NOTICE_ARTICLE_ID)
val category = intent.getStringExtra(NOTICE_CATEGORY)
Timber.e("notice url : $url")
// Deprecated되지 않은 다른 함수가 API 33 이상에서만 사용할 수 있어서 부득이하게 deprecated 함수를 사용
val webViewNotice = intent.getSerializableExtra(WEB_VIEW_NOTICE) as? WebViewNotice
?: throw IllegalStateException("WebViewNotice should not be null.")
Timber.e("web view notice: $webViewNotice")

binding.noticeBackBt.setOnClickListener { finish() }

binding.noticeShareBt.setOnClickListener {
shareLinkExternally(url)
shareLinkExternally(webViewNotice.url)
}

binding.noticeSaveButton.setOnClickListener { viewModel.onSaveButtonClick() }
Expand Down Expand Up @@ -75,7 +74,7 @@ class NoticeWebActivity : AppCompatActivity() {
override fun onProgressChanged(view: WebView, newProgress: Int) {
binding.noticeProgressbar.progress = newProgress
if (newProgress == 100) {
updateNoticeTobeRead(articleId, category)
updateNoticeTobeRead(webViewNotice.articleId, webViewNotice.category)
binding.noticeProgressbar.visibility = View.GONE
binding.noticeWebView.webChromeClient = null
} else {
Expand All @@ -85,7 +84,7 @@ class NoticeWebActivity : AppCompatActivity() {
}
}

binding.noticeWebView.loadUrl(url)
binding.noticeWebView.loadUrl(webViewNotice.url)
}

private fun collectSavedStatus() {
Expand Down Expand Up @@ -118,50 +117,30 @@ class NoticeWebActivity : AppCompatActivity() {
}

companion object {
const val NOTICE_URL = "url"
const val NOTICE_ARTICLE_ID = "articleId"
const val NOTICE_CATEGORY = "category"
const val NOTICE_POSTED_DATE = "postedDate"
const val NOTICE_SUBJECT = "subject"

fun createIntent(context: Context, notice: Notice) = createIntent(
context,
notice.url,
notice.articleId,
notice.category,
notice.postedDate,
concatSubjectAndTag(notice.subject, notice.tag)
)

fun createIntent(context: Context, pushContent: PushContentUiModel): Intent {
with(pushContent) {
val categoryEng = WordConverter.convertKoreanToEnglish(categoryKor)
return createIntent(
context,
fullUrl,
articleId,
categoryEng,
postedDate,
concatSubjectAndTag(subject, tag)
)
}
}
const val WEB_VIEW_NOTICE = "webview_notice"

fun createIntent(context: Context, notice: Notice) =
createIntent(context, notice.toWebViewNotice())

fun createIntent(context: Context, pushContent: PushContentUiModel) =
createIntent(context, pushContent.toWebViewNotice())

fun createIntent(
context: Context,
url: String?,
articleId: String?,
category: String?,
postedDate: String?,
subject: String?
) = Intent(context, NoticeWebActivity::class.java).apply {
Timber.e("url: $url, category: $category")

putExtra(NOTICE_URL, url)
putExtra(NOTICE_ARTICLE_ID, articleId)
putExtra(NOTICE_CATEGORY, category)
putExtra(NOTICE_POSTED_DATE, postedDate)
putExtra(NOTICE_SUBJECT, subject)
): Intent {
if (url == null || articleId == null || category == null) {
throw IllegalArgumentException("intent parameters shouldn't be null: $url, $articleId, $category")
}
return createIntent(context, WebViewNotice(url, articleId, category))
}

fun createIntent(context: Context, webViewNotice: WebViewNotice) =
Intent(context, NoticeWebActivity::class.java).apply {
Timber.d("WebViewNotice: $webViewNotice")
putExtra(WEB_VIEW_NOTICE, webViewNotice)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.ku_stacks.ku_ring.ui.notice_webview
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ku_stacks.ku_ring.data.model.WebViewNotice
import com.ku_stacks.ku_ring.repository.NoticeRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import io.reactivex.rxjava3.disposables.CompositeDisposable
Expand All @@ -20,12 +21,8 @@ class NoticeWebViewModel @Inject constructor(
) : ViewModel() {

private val disposable = CompositeDisposable()

private val articleId = savedStateHandle.getString(NoticeWebActivity.NOTICE_ARTICLE_ID)
private val category = savedStateHandle.getString(NoticeWebActivity.NOTICE_CATEGORY)
private val url = savedStateHandle.getString(NoticeWebActivity.NOTICE_URL)
private val postedDate = savedStateHandle.getString(NoticeWebActivity.NOTICE_POSTED_DATE)
private val subject = savedStateHandle.getString(NoticeWebActivity.NOTICE_SUBJECT)
private val webViewNotice =
savedStateHandle.get(NoticeWebActivity.WEB_VIEW_NOTICE) as? WebViewNotice

private val _isSaved = MutableStateFlow(false)
val isSaved: StateFlow<Boolean>
Expand All @@ -34,7 +31,7 @@ class NoticeWebViewModel @Inject constructor(
init {
viewModelScope.launch {
noticeRepository.getSavedNotices().collect { savedNotices ->
_isSaved.value = savedNotices.any { it.articleId == articleId }
_isSaved.value = savedNotices.any { it.articleId == webViewNotice?.articleId }
}
}
}
Expand All @@ -52,10 +49,14 @@ class NoticeWebViewModel @Inject constructor(
}

fun onSaveButtonClick() {
Timber.e("Save button click: $articleId, $category, $url, $postedDate, $subject")
if (articleId == null || category == null || url == null || postedDate == null || subject == null) return
Timber.e("Save button click: $webViewNotice")
if (webViewNotice == null) return
viewModelScope.launch {
noticeRepository.updateSavedStatus(articleId, category, !isSaved.value)
noticeRepository.updateSavedStatus(
webViewNotice.articleId,
webViewNotice.category,
!isSaved.value
)
}
}

Expand All @@ -66,6 +67,4 @@ class NoticeWebViewModel @Inject constructor(
disposable.dispose()
}
}

private fun SavedStateHandle.getString(key: String) = get<String>(key)
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ class SplashActivity : AppCompatActivity() {
url = fullUrl,
articleId = articleId,
category = category,
postedDate = postedDate,
subject = subject
)
}
}
Expand Down

0 comments on commit 955a29a

Please sign in to comment.