diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2017-10-20 18:18:59 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2017-10-20 18:18:59 +0900 |
commit | 91b7a80ae0265fbfb09cdfcb8f7d45a784daefb3 (patch) | |
tree | 567cfd0d89d8a3b74872a1952dfcbe89e211580a /app | |
parent | 3b33fe0ad4172b6a3480b4bd750dd24a77956553 (diff) | |
download | SmileEssence-91b7a80ae0265fbfb09cdfcb8f7d45a784daefb3.tar.gz |
SE42017-10-20
Diffstat (limited to 'app')
-rw-r--r-- | app/src/main/AndroidManifest.xml | 4 | ||||
-rw-r--r-- | app/src/main/java/net/lacolaco/smileessence/World.kt | 8 | ||||
-rw-r--r-- | app/src/main/java/net/lacolaco/smileessence/activity/MainActivity.kt | 6 | ||||
-rw-r--r-- | app/src/main/java/net/lacolaco/smileessence/activity/ManageProfilesActivity.kt (renamed from app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.kt) | 108 | ||||
-rw-r--r-- | app/src/main/java/net/lacolaco/smileessence/data/DbHelper.kt | 32 | ||||
-rw-r--r-- | app/src/main/java/net/lacolaco/smileessence/data/ExtractionWord.kt | 1 | ||||
-rw-r--r-- | app/src/main/res/layout/activity_manage_profiles.xml (renamed from app/src/main/res/layout/layout_manage_accounts.xml) | 7 | ||||
-rw-r--r-- | app/src/main/res/layout/list_item_profile.xml (renamed from app/src/main/res/layout/list_item_account.xml) | 18 | ||||
-rw-r--r-- | app/src/main/res/values/strings.xml | 3 |
9 files changed, 89 insertions, 98 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index caca3a96..0bebec79 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -13,7 +13,7 @@ android:label="@string/app_name" android:theme="@style/theme_light"> <activity - android:name=".activity.ManageAccountsActivity" + android:name=".activity.ManageProfilesActivity" android:configChanges="keyboardHidden|orientation" android:excludeFromRecents="true" android:label="@string/app_name" @@ -113,7 +113,7 @@ android:configChanges="keyboardHidden|orientation" android:excludeFromRecents="false" android:label="@string/activity_authenticate" - android:parentActivityName=".activity.ManageAccountsActivity" /> + android:parentActivityName=".activity.ManageProfilesActivity" /> <activity android:name=".activity.ManagePagesActivity" android:label="@string/title_activity_page_manage" /> diff --git a/app/src/main/java/net/lacolaco/smileessence/World.kt b/app/src/main/java/net/lacolaco/smileessence/World.kt index ed2bddad..c5377232 100644 --- a/app/src/main/java/net/lacolaco/smileessence/World.kt +++ b/app/src/main/java/net/lacolaco/smileessence/World.kt @@ -27,7 +27,7 @@ import java.util.* import java.util.concurrent.ConcurrentHashMap /** - * World contains data that are specific to an account. + * World contains data that are specific to a profile. */ class World private constructor(private val persistentData: PersistentData) { val id = persistentData.id @@ -273,7 +273,7 @@ class World private constructor(private val persistentData: PersistentData) { private val cache = LinkedHashMap<Long, World>() operator fun get(i: Long) = - cache[i] ?: throw IllegalStateException("[BUG] Account with id == $i not found") + cache[i] ?: throw IllegalStateException("[BUG] World with id == $i not found") fun all() = ArrayList(cache.values) @@ -312,7 +312,7 @@ class World private constructor(private val persistentData: PersistentData) { values.put("page_infos", PageInfo.stringifyList(pageInfos)) values.put("theme", themeIndex) values.put("theme_color", themeColor) - if (DbHelper.db.replaceOrThrow("accounts", null, values) == -1L) + if (DbHelper.db.replaceOrThrow("profiles", null, values) == -1L) throw RuntimeException("SQLiteDatabase#replaceOrThrow failed") } @@ -331,7 +331,7 @@ class World private constructor(private val persistentData: PersistentData) { fun fetchAll(): List<PersistentData> { val ret = arrayListOf<PersistentData>() - DbHelper.db.query("accounts", null, null, null, null, null, null).use { cursor -> + DbHelper.db.query("profiles", null, null, null, null, null, null).use { cursor -> while (cursor != null && cursor.moveToNext()) { val pd = PersistentData( id = cursor.getLong( diff --git a/app/src/main/java/net/lacolaco/smileessence/activity/MainActivity.kt b/app/src/main/java/net/lacolaco/smileessence/activity/MainActivity.kt index 631350eb..4edb2aca 100644 --- a/app/src/main/java/net/lacolaco/smileessence/activity/MainActivity.kt +++ b/app/src/main/java/net/lacolaco/smileessence/activity/MainActivity.kt @@ -270,8 +270,8 @@ class MainActivity : AppCompatActivity() { override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { android.R.id.home -> { - val intent = Intent(this, ManageAccountsActivity::class.java) - intent.putExtra(ManageAccountsActivity.INTENT_KEY_NOINIT, true) + val intent = Intent(this, ManageProfilesActivity::class.java) + intent.putExtra(ManageProfilesActivity.INTENT_KEY_NOINIT, true) startActivity(intent) } R.id.actionbar_setting -> startActivity(Intent(this, SettingActivity::class.java)) @@ -303,7 +303,7 @@ class MainActivity : AppCompatActivity() { // properly when resuming an already running Activity that has configured as // documentLaunchMode=intoExisting. So, as a workaround, store the actual Intent in // the World associated and check it in onResume() for now. See also - // ManageAccountsActivity::goToWorld(). + // ManageProfilesActivity::goToWorld(). val intent = world.mainActivityIntent if (intent != null) { world.mainActivityIntent = null diff --git a/app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.kt b/app/src/main/java/net/lacolaco/smileessence/activity/ManageProfilesActivity.kt index 8acbe2c3..3c4c50c9 100644 --- a/app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.kt +++ b/app/src/main/java/net/lacolaco/smileessence/activity/ManageProfilesActivity.kt @@ -13,26 +13,27 @@ import android.support.v4.content.pm.ShortcutInfoCompat import android.support.v4.content.pm.ShortcutManagerCompat import android.support.v4.graphics.drawable.IconCompat import android.support.v7.app.AppCompatActivity +import android.support.v7.widget.DividerItemDecoration +import android.support.v7.widget.LinearLayoutManager +import android.support.v7.widget.RecyclerView import android.view.Menu import android.view.MenuItem import android.view.View import android.view.ViewGroup -import android.widget.BaseAdapter import android.widget.ImageView import com.android.volley.toolbox.ImageRequest -import kotlinx.android.synthetic.main.layout_manage_accounts.* -import kotlinx.android.synthetic.main.list_item_account.view.* +import kotlinx.android.synthetic.main.activity_manage_profiles.* +import kotlinx.android.synthetic.main.list_item_profile.view.* import net.lacolaco.smileessence.Application import net.lacolaco.smileessence.R import net.lacolaco.smileessence.World import net.lacolaco.smileessence.data.ImageCache -import net.lacolaco.smileessence.util.launchBg +import net.lacolaco.smileessence.util.bg import net.lacolaco.smileessence.util.launchUi import net.lacolaco.smileessence.view.confirm -import java.util.* -class ManageAccountsActivity : AppCompatActivity() { - private val adapter by lazy { EditAccountsAdapter() } +class ManageProfilesActivity : AppCompatActivity() { + private lateinit var adapter: ProfilesAdapter private lateinit var receivedIntent: Intent override fun onCreate(savedInstanceState: Bundle?) { @@ -54,14 +55,13 @@ class ManageAccountsActivity : AppCompatActivity() { } } - setContentView(R.layout.layout_manage_accounts) + setContentView(R.layout.activity_manage_profiles) setSupportActionBar(toolbar) - listview_edit_list.adapter = adapter - listview_edit_list.setOnItemClickListener { _, _, i, _ -> - val world = adapter.getItem(i) - goToWorld(World[world.id]) - } + profiles.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL)) + profiles.layoutManager = LinearLayoutManager(this) + adapter = ProfilesAdapter(World.all()) + profiles.adapter = adapter } override fun onNewIntent(intent: Intent) { @@ -126,48 +126,51 @@ class ManageAccountsActivity : AppCompatActivity() { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { assert(requestCode == REQUEST_OAUTH) if (resultCode == RESULT_OK) { - launchBg { + launchUi { data!! - adapter.add(World.register( - data.getStringExtra(OAuthActivity.KEY_TOKEN), - data.getStringExtra(OAuthActivity.KEY_TOKEN_SECRET), - data.getLongExtra(OAuthActivity.KEY_USER_ID, -1L), - data.getStringExtra(OAuthActivity.KEY_SCREEN_NAME))) - launchUi { adapter.notifyDataSetChanged() } + val world = bg { + World.register( + data.getStringExtra(OAuthActivity.KEY_TOKEN), + data.getStringExtra(OAuthActivity.KEY_TOKEN_SECRET), + data.getLongExtra(OAuthActivity.KEY_USER_ID, -1L), + data.getStringExtra(OAuthActivity.KEY_SCREEN_NAME)) + }.await() + adapter.add(world) } } } - private inner class EditAccountsAdapter : BaseAdapter() { - private val worlds: MutableList<World> = ArrayList(World.all()) - - override fun getCount(): Int = worlds.size - - override fun getItem(position: Int) = worlds[position] + private inner class ProfilesAdapter(initialList: Collection<World>) : + RecyclerView.Adapter<ProfilesAdapter.ViewHolder>() { + private val profiles = ArrayList(initialList) - override fun getItemId(position: Int): Long = worlds[position].id + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + return ViewHolder(layoutInflater.inflate(R.layout.list_item_profile, parent, false)) + } - override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { - val view = convertView ?: layoutInflater.inflate(R.layout.list_item_account, parent, - false) - val world = getItem(position) + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + val world = profiles[position] + val view = holder.itemView + view.setOnClickListener { + goToWorld(world) + } if (world.themeColor != -1) - view.account_accent_color_view.setBackgroundColor(world.themeColor) + view.profile_accent_color_view.setBackgroundColor(world.themeColor) else - view.account_accent_color_view.setBackgroundColor(Color.TRANSPARENT) - view.account_icon.setImageUrl(world.user.profileImageUrl, ImageCache.getImageLoader()) - view.account_text_view.text = "@${world.user.screenName}" - view.account_action_menu.clear() - view.account_action_menu.add("Remove") { - confirm(R.string.dialog_confirm_clear_account, world.user.screenName) { - worlds.removeAt(position) + view.profile_accent_color_view.setBackgroundColor(Color.TRANSPARENT) + view.profile_user_icon.setImageUrl(world.user.profileImageUrl, ImageCache.getImageLoader()) + view.profile_name.text = "@${world.user.screenName}" + view.profile_action_menu.clear() + view.profile_action_menu.add("Remove") { + confirm(R.string.dialog_confirm_remove_profile, world.user.screenName) { + profiles.removeAt(position) notifyDataSetChanged() // TODO: Account.unregister(account.id) // TODO: Stop the world } } - view.account_action_menu.add("Add to homescreen") { + view.profile_action_menu.add("Add to homescreen") { val data = Uri.parse("smileessence://mainactivity/?user_id=" + world.id) val shortcutIntent = Intent(Intent.ACTION_VIEW, data) @@ -175,26 +178,33 @@ class ManageAccountsActivity : AppCompatActivity() { if (isDestroyed) return@ImageRequest val id = "mainactivity-${world.id}" - val shortcutInfo = ShortcutInfoCompat.Builder(this@ManageAccountsActivity, id) + val shortcutInfo = ShortcutInfoCompat.Builder(this@ManageProfilesActivity, id) .setShortLabel("@${world.user.screenName}") .setIcon(IconCompat.createWithAdaptiveBitmap(b)) .setIntent(shortcutIntent) .build() - ShortcutManagerCompat.requestPinShortcut(this@ManageAccountsActivity, shortcutInfo, null) + ShortcutManagerCompat.requestPinShortcut(this@ManageProfilesActivity, shortcutInfo, null) }, 0, 0, ImageView.ScaleType.CENTER, Bitmap.Config.ARGB_4444, { error -> error.printStackTrace() })) } - return view } - fun add(world: World): Int = if (!worlds.contains(world)) { - worlds.add(world) - notifyDataSetChanged() - worlds.size - 1 - } else { - worlds.indexOf(world) + override fun getItemCount(): Int { + return profiles.size } + + fun add(item: World) { + if (profiles.indexOf(item) != -1) + notifyItemChanged(profiles.indexOf(item)) + else { + val pos = profiles.size + profiles.add(item) + notifyItemInserted(pos) + } + } + + private inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) } companion object { diff --git a/app/src/main/java/net/lacolaco/smileessence/data/DbHelper.kt b/app/src/main/java/net/lacolaco/smileessence/data/DbHelper.kt index eb1b3a73..38d76bd6 100644 --- a/app/src/main/java/net/lacolaco/smileessence/data/DbHelper.kt +++ b/app/src/main/java/net/lacolaco/smileessence/data/DbHelper.kt @@ -9,46 +9,28 @@ class DbHelper private constructor(context: Context) : override fun onCreate(db: SQLiteDatabase) { // Schema v1 db.execSQL(""" - CREATE TABLE accounts ( + CREATE TABLE profiles ( id INTEGER PRIMARY KEY, oauth_token TEXT NOT NULL, oauth_token_secret TEXT NOT NULL, screen_name TEXT NOT NULL, profile_image_url TEXT NOT NULL, - theme INTEGER NOT NULL + theme INTEGER NOT NULL, + theme_color INTEGER NOT NULL, + page_infos TEXT NOT NULL ) """) onUpgrade(db, 1, DATABASE_VERSION) } override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { - if (oldVersion < 3) { - // Add accounts.themeColor - db.execSQL(""" - ALTER TABLE accounts ADD COLUMN theme_color INTEGER NOT NULL DEFAULT 0 - """) - } - - if (oldVersion < 4) { - // Add extraction_words - db.execSQL(""" - CREATE TABLE extraction_words ( - pattern_string TEXT PRIMARY KEY - ) - """) - } - - if (oldVersion < 5) { - // Add accounts.page_infos - db.execSQL(""" - ALTER TABLE accounts ADD COLUMN page_infos TEXT NOT NULL DEFAULT "[]" - """) + if (oldVersion < 1) { } } companion object { private val DATABASE_NAME = "main.db" - private val DATABASE_VERSION = 5 + private val DATABASE_VERSION = 1 // Lifetime is same as Application private lateinit var instance: DbHelper @@ -58,6 +40,6 @@ class DbHelper private constructor(context: Context) : } val db - get() = instance.writableDatabase + get() = instance.writableDatabase!! } } diff --git a/app/src/main/java/net/lacolaco/smileessence/data/ExtractionWord.kt b/app/src/main/java/net/lacolaco/smileessence/data/ExtractionWord.kt index 93d13dd6..5f533112 100644 --- a/app/src/main/java/net/lacolaco/smileessence/data/ExtractionWord.kt +++ b/app/src/main/java/net/lacolaco/smileessence/data/ExtractionWord.kt @@ -3,6 +3,7 @@ package net.lacolaco.smileessence.data import android.content.ContentValues import java.util.regex.Pattern +@Deprecated("Remove when ManagePagesActivity is done") class ExtractionWord private constructor(val patternString: String) { val pattern: Pattern by lazy { Pattern.compile(patternString) diff --git a/app/src/main/res/layout/layout_manage_accounts.xml b/app/src/main/res/layout/activity_manage_profiles.xml index e8aca0f7..81f2606e 100644 --- a/app/src/main/res/layout/layout_manage_accounts.xml +++ b/app/src/main/res/layout/activity_manage_profiles.xml @@ -10,9 +10,8 @@ android:background="?attr/colorPrimary" android:elevation="4dp" /> - <ListView - android:id="@+id/listview_edit_list" + <android.support.v7.widget.RecyclerView + android:id="@+id/profiles" android:layout_width="match_parent" - android:layout_height="match_parent" - android:fastScrollEnabled="true" /> + android:layout_height="match_parent" /> </LinearLayout> diff --git a/app/src/main/res/layout/list_item_account.xml b/app/src/main/res/layout/list_item_profile.xml index 14fc2ac2..ebbba597 100644 --- a/app/src/main/res/layout/list_item_account.xml +++ b/app/src/main/res/layout/list_item_profile.xml @@ -1,31 +1,31 @@ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" - android:layout_height="match_parent" + android:layout_height="wrap_content" android:descendantFocusability="blocksDescendants"> <FrameLayout - android:id="@+id/account_accent_color_view" + android:id="@+id/profile_accent_color_view" android:layout_width="3dp" android:layout_height="match_parent" /> <com.android.volley.toolbox.NetworkImageView - android:id="@+id/account_icon" - android:layout_width="48dp" - android:layout_height="48dp" - android:layout_margin="8dp" + android:id="@+id/profile_user_icon" + android:layout_width="40dp" + android:layout_height="40dp" + android:layout_margin="5dp" android:scaleType="fitCenter" /> <TextView - android:id="@+id/account_text_view" + android:id="@+id/profile_name" android:layout_width="0dp" android:layout_height="wrap_content" - android:layout_margin="8dp" + android:layout_margin="5dp" android:layout_weight="1" android:layout_gravity="center_vertical" android:textSize="20sp" /> <net.lacolaco.smileessence.view.ActionMenuView - android:id="@+id/account_action_menu" + android:id="@+id/profile_action_menu" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 48d16bbb..7ef10eeb 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -50,7 +50,7 @@ <string name="dialog_title_add">Add</string> <string name="dialog_confirm_finish_app">OK to close?</string> <string name="dialog_confirm_commands">Execute, OK?</string> - <string name="dialog_confirm_clear_account">Are you sure you want to remove this account @%s?</string> + <string name="dialog_confirm_remove_profile">Are you sure you want to remove the profile for @%s?</string> <string name="dialog_confirm_delete_query">Delete this query, OK?</string> <string name="notice_favorite_succeeded">Succeeded to favorite</string> <string name="notice_favorite_failed">Failed to favorite</string> @@ -108,7 +108,6 @@ <string name="command_status_add_to_reply">Add to reply</string> <string name="notice_error_get_user_timeline">Failed to get user timeline</string> <string name="notice_error_get_list">Failed to get list timeline</string> - <string name="notice_cant_remove_last_account">You can\'t remove last account</string> <string name="notice_error_storage_permission">Write access to external storage is required.</string> <string name="title_activity_page_manage">PageManageActivity</string> </resources> |