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

add error-handlig and log #23

Merged
merged 1 commit into from
Nov 22, 2022
Merged
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,12 @@ import okhttp3.RequestBody.Companion.toRequestBody
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import java.io.IOException

class ConvertRepositoryImpl : ConvertRepository {

private val tag = ConvertRepositoryImpl::class.java.simpleName

// JSONからKotlinのクラスに変換するためのライブラリの設定
private val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
Expand All @@ -34,21 +37,28 @@ class ConvertRepositoryImpl : ConvertRepository {
type: String,
appId: String
): Response<ResponseData>? {
return try {
try {
val requestData = RequestData(
appId = appId,
sentence = sentence,
outputType = type
)
val json = moshi.adapter(RequestData::class.java).toJson(requestData)
Log.i("json: ", json)
Log.i(tag, "json: $json")
val body = json.toRequestBody(
contentType = "application/json; charset=utf-8".toMediaTypeOrNull()
)
convertService.requestConvert(body)
val response = convertService.requestConvert(body)
if (response.isSuccessful.not()) {
Log.w(tag, response.raw().message)
}
return response
} catch (ioe: IOException) {
Log.e(tag, "ネットワークエラー: $ioe")
return null
} catch (e: Exception) {
Log.e("request_convert", e.toString())
null
Log.e(tag, e.toString())
return null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import javax.inject.Inject
@AndroidEntryPoint
class MainActivity : ComponentActivity() {

private val tag = MainActivity::class.java.simpleName

@Inject lateinit var preferencesDataStore: DataStore<Preferences>

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -32,7 +34,7 @@ class MainActivity : ComponentActivity() {
setContent {
val themeNum: State<Int> = preferencesDataStore.data
.catch { exception ->
Log.e("preferencesDataStore", exception.toString())
Log.e(tag, "preferencesDataStore $exception")
if (exception is IOException) {
emit(emptyPreferences())
} else {
Expand All @@ -46,7 +48,7 @@ class MainActivity : ComponentActivity() {

val customFont: State<String> = preferencesDataStore.data
.catch { exception ->
Log.e("preferencesDataStore", exception.toString())
Log.e(tag, "preferencesDataStore $exception")
if (exception is IOException) {
emit(emptyPreferences())
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class ConvertViewModelImpl @Inject constructor(
private val sharedPreferences: SharedPreferences
) : ConvertViewModel() {

private val tag = ConvertViewModelImpl::class.java.simpleName

override val previousInputText: MutableState<String> = mutableStateOf("")
override val inputText: MutableState<String> = mutableStateOf("")
override val outputText: MutableState<String> = mutableStateOf("")
Expand All @@ -41,23 +43,27 @@ class ConvertViewModelImpl @Inject constructor(
errorText.value = context.getString(R.string.limit_local_count)
} else {
val appId = BuildConfig.apiKey
val nullRawErrorText = context.getString(R.string.conversion_failed)
CoroutineScope(Dispatchers.IO).launch {
raw.value = convertRepository.requestConvert(
sentence = inputText.value,
type = selectedTextType.value.name.lowercase(Locale.ENGLISH),
appId = appId
)
if (raw.value == null) {
errorText.value = nullRawErrorText
}
outputText.value = raw.value?.body()?.converted ?: ""
previousInputText.value = inputText.value
}
}
}

override fun updateErrorText(context: Context) {
Log.i("raw", raw.value?.raw().toString())
Log.i(tag, "raw: ${raw.value?.raw()}")
when (raw.value?.code()) {
null -> {
errorText.value = ""
errorText.value = "" // 初期化時しか通らない
}
200 -> {
errorText.value = ""
Expand All @@ -79,23 +85,24 @@ class ConvertViewModelImpl @Inject constructor(
errorText.value = context.getString(R.string.conversion_failed)
}
}
Log.i(tag, "errorText: ${errorText.value}")
}

/**
* 制限回数(1日あたり200回)に達していることを確認する処理
*/
private fun limitIsReached(): Boolean {
val lastSearchTime = sharedPreferences.getString("last_search_time", "")
Log.i(tag, "last_search_time: $lastSearchTime")
val now = DateFormat.format("yyyy-MM-dd", Calendar.getInstance()).toString()
val todayCount = if (lastSearchTime != now) {
1
} else {
sharedPreferences.getInt("search_count", 0) + 1
}
sharedPreferences.edit().putInt("search_count", todayCount).apply()
Log.i("search_count", todayCount.toString())
Log.i(tag, "today_search_count: $todayCount")
sharedPreferences.edit().putString("last_search_time", now).apply()
Log.i("last_search_time", now)
return todayCount > 200
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class SettingsViewModelImpl @Inject constructor(
private val preferencesDataStore: DataStore<Preferences>
) : SettingsViewModel() {

private val tag = SettingsViewModelImpl::class.java.simpleName

private val _themeNum = mutableStateOf(ThemeNum.AUTO.num)
override val themeNum: Int = _themeNum.value
private val _customFont = mutableStateOf(CustomFont.DEFAULT.name)
Expand All @@ -47,7 +49,7 @@ class SettingsViewModelImpl @Inject constructor(
CoroutineScope(Dispatchers.IO).launch {
val customFontFlow: Flow<Int> = preferencesDataStore.data
.catch { exception ->
Log.e("preferencesDataStore", exception.toString())
Log.e(tag, "preferencesDataStore $exception")
if (exception is IOException) {
emit(emptyPreferences())
} else {
Expand All @@ -67,7 +69,7 @@ class SettingsViewModelImpl @Inject constructor(
CoroutineScope(Dispatchers.IO).launch {
val customFontFlow: Flow<String> = preferencesDataStore.data
.catch { exception ->
Log.e("preferencesDataStore", exception.toString())
Log.e(tag, "preferencesDataStore $exception")
if (exception is IOException) {
emit(emptyPreferences())
} else {
Expand Down