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

Convert WebItem, Item and Favorite from java to Kotlin #1154

Open
wants to merge 3 commits into
base: master
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
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,11 @@ private void bindData(@Nullable final WebItem story) {
postedTextView.append(story.getDisplayedAuthor(this, true, 0));
postedTextView.setMovementMethod(LinkMovementMethod.getInstance());
switch (story.getType()) {
case Item.JOB_TYPE:
case WebItem.JOB_TYPE:
postedTextView.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.ic_work_white_18dp, 0, 0, 0);
break;
case Item.POLL_TYPE:
case WebItem.POLL_TYPE:
postedTextView.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.ic_poll_white_18dp, 0, 0, 0);
break;
Expand Down
155 changes: 0 additions & 155 deletions app/src/main/java/io/github/hidroh/materialistic/data/Favorite.java

This file was deleted.

86 changes: 86 additions & 0 deletions app/src/main/java/io/github/hidroh/materialistic/data/Favorite.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2016 Ha Duy Trung
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.github.hidroh.materialistic.data

import android.content.Context
import android.net.Uri
import android.os.Parcel
import android.os.Parcelable
import android.text.Spannable
import android.text.SpannableString
import io.github.hidroh.materialistic.AppUtils
import io.github.hidroh.materialistic.R
import io.github.hidroh.materialistic.data.WebItem.Companion.STORY_TYPE

open class Favorite(override val id: String, override val url: String?, override val displayedTitle: String, val time: Long) : WebItem {
override var isFavorite: Boolean = true
private val displayedAuthor = SpannableString("")
private var displayedTime: Spannable? = null

constructor(parcel: Parcel) : this(parcel.readString()!!, parcel.readString()!!, parcel.readString()!!, parcel.readLong()) {
isFavorite = parcel.readInt() != 0
}

override val isStoryType: Boolean
get() = true
override val longId: Long
get() = id.toLong()
override val source: String?
get() = if (url.isNullOrEmpty()) null else Uri.parse(url).host
override val type: String
// TODO treating all saved items as stories for now
get() = STORY_TYPE


override fun getDisplayedAuthor(context: Context, linkify: Boolean, color: Int): Spannable {
return displayedAuthor
}

override fun getDisplayedTime(context: Context): Spannable {
if (displayedTime == null) {
displayedTime = SpannableString(context.getString(R.string.saved, AppUtils.getAbbreviatedTimeSpan(time)))
}
return displayedTime!!
}


override fun describeContents(): Int {
return 0
}

override fun writeToParcel(dest: Parcel, flags: Int) {
dest.writeString(id)
dest.writeString(url)
dest.writeString(displayedTitle)
dest.writeLong(time)
dest.writeInt(if (isFavorite) 1 else 0)
}

companion object {
@JvmField
val CREATOR: Parcelable.Creator<Favorite> = object : Parcelable.Creator<Favorite> {
override fun createFromParcel(source: Parcel): Favorite {
return Favorite(source)
}

override fun newArray(size: Int): Array<Favorite?> {
return arrayOfNulls(size)
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void getItem(final String itemId, @CacheMode int cacheMode, ResponseListe
(isViewed, favorite, hackerNewsItem) -> {
if (hackerNewsItem != null) {
hackerNewsItem.preload();
hackerNewsItem.setIsViewed(isViewed);
hackerNewsItem.setViewed(isViewed);
hackerNewsItem.setFavorite(favorite);
}
return hackerNewsItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(previous);
}

@NonNull
@Override
public String getId() {
return String.valueOf(id);
Expand All @@ -250,11 +251,11 @@ public String getTitle() {
@Override
public String getDisplayedTitle() {
switch (getType()) {
case COMMENT_TYPE:
case WebItem.COMMENT_TYPE:
return text;
case JOB_TYPE:
case STORY_TYPE:
case POLL_TYPE: // TODO poll need to display options
case WebItem.JOB_TYPE:
case WebItem.STORY_TYPE:
case WebItem.POLL_TYPE: // TODO poll need to display options
default:
return title;
}
Expand All @@ -263,11 +264,12 @@ public String getDisplayedTitle() {
@NonNull
@Override
public String getType() {
return !TextUtils.isEmpty(type) ? type : STORY_TYPE;
return !TextUtils.isEmpty(type) ? type : WebItem.STORY_TYPE;
}

@NonNull
@Override
public Spannable getDisplayedAuthor(Context context, boolean linkify, int color) {
public Spannable getDisplayedAuthor(@NonNull Context context, boolean linkify, int color) {
if (displayedAuthor == null) {
if (TextUtils.isEmpty(by)) {
displayedAuthor = new SpannableString("");
Expand All @@ -286,8 +288,9 @@ public Spannable getDisplayedAuthor(Context context, boolean linkify, int color)
return displayedAuthor;
}

@NonNull
@Override
public Spannable getDisplayedTime(Context context) {
public Spannable getDisplayedTime(@NonNull Context context) {
if (displayedTime == null) {
SpannableStringBuilder builder = new SpannableStringBuilder(dead ?
context.getString(R.string.dead_prefix) + " " : "");
Expand Down Expand Up @@ -330,9 +333,9 @@ public boolean hasNewKids() {
@Override
public String getUrl() {
switch (getType()) {
case JOB_TYPE:
case POLL_TYPE:
case COMMENT_TYPE:
case WebItem.JOB_TYPE:
case WebItem.POLL_TYPE:
case WebItem.COMMENT_TYPE:
return getItemUrl(getId());
default:
return TextUtils.isEmpty(url) ? getItemUrl(getId()) : url;
Expand All @@ -356,7 +359,7 @@ public void onClick(View view) {
}

@Override
public void updateDrawState(TextPaint ds) {
public void updateDrawState(@NonNull TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
Expand Down Expand Up @@ -416,11 +419,11 @@ public CharSequence getDisplayedText() {
@Override
public boolean isStoryType() {
switch (getType()) {
case STORY_TYPE:
case POLL_TYPE:
case JOB_TYPE:
case WebItem.STORY_TYPE:
case WebItem.POLL_TYPE:
case WebItem.JOB_TYPE:
return true;
case COMMENT_TYPE:
case WebItem.COMMENT_TYPE:
default:
return false;
}
Expand Down Expand Up @@ -457,7 +460,7 @@ public boolean isViewed() {
}

@Override
public void setIsViewed(boolean isViewed) {
public void setViewed(boolean isViewed) {
viewed = isViewed;
}

Expand Down
Loading