Compare commits

..

76 Commits

Author SHA1 Message Date
c7d1a42d5a add View root button. (#212) 2023-04-16 22:55:23 -07:00
f83fd66bcc fix onboarding flow. (#211) 2023-04-16 20:21:48 -07:00
c2ec3647e2 bump Flutter version and add Noto Serif font. (#210) 2023-04-16 19:42:18 -07:00
ba63852b7d update tablet view. (#207) 2023-04-11 21:12:48 -07:00
438041183c fix in app review for android. (#206) 2023-04-11 19:09:35 -07:00
114540edd7 cleanup. (#205) 2023-04-11 18:30:03 -07:00
588b3e9508 fix reply box. (#204) 2023-04-11 13:23:11 -07:00
2f0376f8f8 add shortcuts to jump to previous or next root level comment. (#203) 2023-04-11 00:05:56 -07:00
ab4051c018 remove scroll bar. (#202) 2023-04-09 19:47:30 -07:00
c230c21218 update scrollbar. (#201) 2023-04-09 18:35:40 -07:00
c24e12237e fix status bar for android. (#200) 2023-04-09 17:55:29 -07:00
e15dcba93b update dev link. (#197) 2023-04-06 12:24:41 -07:00
1362b93a74 update pubspec file. (#196) 2023-04-06 11:07:02 -07:00
ac18793f98 bump flutter version. (#195) 2023-04-06 09:54:21 -07:00
e52f65c773 update tap target of story tile. (#194) 2023-04-04 15:31:50 -07:00
06212a0d72 fix auth for user with no activity. (#191) 2023-04-01 10:06:32 -07:00
e77c0e3e73 update bottom sheet. (#190) 2023-03-31 23:15:53 -07:00
cb6f41ec49 add keyword filter. (#189) 2023-03-31 13:59:12 -07:00
ab1e90ccad fix comment tile and bottom navigation bar. (#187) 2023-03-26 19:16:38 -07:00
0ca3e96d91 update story tile. (#183) 2023-03-02 18:36:23 -08:00
d1c8eed3de bump flutter version. (#182) 2023-03-02 00:29:43 -08:00
aa6a2c684c bugfixes. (#181) 2023-03-01 12:24:16 -08:00
d4778d9530 remove bottom padding. (#178) 2023-02-28 15:29:03 -08:00
c702e08481 allow exporting favorites to clipboard. (#177) 2023-02-28 14:54:51 -08:00
2af10391bc update story tile. (#175) 2023-02-27 16:48:53 -08:00
c420dd3ca4 correct spelling. (#174) 2023-02-27 15:16:10 -08:00
da7d0757cd add link to privacy policy. (#173) 2023-02-27 14:48:49 -08:00
32ae2087bc fix link button. (#171) 2023-02-26 23:03:48 -08:00
0b5329d050 bugfixes. (#170) 2023-02-26 15:08:18 -08:00
c375def289 bugfixes. (#169) 2023-02-26 12:12:11 -08:00
3469543c7b update action menu. (#168) 2023-02-26 02:40:11 -08:00
ab755581fd add favorite to action menu. (#167) 2023-02-25 23:16:55 -08:00
6b75eb8549 bump version. (#165) 2023-02-24 11:41:19 -08:00
36ded8a8e3 improve search experience. (#164) 2023-02-24 10:38:10 -08:00
582ac7b0be fix push notification. (#161) 2023-02-23 23:14:06 -08:00
e5e3391785 bump flutter version. (#160) 2023-02-23 01:04:44 -08:00
9159fe0fe1 add font customization. (#159) 2023-02-22 15:54:01 -08:00
7c51bad35e add shortcuts for wikipedia and wiktionary. (#157) 2023-02-22 13:37:32 -08:00
6836138d11 fix quote rendering. (#158) 2023-02-22 11:30:33 -08:00
2f71964277 linkifier cleanup. (#156) 2023-02-22 00:15:52 -08:00
c24c5c1b7a add formatting support (#155) 2023-02-21 23:40:25 -08:00
755b112382 remove isFirstLaunch val. (#153) 2023-02-12 20:22:38 -08:00
d44b64d249 fix feature discovery. (#152) 2023-02-12 19:39:51 -08:00
35ed917e66 improve onboarding experience. (#151) 2023-02-12 18:49:17 -08:00
15b75ef37c cleanup. (#149) 2023-02-11 19:44:54 -08:00
f39408fbcc fix time machine. (#148) 2023-02-11 04:17:43 -08:00
ca2f063297 update comment tile. (#146) 2023-02-11 02:30:51 -08:00
1ad231adbb fix offline mode. (#145) 2023-02-11 01:33:45 -08:00
60b09fd81e cleanup. (#143) 2023-02-11 00:39:30 -08:00
fe162208ca fix expand animation. (#142) 2023-02-10 14:08:31 -08:00
58139ba7a3 update commit_check.yml (#141) 2023-02-09 15:42:08 -08:00
33a31acbe2 update Fastfile. 2023-02-09 15:20:19 -08:00
0fcfcbb7e3 update Fastfile. (#140) 2023-02-09 15:12:00 -08:00
a98f52c90b update publish_ios.yml 2023-02-09 14:37:11 -08:00
8e8e48c44a update GitHub action. (#139) 2023-02-09 14:28:46 -08:00
603b7cc939 bump flutter to 3.7.3 (#138) 2023-02-09 11:27:03 -08:00
649fa33df3 fix err msg. (#137) 2023-02-09 00:19:34 -08:00
81d4a0f2df banner cleanup. (#136) 2023-02-08 23:44:15 -08:00
24112a471e add collapse/expand animation to comment tile. (#135) 2023-02-08 23:08:09 -08:00
c7824eaef3 bump flutter to 3.7.2 (#134) 2023-02-08 17:43:23 -08:00
c2b66d29c3 add sharing option. (#131) 2023-02-04 18:46:04 -08:00
e0a53e44b2 bump flutter to 3.7.1 (#129) 2023-02-01 15:19:06 -08:00
4cf8379db0 fix Story model. (#128) 2023-01-31 22:02:17 -08:00
c1c26bf0e0 fix preference model. (#127) 2023-01-31 18:19:34 -08:00
29e2f4163d fix offline mode. (#126) 2023-01-31 16:54:28 -08:00
c3de80015d fix PinnedStories (#125) 2023-01-31 16:36:58 -08:00
436cd9ce8b fix Item model. (#123) 2023-01-31 15:56:29 -08:00
efb326be68 refactor models. (#122) 2023-01-30 23:43:12 -08:00
047903fe24 refactor. (#121) 2023-01-30 22:46:29 -08:00
41068ddf89 cleanup. (#120) 2023-01-29 21:34:54 -08:00
196516ce85 fix logger. (#119) 2023-01-29 20:55:46 -08:00
7f647b127d enable swipe gesture. (#118) 2023-01-29 20:03:11 -08:00
a50a0874e7 fix logger. (#117) 2023-01-29 18:46:55 -08:00
b176be96fb Allow customizing tab bar. (#112) 2023-01-29 16:48:08 -08:00
1e5af07691 improve UX. (#110) 2023-01-25 21:47:05 -08:00
ecf8c902dc bump flutter and linter version. (#108) 2023-01-25 12:33:06 -08:00
189 changed files with 8609 additions and 4931 deletions

View File

@ -11,15 +11,13 @@ jobs:
name: Check commit name: Check commit
runs-on: ubuntu-latest runs-on: ubuntu-latest
timeout-minutes: 30 timeout-minutes: 30
env:
FLUTTER_VERSION: "3.3.10"
steps: steps:
- uses: actions/checkout@v2 - name: checkout all the submodules
- uses: subosito/flutter-action@v2 uses: actions/checkout@v3
with: with:
flutter-version: '3.3.10' submodules: recursive
channel: 'stable' - run: submodules/flutter/bin/flutter doctor
- run: flutter pub get - run: submodules/flutter/bin/flutter pub get
- run: flutter format --set-exit-if-changed . - run: submodules/flutter/bin/dart format --set-exit-if-changed lib test integration_test
- run: flutter analyze - run: submodules/flutter/bin/flutter analyze lib test integration_test
- run: flutter test - run: submodules/flutter/bin/flutter test

View File

@ -20,21 +20,21 @@ jobs:
steps: steps:
- name: Check out from git - name: Check out from git
uses: actions/checkout@v2 uses: actions/checkout@v3
with:
submodules: recursive
- run: submodules/flutter/bin/flutter doctor
- run: submodules/flutter/bin/flutter pub get
- run: submodules/flutter/bin/dart format --set-exit-if-changed lib test integration_test
- run: submodules/flutter/bin/flutter analyze lib test integration_test
- run: submodules/flutter/bin/flutter test
# Configure ruby according to our .ruby-version # Configure ruby according to our .ruby-version
- name: Setup ruby & Bundler - name: Setup ruby & Bundler
uses: ruby/setup-ruby@v1 uses: ruby/setup-ruby@v1
with: with:
bundler-cache: true bundler-cache: true
# Set up flutter (feel free to adjust the version below)
- name: Setup flutter
uses: subosito/flutter-action@v2
with:
cache: true
flutter-version: 3.3.10
- run: flutter pub get
- run: flutter format --set-exit-if-changed .
- run: flutter analyze
# Start an ssh-agent that will provide the SSH key from the # Start an ssh-agent that will provide the SSH key from the
# SSH_PRIVATE_KEY secret to `fastlane match` # SSH_PRIVATE_KEY secret to `fastlane match`
- name: Setup SSH key - name: Setup SSH key
@ -43,8 +43,7 @@ jobs:
run: | run: |
ssh-agent -a $SSH_AUTH_SOCK > /dev/null ssh-agent -a $SSH_AUTH_SOCK > /dev/null
ssh-add - <<< "${{ secrets.SSH_PRIVATE_KEY }}" ssh-add - <<< "${{ secrets.SSH_PRIVATE_KEY }}"
- name: Download dependencies
run: flutter pub get
- name: Build & Publish to TestFlight with Fastlane - name: Build & Publish to TestFlight with Fastlane
env: env:
APP_STORE_CONNECT_API_KEY_KEY: ${{ secrets.APP_STORE_CONNECT_API_KEY_KEY }} APP_STORE_CONNECT_API_KEY_KEY: ${{ secrets.APP_STORE_CONNECT_API_KEY_KEY }}

View File

@ -1,4 +1,4 @@
include: package:very_good_analysis/analysis_options.2.4.0.yaml include: package:very_good_analysis/analysis_options.3.1.0.yaml
linter: linter:
rules: rules:
parameter_assignments: false parameter_assignments: false

View File

@ -64,12 +64,15 @@ android {
storePassword keystoreProperties['storePassword'] storePassword keystoreProperties['storePassword']
} }
} }
buildTypes { buildTypes {
release { release {
signingConfig signingConfigs.release signingConfig signingConfigs.release
minifyEnabled true
shrinkResources true
} }
} }
} }
flutter { flutter {

View File

@ -37,15 +37,6 @@
android:name="io.flutter.embedding.android.NormalTheme" android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme" android:resource="@style/NormalTheme"
/> />
<!-- Displays an Android View that continues showing the launch screen
Drawable until Flutter paints its first frame, then this splash
screen fades out. A splash screen is useful to avoid any visual
gap between the end of Android's launch screen and the painting of
Flutter's first frame. -->
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background"
/>
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN"/> <action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/> <category android:name="android.intent.category.LAUNCHER"/>

18
assets/eula.md Normal file
View File

@ -0,0 +1,18 @@
## End-user License Agreement
This policy applies to the usage of the Hacki app.
Please read this Mobile Application End User License Agreement (“EULA”) carefully before using the Hacki mobile application ("Mobile App"), which allows You to read and contribute to Hacker News from Your mobile device. This EULA forms a binding legal agreement between you (and any other entity on whose behalf you accept these terms) (collectively “You” or “Your”) and Hacki (each separately a “Party” and collectively the “Parties”) as of the date you download the Mobile App. Your use of the Mobile App is subject to this EULA.
### Changes to this EULA
Hacki reserves the right to modify this EULA at any time and for any reason. You are responsible for complying with the updated EULA. Your continued use of the Mobile App indicates Your consent to the updated terms.
### No Included Maintenance and Support
Hacki may deploy changes, updates, or enhancements to the Mobile App at any time. Hacki may provide maintenance and support for the Mobile App, but has no obligation whatsoever to furnish such services to You and may terminate such services at any time without notice.
### No Warranty
Hacki expressly disclaims all warranties of any kind, whether express or implied.
The Mobile App is only available for supported devices and might not work on every device. Determining whether Your device is a supported or compatible device for use of the Mobile App is solely Your responsibility, and downloading the Mobile App is done at Your own risk. Smartsheet does not represent or warrant that the Mobile App and Your device are compatible or that the Mobile App will work on Your device.
### Your Consent
By using the app, you consent to the end-user license agreement.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

48
assets/privacy_policy.md Normal file
View File

@ -0,0 +1,48 @@
## Privacy Policy
This policy applies to all information collected or submitted on Hacki.
### Information we collect
Hacki collects anonymous statistics such as crash reports and feature usage. These data are solely used to track app's health and are only stored locally on your device and only got sent to us when you choose to do so.
### Ads and analytics
Hacki does not serve ads.
Hacki collects aggregate, anonymous statistics to improve the app but these data are only stored locally on your device and only got sent to us when you choose to do so.
### Information usage
We use the information we collect to operate and improve our website, apps, and customer support.
We do not share personal information with outside parties except to the extent necessary to accomplish Hackis functionality.
We may disclose your information in response to subpoenas, court orders, or other legal requirements; to exercise our legal rights or defend against legal claims; to investigate, prevent, or take action regarding illegal activities, suspected fraud or abuse, violations of our policies; or to protect our rights and property.
### Security
Hacki uses the official Hacker News API for fetching data from Hacker News.
When logging in, usernames and passwords are securely sent to Hacker News' servers for authentication.
### Third-party links and content
Hacki displays links and content from third-party websites. These websites have their own independent privacy policies, and we have no responsibility or liability for their content or activities.
#### California Online Privacy Protection Act Compliance
Hacki complies with the California Online Privacy Protection Act. We therefore will not distribute your personal information to outside parties without your consent.
#### Childrens Online Privacy Protection Act Compliance
Hacki never collects or maintain information at our website from those we actually know are under 13, and no part of our website is structured to attract anyone under 13.
#### Information for European Union Customers
By using Hacki and providing your information, you authorize us to collect, use, and store your information outside of the European Union.
#### International Transfers of Information
Information may be processed, stored, and used outside of the country in which you are located. Data privacy laws vary across jurisdictions, and different laws may be applicable to your data depending on where it is processed, stored, or used.
### Your Consent
By using the app, you consent to the privacy policy.
### Contacting Us
If you have questions regarding this privacy policy, you may e-mail me us at jfeng@fastmail.com.
### Changes to this policy
If we decide to change this privacy policy, we will post those changes on this page.
February 27, 2023: First published.

30
components/in_app_review/.gitignore vendored Normal file
View File

@ -0,0 +1,30 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/
# IntelliJ related
*.iml
*.ipr
*.iws
.idea/
# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/
# Flutter/Dart/Pub related
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
/pubspec.lock
**/doc/api/
.dart_tool/
.packages
build/

View File

@ -0,0 +1,39 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled.
version:
revision: e3c29ec00c9c825c891d75054c63fcc46454dca1
channel: stable
project_type: plugin
# Tracks metadata for the flutter migrate command
migration:
platforms:
- platform: root
create_revision: e3c29ec00c9c825c891d75054c63fcc46454dca1
base_revision: e3c29ec00c9c825c891d75054c63fcc46454dca1
- platform: android
create_revision: e3c29ec00c9c825c891d75054c63fcc46454dca1
base_revision: e3c29ec00c9c825c891d75054c63fcc46454dca1
- platform: ios
create_revision: e3c29ec00c9c825c891d75054c63fcc46454dca1
base_revision: e3c29ec00c9c825c891d75054c63fcc46454dca1
- platform: macos
create_revision: e3c29ec00c9c825c891d75054c63fcc46454dca1
base_revision: e3c29ec00c9c825c891d75054c63fcc46454dca1
- platform: windows
create_revision: e3c29ec00c9c825c891d75054c63fcc46454dca1
base_revision: e3c29ec00c9c825c891d75054c63fcc46454dca1
# User provided section
# List of Local paths (relative to this file) that should be
# ignored by the migrate tool.
#
# Files that are not part of the templates will be ignored by default.
unmanaged_files:
- 'lib/main.dart'
- 'ios/Runner.xcodeproj/project.pbxproj'

View File

@ -0,0 +1,102 @@
# [2.0.6]
- Update Android Play Core dependency to Play Review 2.0.1.
# [2.0.5]
- Migrate Android Play Core dependency to Play Review 2.0.0.
- Recreate the example app.
- Update in_app_review_platform_interface to 2.0.4
# [2.0.4]
- Migrate maven repository from jcenter to mavenCentral
- `isAvailable()` now returns `false` on web.
# [2.0.3]
- Fix iOS no-scene exception. ([#41](https://github.com/britannio/in_app_review/issues/41))
# [2.0.2]
- Replace iOS Swift code with Objective-C to add compatibility with Objective-C Flutter apps.
# [2.0.1]
- Fix rare null pointer exception on Android
- Fix MissingPluginException on MacOS
- Bump the minimum Dart SDK version from `2.12.0-0` to `2.12.0`.
- Bump the minimum Flutter version to `2.0.0`.
- Update in_app_review_platform_interface to 2.0.2
# [2.0.0]
- Migrate to null safety.
# [1.0.4]
- Update in_app_review_platform_interface to 1.0.5
- Remove dependency on `package_info`.
- Handle `openStoreListing()` with native code for Android, iOS and MacOS.
# [1.0.3]
- Update in_app_review_platform_interface to 1.0.4
- Update android compileSdkVersion to 29.
- Lower dependency version constraints.
# [1.0.2]
- Update in_app_review_platform_interface to 1.0.3
- Open the App Store directly instead of via the Safari View Controller.
- Add automated tests.
- Improve docs.
# [1.0.1+1]
- Update in_app_review_platform_interface to 1.0.2
# [1.0.0]
- Migrate to use `in_app_review_platform_interface`.
- Add Windows support for `openStoreListing`.
# [0.2.1+1]
- Improve iOS testing docs.
# [0.2.1]
- Update dependencies.
- Android Play Core Library V1.8.2 release notes:
- Fixed UI flickering in the In-App Review API
# [0.2.0+4]
- Remove deprecated API warning.
- Update dependencies.
# [0.2.0+3]
- Instructions in the README have been improved along with the example.
# [0.2.0+2]
- Update changelog format
# [0.2.0+1]
- Update MacOS testing instructions
# [0.2.0] Breaking Change
- Add MacOS support
- Rename `openStoreListing(iOSAppStoreId: '')` to `openStoreListing(appStoreId: '')`
# [0.1.0]
- Improve docs
- Set Android minSdkVersion to 16
- Refactor Android Plugin
# [0.0.1]
Initial release

View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2020 Britannio Jarrett
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,9 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.cxx

View File

@ -0,0 +1,49 @@
group 'dev.britannio.in_app_review'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.6.10'
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 31
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
minSdkVersion 16
}
dependencies {
}
}

View File

@ -0,0 +1 @@
rootProject.name = 'in_app_review'

View File

@ -0,0 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dev.britannio.in_app_review">
</manifest>

View File

@ -0,0 +1,59 @@
package dev.britannio.in_app_review;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import androidx.annotation.NonNull;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
/**
* InAppReviewPlugin
*/
public class InAppReviewPlugin implements FlutterPlugin, MethodCallHandler {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private MethodChannel channel;
private final String TAG = "InAppReviewPlugin";
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "dev.britannio.in_app_review");
channel.setMethodCallHandler(this);
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
Log.i(TAG, "onMethodCall: " + call.method);
switch (call.method) {
case "isAvailable":
case "requestReview":
case "openStoreListing":
default:
result.notImplemented();
break;
}
}
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
}
}

38
components/in_app_review/ios/.gitignore vendored Normal file
View File

@ -0,0 +1,38 @@
.idea/
.vagrant/
.sconsign.dblite
.svn/
.DS_Store
*.swp
profile
DerivedData/
build/
GeneratedPluginRegistrant.h
GeneratedPluginRegistrant.m
.generated/
*.pbxuser
*.mode1v3
*.mode2v3
*.perspectivev3
!default.pbxuser
!default.mode1v3
!default.mode2v3
!default.perspectivev3
xcuserdata
*.moved-aside
*.pyc
*sync/
Icon?
.tags*
/Flutter/Generated.xcconfig
/Flutter/ephemeral/
/Flutter/flutter_export_environment.sh

View File

@ -0,0 +1,4 @@
#import <Flutter/Flutter.h>
@interface InAppReviewPlugin : NSObject<FlutterPlugin>
@end

View File

@ -0,0 +1,107 @@
#import "InAppReviewPlugin.h"
@import StoreKit;
@implementation InAppReviewPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
FlutterMethodChannel* channel = [FlutterMethodChannel methodChannelWithName:@"dev.britannio.in_app_review" binaryMessenger:[registrar messenger]];
InAppReviewPlugin* instance = [[InAppReviewPlugin alloc] init];
[registrar addMethodCallDelegate:instance channel:channel];
}
- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
[self logMessage:@"handle" details:call.method];
if ([call.method isEqual:@"requestReview"]) {
[self requestReview:result];
} else if ([call.method isEqual:@"isAvailable"]) {
[self isAvailable:result];
} else if ([call.method isEqual:@"openStoreListing"]) {
[self openStoreListingWithStoreId:call.arguments result:result];
} else {
[self logMessage:@"method not implemented"];
result(FlutterMethodNotImplemented);
}
}
- (void) requestReview:(FlutterResult)result {
if (@available(iOS 14, *)) {
[self logMessage:@"iOS 14+"];
UIWindowScene *scene = [self findActiveScene];
[SKStoreReviewController requestReviewInScene:scene];
result(nil);
} else if (@available(iOS 10.3, *)) {
[self logMessage:@"iOS 10.3+"];
[SKStoreReviewController requestReview];
result(nil);
} else {
result([FlutterError errorWithCode:@"unavailable"
message:@"In-App Review unavailable"
details:nil]);
}
}
- (UIWindowScene *) findActiveScene API_AVAILABLE(ios(13.0)){
for (UIWindowScene *scene in UIApplication.sharedApplication.connectedScenes) {
if (scene.activationState == UISceneActivationStateForegroundActive) {
return scene;
}
}
return nil;
}
- (void) isAvailable:(FlutterResult)result {
if (@available(iOS 10.3, *)) {
[self logMessage:@"available"];
result(@YES);
} else {
[self logMessage:@"unavailable"];
result(@NO);
}
}
- (void) openStoreListingWithStoreId:(NSString *)storeId result:(FlutterResult)result {
if (!storeId) {
result([FlutterError errorWithCode:@"no-store-id"
message:@"Your store id must be passed as the method channel's argument"
details:nil]);
return;
}
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://apps.apple.com/app/id%@?action=write-review", storeId]];
if (!url) {
result([FlutterError errorWithCode:@"url-construct-fail"
message:@"Failed to construct url"
details:nil]);
return;
}
UIApplication *app = [UIApplication sharedApplication];
if (@available(iOS 10.0, *)) {
[app openURL:url options:@{} completionHandler:nil];
} else {
[app openURL:url];
}
}
#pragma mark - Logging Helpers
- (void) logMessage:(NSString *) message {
NSLog(@"InAppReviewPlugin: %@", message);
}
- (void) logMessage:(NSString *) message
details:(NSString *) details {
NSLog(@"InAppReviewPlugin: %@ %@", message, details);
}
@end

View File

@ -0,0 +1,23 @@
#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html.
# Run `pod lib lint in_app_review.podspec` to validate before publishing.
#
Pod::Spec.new do |s|
s.name = 'in_app_review'
s.version = '0.2.0'
s.summary = 'Flutter plugin for showing the In-App Review/System Rating pop up.'
s.description = <<-DESC
Flutter plugin for showing the In-App Review/System Rating pop up..
DESC
s.homepage = 'http://example.com'
s.license = { :file => '../LICENSE' }
s.author = { 'Britannio Jarrett' => 'britanniojarrett@gmail.com' }
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.dependency 'Flutter'
s.platform = :ios, '9.0'
# Flutter.framework does not contain a i386 slice.
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' }
s.swift_version = '5.0'
end

View File

@ -0,0 +1,50 @@
import 'dart:async';
import 'package:in_app_review_platform_interface/in_app_review_platform_interface.dart';
class InAppReview {
InAppReview._();
static final InAppReview instance = InAppReview._();
/// Checks if the device is able to show a review dialog.
///
/// On Android the Google Play Store must be installed and the device must be
/// running **Android 5 Lollipop(API 21)** or higher.
///
/// iOS devices must be running **iOS version 10.3** or higher.
///
/// MacOS devices must be running **MacOS version 10.14** or higher
Future<bool> isAvailable() => InAppReviewPlatform.instance.isAvailable();
/// Attempts to show the review dialog. It's recommended to first check if
/// the device supports this feature via [isAvailable].
///
/// To improve the users experience, iOS and Android enforce limitations
/// that might prevent this from working after a few tries. iOS & MacOS users
/// can also disable this feature entirely in the App Store settings.
///
/// More info and guidance:
/// https://developer.android.com/guide/playcore/in-app-review#when-to-request
/// https://developer.apple.com/design/human-interface-guidelines/ios/system-capabilities/ratings-and-reviews/
/// https://developer.apple.com/design/human-interface-guidelines/macos/system-capabilities/ratings-and-reviews/
Future<void> requestReview() => InAppReviewPlatform.instance.requestReview();
/// Opens the Play Store on Android, the App Store with a review
/// screen on iOS & MacOS and the Microsoft Store on Windows.
///
/// [appStoreId] is required for iOS & MacOS.
///
/// [microsoftStoreId] is required for Windows.
Future<void> openStoreListing({
/// Required for iOS & MacOS.
String? appStoreId,
/// Required for Windows.
String? microsoftStoreId,
}) =>
InAppReviewPlatform.instance.openStoreListing(
appStoreId: appStoreId,
microsoftStoreId: microsoftStoreId,
);
}

View File

@ -0,0 +1,42 @@
import Cocoa
import FlutterMacOS
import StoreKit
public class InAppReviewPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "dev.britannio.in_app_review", binaryMessenger: registrar.messenger)
let instance = InAppReviewPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "requestReview":
//App Store Review
if #available(OSX 10.14, *) {
SKStoreReviewController.requestReview()
result(nil)
} else {
result(FlutterError(code: "unavailable", message: "In-App Review unavailable", details: nil))
}
case "isAvailable":
if #available(OSX 10.14, *) {
result(true)
} else {
result(false)
}
case "openStoreListing":
let storeId : String = call.arguments as! String;
guard let writeReviewURL = URL(string: "macappstore://apps.apple.com/app/id" + storeId + "?action=write-review")
else {
result(FlutterError(code: "url_construct_fail", message: "Failed to construct url", details: nil))
return
}
NSWorkspace.shared.open(writeReviewURL)
result(nil);
default:
result(FlutterMethodNotImplemented)
}
}
}

View File

@ -0,0 +1,22 @@
#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html.
# Run `pod lib lint in_app_review.podspec' to validate before publishing.
#
Pod::Spec.new do |s|
s.name = 'in_app_review'
s.version = '0.2.0'
s.summary = 'Flutter plugin for showing the In-App Review/System Rating pop up.'
s.description = <<-DESC
Flutter plugin for showing the In-App Review/System Rating pop up.
DESC
s.homepage = 'https://github.com/britannio/in_app_review'
s.license = { :file => '../LICENSE' }
s.author = { 'Britannio Jarrett' => 'britanniojarrett@gmail.com' }
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.dependency 'FlutterMacOS'
s.platform = :osx, '10.11'
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' }
s.swift_version = '5.0'
end

View File

@ -0,0 +1,46 @@
name: in_app_review
description: Flutter plugin for showing the In-App Review/System Rating pop up on Android, iOS and MacOS. It makes it easy for users to rate your app.
version: 2.0.6
homepage: https://github.com/britannio/in_app_review/tree/master/in_app_review
environment:
sdk: '>=2.12.0 <3.0.0'
flutter: ">=2.0.0"
dependencies:
flutter:
sdk: flutter
in_app_review_platform_interface: ^2.0.4
dev_dependencies:
flutter_test:
sdk: flutter
mockito: ^5.0.0
plugin_platform_interface: ^2.0.0
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter packages.
flutter:
# This section identifies this Flutter project as a plugin project.
# The 'pluginClass' specifies the class (in Java, Kotlin, Swift, Objective-C, etc.)
# which should be registered in the plugin registry. This is required for
# using method channels.
# The Android 'package' specifies package in which the registered class is.
# This is required for using method channels on Android.
# The 'ffiPlugin' specifies that native code should be built and bundled.
# This is required for using `dart:ffi`.
# All these are used by the tooling to maintain consistency when
# adding or updating assets for this project.
plugin:
platforms:
android:
package: dev.britannio.in_app_review
pluginClass: InAppReviewPlugin
ios:
pluginClass: InAppReviewPlugin
macos:
pluginClass: InAppReviewPlugin

View File

@ -0,0 +1,108 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:in_app_review/in_app_review.dart';
import 'package:in_app_review_platform_interface/in_app_review_platform_interface.dart';
import 'package:mockito/mockito.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
final inAppReview = InAppReview.instance;
late MockInAppReviewPlatform platform;
setUp(() {
platform = MockInAppReviewPlatform();
InAppReviewPlatform.instance = platform;
});
tearDown(() {
verifyNoMoreInteractions(platform);
});
group('isAvailable', () {
test(
'should call InAppReviewPlatform.isAvailable()',
() async {
// ARRANGE
when(platform.isAvailable()).thenAnswer((_) async => true);
// ACT
final result = await inAppReview.isAvailable();
// ASSERT
verify(platform.isAvailable());
expect(result, isTrue);
},
);
});
group('requestReview', () {
test(
'should call InAppReviewPlatform.requestReview()',
() async {
// ARRANGE
when(platform.requestReview()).thenAnswer((_) async {});
// ACT
await inAppReview.requestReview();
// ASSERT
verify(platform.requestReview());
},
);
});
group('openStoreListing', () {
test(
'should call InAppReviewPlatform.openStoreListing()',
() async {
// ARRANGE
const appStoreId = 'app_store_id';
const microsoftStoreId = 'microsoft_store_id';
when(platform.openStoreListing(
appStoreId: appStoreId,
microsoftStoreId: microsoftStoreId,
)).thenAnswer((_) async {});
// ACT
await inAppReview.openStoreListing(
appStoreId: appStoreId,
microsoftStoreId: microsoftStoreId,
);
// ASSERT
verify(platform.openStoreListing(
appStoreId: appStoreId,
microsoftStoreId: microsoftStoreId,
));
},
);
});
}
class MockInAppReviewPlatform extends Mock
with MockPlatformInterfaceMixin
implements InAppReviewPlatform {
@override
Future<bool> isAvailable() => super.noSuchMethod(
Invocation.method(#isAvailable, null),
returnValue: Future.value(true),
);
@override
Future<void> requestReview() => super.noSuchMethod(
Invocation.method(#requestReview, null),
returnValue: Future<void>.value(),
);
@override
Future<void> openStoreListing({
String? appStoreId,
String? microsoftStoreId,
}) =>
super.noSuchMethod(
Invocation.method(
#openStoreListing,
null,
{#appStoreId: appStoreId, #microsoftStoreId: microsoftStoreId},
),
returnValue: Future<void>.value(),
);
}

View File

@ -0,0 +1,12 @@
.DS_Store
.dart_tool/
.packages
.pub/
build/
pubspec.lock
.flutter-plugins
.flutter-plugins-dependencies

View File

@ -0,0 +1,46 @@
# [2.0.4]
- Update usage of `pkg:url_launcher` to address deprecations.
# [2.0.3]
- `isAvailable()` now returns `false` on web.
# [2.0.2]
- Bump the minimum Flutter version to `2.0.0`.
# [2.0.1]
- Bump the minimum Dart SDK version from `2.12.0-0` to `2.12.0`.
# [2.0.0]
- Migrate to null safety.
# [1.0.5]
- Remove dependency on `package_info`.
- Handle `openStoreListing()` with native code for Android, iOS and MacOS.
# [1.0.4]
- Lower dependency version constraints
# [1.0.3]
- Open the App Store directly instead of via the Safari View Controller.
- Add automated tests.
# [1.0.2]
- Rename `openStoreListing(windowsStoreId: '')` to `openStoreListing(microsoftStoreId: '')`.
- Update dependencies.
# [1.0.1]
- Remove unnecessary files.
# [1.0.0]
- Initial release.

View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2020 Britannio Jarrett
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,26 @@
# in_app_review_platform_interface
A common platform interface for the [`in_app_review`][1] plugin.
This interface allows platform-specific implementations of the `in_app_review`
plugin, as well as the plugin itself, to ensure they are supporting the
same interface.
# Usage
To implement a new platform-specific implementation of `in_app_review`, extend
[`InAppReviewPlatform`][2] with an implementation that performs the
platform-specific behavior, and when you register your plugin, set the default
`InAppReviewPlatform` by calling
`InAppReviewPlatform.instance = MyInAppReview()`.
# Note on breaking changes
Strongly prefer non-breaking changes (such as adding a method to the interface)
over breaking changes for this package.
See https://flutter.dev/go/platform-interface-breaking-changes for a discussion
on why a less-clean interface is preferable to a breaking change.
[1]: ../in_app_review
[2]: lib/in_app_review_platform_interface.dart

View File

@ -0,0 +1,71 @@
import 'package:in_app_review_platform_interface/method_channel_in_app_review.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
/// The interface that implementations of in_app_review must implement.
///
/// Platform implementations should extend this class rather than implement it
/// as `in_app_review` does not consider newly added methods to be breaking
/// changes. Extending this class (using `extends`) ensures that the subclass
/// will get the default implementation, while platform implementations that
/// `implements` this interface will be broken by newly added
/// [InAppReviewPlatform] methods.
abstract class InAppReviewPlatform extends PlatformInterface {
InAppReviewPlatform() : super(token: _token);
static InAppReviewPlatform _instance = MethodChannelInAppReview();
static final Object _token = Object();
static InAppReviewPlatform get instance => _instance;
/// Platform-specific plugins should set this with their own platform-specific
/// class that extends [InAppReviewPlatform] when they register themselves.
static set instance(InAppReviewPlatform instance) {
PlatformInterface.verifyToken(instance, _token);
_instance = instance;
}
/// Checks if the device is able to show a review dialog.
///
/// On Android the Google Play Store must be installed and the device must be
/// running **Android 5 Lollipop(API 21)** or higher.
///
/// iOS devices must be running **iOS version 10.3** or higher.
///
/// MacOS devices must be running **MacOS version 10.14** or higher
Future<bool> isAvailable() {
throw UnimplementedError('isAvailable() has not been implemented.');
}
/// Attempts to show the review dialog. It's recommended to first check if
/// this cannot be done via [isAvailable]. If it is not available then
/// you can open the store listing via [openStoreListing].
///
/// To improve the users experience, iOS and Android enforce limitations
/// that might prevent this from working after a few tries. iOS & MacOS users
/// can also disable this feature entirely in the App Store settings.
///
/// More info and guidance:
/// https://developer.android.com/guide/playcore/in-app-review#when-to-request
/// https://developer.apple.com/design/human-interface-guidelines/ios/system-capabilities/ratings-and-reviews/
/// https://developer.apple.com/design/human-interface-guidelines/macos/system-capabilities/ratings-and-reviews/
Future<void> requestReview() {
throw UnimplementedError('requestReview() has not been implemented.');
}
/// Opens the Play Store on Android, the App Store with a review
/// screen on iOS & MacOS and the Microsoft Store on Windows.
///
/// [appStoreId] is required for iOS & MacOS.
///
/// [microsoftStoreId] is required for Windows.
Future<void> openStoreListing({
/// Required for iOS & MacOS.
String? appStoreId,
/// Required for Windows.
String? microsoftStoreId,
}) {
throw UnimplementedError('openStoreListing() has not been implemented.');
}
}

View File

@ -0,0 +1,65 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:platform/platform.dart';
import 'package:url_launcher/url_launcher_string.dart';
import 'in_app_review_platform_interface.dart';
/// An implementation of [InAppReviewPlatform] that uses method channels.
class MethodChannelInAppReview extends InAppReviewPlatform {
MethodChannel _channel = MethodChannel('dev.britannio.in_app_review');
Platform _platform = const LocalPlatform();
@visibleForTesting
set channel(MethodChannel channel) => _channel = channel;
@visibleForTesting
set platform(Platform platform) => _platform = platform;
@override
Future<bool> isAvailable() async {
if (kIsWeb) return false;
return _channel
.invokeMethod<bool>('isAvailable')
.then((bool? available) => available ?? false, onError: (_) => false);
}
@override
Future<void> requestReview() => _channel.invokeMethod('requestReview');
@override
Future<void> openStoreListing({
String? appStoreId,
String? microsoftStoreId,
}) async {
final bool isiOS = _platform.isIOS;
final bool isMacOS = _platform.isMacOS;
final bool isAndroid = _platform.isAndroid;
final bool isWindows = _platform.isWindows;
if (isiOS || isMacOS) {
await _channel.invokeMethod(
'openStoreListing',
ArgumentError.checkNotNull(appStoreId, 'appStoreId'),
);
} else if (isAndroid) {
await _channel.invokeMethod('openStoreListing');
} else if (isWindows) {
ArgumentError.checkNotNull(microsoftStoreId, 'microsoftStoreId');
await _launchUrl(
'ms-windows-store://review/?ProductId=$microsoftStoreId',
);
} else {
throw UnsupportedError(
'Platform(${_platform.operatingSystem}) not supported',
);
}
}
Future<void> _launchUrl(String url) async {
if (!await canLaunchUrlString(url)) return;
await launchUrlString(url, mode: LaunchMode.externalNonBrowserApplication);
}
}

View File

@ -0,0 +1,24 @@
name: in_app_review_platform_interface
description: A common platform interface for the in_app_review plugin.
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 2.0.4
homepage: https://github.com/britannio/in_app_review/tree/master/in_app_review_platform_interface
environment:
sdk: '>=2.12.0 <3.0.0'
flutter: ">=2.0.0"
dependencies:
flutter:
sdk: flutter
url_launcher: ^6.1.0
plugin_platform_interface: ^2.0.0
platform: ^3.0.0
dev_dependencies:
flutter_test:
sdk: flutter

View File

@ -0,0 +1,136 @@
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:in_app_review_platform_interface/method_channel_in_app_review.dart';
import 'package:platform/platform.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
late MethodChannelInAppReview methodChannelInAppReview;
late List<MethodCall> log = <MethodCall>[];
const MethodChannel channel = MethodChannel('dev.britannio.in_app_review');
setUp(() {
methodChannelInAppReview = MethodChannelInAppReview();
methodChannelInAppReview.channel = channel;
log = <MethodCall>[];
});
tearDown(() {
log.clear();
});
channel.setMockMethodCallHandler((MethodCall call) async {
log.add(call);
switch (call.method) {
case 'isAvailable':
return true;
case 'requestReview':
case 'openStoreListing':
return null;
default:
assert(false);
return null;
}
});
group('isAvailable', () {
test(
'should invoke the isAvailable method channel',
() async {
// ACT
final bool result = await methodChannelInAppReview.isAvailable();
// ASSERT
expect(log, <Matcher>[isMethodCall('isAvailable', arguments: null)]);
expect(result, isTrue);
},
);
});
group('requestReview', () {
test(
'should invoke the requestReview method channel',
() async {
// ACT
await methodChannelInAppReview.requestReview();
// ASSERT
expect(log, <Matcher>[isMethodCall('requestReview', arguments: null)]);
},
);
});
group('openStoreListing', () {
test(
'should invoke the openStoreListing method channel on Android',
() async {
// ARRANGE
methodChannelInAppReview.platform =
FakePlatform(operatingSystem: 'android');
// ACT
await methodChannelInAppReview.openStoreListing();
// ASSERT
expect(
log,
<Matcher>[isMethodCall('openStoreListing', arguments: null)],
);
},
);
test(
'should invoke the openStoreListing method channel on iOS',
() async {
// ARRANGE
methodChannelInAppReview.platform =
FakePlatform(operatingSystem: 'ios');
final String appStoreId = "store_id";
// ACT
await methodChannelInAppReview.openStoreListing(appStoreId: appStoreId);
// ASSERT
expect(log,
<Matcher>[isMethodCall('openStoreListing', arguments: appStoreId)]);
},
);
test(
'should invoke the openStoreListing method channel on MacOS',
() async {
// ARRANGE
methodChannelInAppReview.platform =
FakePlatform(operatingSystem: 'macos');
final String appStoreId = "store_id";
// ACT
await methodChannelInAppReview.openStoreListing(appStoreId: appStoreId);
// ASSERT
expect(log,
<Matcher>[isMethodCall('openStoreListing', arguments: appStoreId)]);
},
);
test(
'should invoke the openStoreListing method channel on Windows',
() async {
// ARRANGE
methodChannelInAppReview.platform =
FakePlatform(operatingSystem: 'windows');
final String microsoftStoreId = 'store_id';
// ACT
await methodChannelInAppReview.openStoreListing(
microsoftStoreId: microsoftStoreId,
);
// ASSERT
expect(log, <Matcher>[
isMethodCall('openStoreListing', arguments: microsoftStoreId)
]);
},
skip:
'The windows uwp implementation still uses the url_launcher package',
);
});
}

View File

@ -1,4 +1,4 @@
include: package:very_good_analysis/analysis_options.2.4.0.yaml include: package:very_good_analysis/analysis_options.3.1.0.yaml
linter: linter:
rules: rules:
parameter_assignments: false parameter_assignments: false

View File

@ -0,0 +1 @@
- Navigation shortcuts.

View File

@ -0,0 +1,3 @@
- Customization of tab bar.
- Option to enable swipe gesture for switching between tabs.
- Access to action menu from home screen.

View File

@ -0,0 +1,5 @@
- Customization of tab bar.
- Option to enable swipe gesture for switching between tabs.
- Access to action menu from home screen.
- Access to Wikipedia and Wiktionary from text selection toolbar.
- Quotes and emphasis rendering.

View File

@ -0,0 +1,5 @@
- Customization of tab bar.
- Option to enable swipe gesture for switching between tabs.
- Access to action menu from home screen.
- Access to Wikipedia and Wiktionary from text selection toolbar.
- Quotes and emphasis rendering.

View File

@ -2,7 +2,11 @@ PODS:
- connectivity_plus (0.0.1): - connectivity_plus (0.0.1):
- Flutter - Flutter
- ReachabilitySwift - ReachabilitySwift
- device_info_plus (0.0.1):
- Flutter
- Flutter (1.0.0) - Flutter (1.0.0)
- flutter_email_sender (0.0.1):
- Flutter
- flutter_inappwebview (0.0.1): - flutter_inappwebview (0.0.1):
- Flutter - Flutter
- flutter_inappwebview/Core (= 0.0.1) - flutter_inappwebview/Core (= 0.0.1)
@ -12,27 +16,31 @@ PODS:
- OrderedSet (~> 5.0) - OrderedSet (~> 5.0)
- flutter_local_notifications (0.0.1): - flutter_local_notifications (0.0.1):
- Flutter - Flutter
- flutter_secure_storage (3.3.1): - flutter_secure_storage (6.0.0):
- Flutter - Flutter
- flutter_siri_suggestions (0.0.1): - flutter_siri_suggestions (0.0.1):
- Flutter - Flutter
- FMDB (2.7.5): - FMDB (2.7.5):
- FMDB/standard (= 2.7.5) - FMDB/standard (= 2.7.5)
- FMDB/standard (2.7.5) - FMDB/standard (2.7.5)
- in_app_review (0.2.0):
- Flutter
- integration_test (0.0.1): - integration_test (0.0.1):
- Flutter - Flutter
- OrderedSet (5.0.0) - OrderedSet (5.0.0)
- package_info_plus (0.4.5): - package_info_plus (0.4.5):
- Flutter - Flutter
- path_provider_ios (0.0.1): - path_provider_foundation (0.0.1):
- Flutter - Flutter
- FlutterMacOS
- ReachabilitySwift (5.0.0) - ReachabilitySwift (5.0.0)
- receive_sharing_intent (0.0.1): - receive_sharing_intent (0.0.1):
- Flutter - Flutter
- share_plus (0.0.1): - share_plus (0.0.1):
- Flutter - Flutter
- shared_preferences_ios (0.0.1): - shared_preferences_foundation (0.0.1):
- Flutter - Flutter
- FlutterMacOS
- sqflite (0.0.2): - sqflite (0.0.2):
- Flutter - Flutter
- FMDB (>= 2.7.5) - FMDB (>= 2.7.5)
@ -49,17 +57,20 @@ PODS:
DEPENDENCIES: DEPENDENCIES:
- connectivity_plus (from `.symlinks/plugins/connectivity_plus/ios`) - connectivity_plus (from `.symlinks/plugins/connectivity_plus/ios`)
- device_info_plus (from `.symlinks/plugins/device_info_plus/ios`)
- Flutter (from `Flutter`) - Flutter (from `Flutter`)
- flutter_email_sender (from `.symlinks/plugins/flutter_email_sender/ios`)
- flutter_inappwebview (from `.symlinks/plugins/flutter_inappwebview/ios`) - flutter_inappwebview (from `.symlinks/plugins/flutter_inappwebview/ios`)
- flutter_local_notifications (from `.symlinks/plugins/flutter_local_notifications/ios`) - flutter_local_notifications (from `.symlinks/plugins/flutter_local_notifications/ios`)
- flutter_secure_storage (from `.symlinks/plugins/flutter_secure_storage/ios`) - flutter_secure_storage (from `.symlinks/plugins/flutter_secure_storage/ios`)
- flutter_siri_suggestions (from `.symlinks/plugins/flutter_siri_suggestions/ios`) - flutter_siri_suggestions (from `.symlinks/plugins/flutter_siri_suggestions/ios`)
- in_app_review (from `.symlinks/plugins/in_app_review/ios`)
- integration_test (from `.symlinks/plugins/integration_test/ios`) - integration_test (from `.symlinks/plugins/integration_test/ios`)
- package_info_plus (from `.symlinks/plugins/package_info_plus/ios`) - package_info_plus (from `.symlinks/plugins/package_info_plus/ios`)
- path_provider_ios (from `.symlinks/plugins/path_provider_ios/ios`) - path_provider_foundation (from `.symlinks/plugins/path_provider_foundation/ios`)
- receive_sharing_intent (from `.symlinks/plugins/receive_sharing_intent/ios`) - receive_sharing_intent (from `.symlinks/plugins/receive_sharing_intent/ios`)
- share_plus (from `.symlinks/plugins/share_plus/ios`) - share_plus (from `.symlinks/plugins/share_plus/ios`)
- shared_preferences_ios (from `.symlinks/plugins/shared_preferences_ios/ios`) - shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/ios`)
- sqflite (from `.symlinks/plugins/sqflite/ios`) - sqflite (from `.symlinks/plugins/sqflite/ios`)
- synced_shared_preferences (from `.symlinks/plugins/synced_shared_preferences/ios`) - synced_shared_preferences (from `.symlinks/plugins/synced_shared_preferences/ios`)
- url_launcher_ios (from `.symlinks/plugins/url_launcher_ios/ios`) - url_launcher_ios (from `.symlinks/plugins/url_launcher_ios/ios`)
@ -76,8 +87,12 @@ SPEC REPOS:
EXTERNAL SOURCES: EXTERNAL SOURCES:
connectivity_plus: connectivity_plus:
:path: ".symlinks/plugins/connectivity_plus/ios" :path: ".symlinks/plugins/connectivity_plus/ios"
device_info_plus:
:path: ".symlinks/plugins/device_info_plus/ios"
Flutter: Flutter:
:path: Flutter :path: Flutter
flutter_email_sender:
:path: ".symlinks/plugins/flutter_email_sender/ios"
flutter_inappwebview: flutter_inappwebview:
:path: ".symlinks/plugins/flutter_inappwebview/ios" :path: ".symlinks/plugins/flutter_inappwebview/ios"
flutter_local_notifications: flutter_local_notifications:
@ -86,18 +101,20 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/flutter_secure_storage/ios" :path: ".symlinks/plugins/flutter_secure_storage/ios"
flutter_siri_suggestions: flutter_siri_suggestions:
:path: ".symlinks/plugins/flutter_siri_suggestions/ios" :path: ".symlinks/plugins/flutter_siri_suggestions/ios"
in_app_review:
:path: ".symlinks/plugins/in_app_review/ios"
integration_test: integration_test:
:path: ".symlinks/plugins/integration_test/ios" :path: ".symlinks/plugins/integration_test/ios"
package_info_plus: package_info_plus:
:path: ".symlinks/plugins/package_info_plus/ios" :path: ".symlinks/plugins/package_info_plus/ios"
path_provider_ios: path_provider_foundation:
:path: ".symlinks/plugins/path_provider_ios/ios" :path: ".symlinks/plugins/path_provider_foundation/ios"
receive_sharing_intent: receive_sharing_intent:
:path: ".symlinks/plugins/receive_sharing_intent/ios" :path: ".symlinks/plugins/receive_sharing_intent/ios"
share_plus: share_plus:
:path: ".symlinks/plugins/share_plus/ios" :path: ".symlinks/plugins/share_plus/ios"
shared_preferences_ios: shared_preferences_foundation:
:path: ".symlinks/plugins/shared_preferences_ios/ios" :path: ".symlinks/plugins/shared_preferences_foundation/ios"
sqflite: sqflite:
:path: ".symlinks/plugins/sqflite/ios" :path: ".symlinks/plugins/sqflite/ios"
synced_shared_preferences: synced_shared_preferences:
@ -113,23 +130,26 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS: SPEC CHECKSUMS:
connectivity_plus: 413a8857dd5d9f1c399a39130850d02fe0feaf7e connectivity_plus: 413a8857dd5d9f1c399a39130850d02fe0feaf7e
device_info_plus: e5c5da33f982a436e103237c0c85f9031142abed
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854 Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
flutter_email_sender: 02d7443217d8c41483223627972bfdc09f74276b
flutter_inappwebview: bfd58618f49dc62f2676de690fc6dcda1d6c3721 flutter_inappwebview: bfd58618f49dc62f2676de690fc6dcda1d6c3721
flutter_local_notifications: 0c0b1ae97e741e1521e4c1629a459d04b9aec743 flutter_local_notifications: 0c0b1ae97e741e1521e4c1629a459d04b9aec743
flutter_secure_storage: 7953c38a04c3fdbb00571bcd87d8e3b5ceb9daec flutter_secure_storage: 23fc622d89d073675f2eaa109381aefbcf5a49be
flutter_siri_suggestions: 226fb7ef33d25d3fe0d4aa2a8bcf4b72730c466f flutter_siri_suggestions: 226fb7ef33d25d3fe0d4aa2a8bcf4b72730c466f
FMDB: 2ce00b547f966261cd18927a3ddb07cb6f3db82a FMDB: 2ce00b547f966261cd18927a3ddb07cb6f3db82a
in_app_review: 318597b3a06c22bb46dc454d56828c85f444f99d
integration_test: a1e7d09bd98eca2fc37aefd79d4f41ad37bdbbe5 integration_test: a1e7d09bd98eca2fc37aefd79d4f41ad37bdbbe5
OrderedSet: aaeb196f7fef5a9edf55d89760da9176ad40b93c OrderedSet: aaeb196f7fef5a9edf55d89760da9176ad40b93c
package_info_plus: 6c92f08e1f853dc01228d6f553146438dafcd14e package_info_plus: 6c92f08e1f853dc01228d6f553146438dafcd14e
path_provider_ios: 14f3d2fd28c4fdb42f44e0f751d12861c43cee02 path_provider_foundation: 37748e03f12783f9de2cb2c4eadfaa25fe6d4852
ReachabilitySwift: 985039c6f7b23a1da463388634119492ff86c825 ReachabilitySwift: 985039c6f7b23a1da463388634119492ff86c825
receive_sharing_intent: c0d87310754e74c0f9542947e7cbdf3a0335a3b1 receive_sharing_intent: c0d87310754e74c0f9542947e7cbdf3a0335a3b1
share_plus: 056a1e8ac890df3e33cb503afffaf1e9b4fbae68 share_plus: 056a1e8ac890df3e33cb503afffaf1e9b4fbae68
shared_preferences_ios: 548a61f8053b9b8a49ac19c1ffbc8b92c50d68ad shared_preferences_foundation: 297b3ebca31b34ec92be11acd7fb0ba932c822ca
sqflite: 6d358c025f5b867b29ed92fc697fd34924e11904 sqflite: 6d358c025f5b867b29ed92fc697fd34924e11904
synced_shared_preferences: f722742b06d65c7315b8e9f56b794c9fbd5597f7 synced_shared_preferences: f722742b06d65c7315b8e9f56b794c9fbd5597f7
url_launcher_ios: 839c58cdb4279282219f5e248c3321761ff3c4de url_launcher_ios: fb12c43172927bb5cf75aeebd073f883801f1993
wakelock: d0fc7c864128eac40eba1617cb5264d9c940b46f wakelock: d0fc7c864128eac40eba1617cb5264d9c940b46f
webview_flutter_wkwebview: b7e70ef1ddded7e69c796c7390ee74180182971f webview_flutter_wkwebview: b7e70ef1ddded7e69c796c7390ee74180182971f
workmanager: 0afdcf5628bbde6924c21af7836fed07b42e30e6 workmanager: 0afdcf5628bbde6924c21af7836fed07b42e30e6

View File

@ -3,7 +3,7 @@
archiveVersion = 1; archiveVersion = 1;
classes = { classes = {
}; };
objectVersion = 51; objectVersion = 54;
objects = { objects = {
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
@ -360,6 +360,7 @@
/* Begin PBXShellScriptBuildPhase section */ /* Begin PBXShellScriptBuildPhase section */
3B06AD1E1E4923F5004D2608 /* Thin Binary */ = { 3B06AD1E1E4923F5004D2608 /* Thin Binary */ = {
isa = PBXShellScriptBuildPhase; isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
); );
@ -413,6 +414,7 @@
}; };
9740EEB61CF901F6004384FC /* Run Script */ = { 9740EEB61CF901F6004384FC /* Run Script */ = {
isa = PBXShellScriptBuildPhase; isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
); );

View File

@ -1,9 +1,9 @@
import UIKit import UIKit
import Flutter import Flutter
import workmanager import workmanager
import shared_preferences_ios import shared_preferences_foundation
import flutter_secure_storage import flutter_secure_storage
import path_provider_ios import path_provider_foundation
import flutter_local_notifications import flutter_local_notifications
@UIApplicationMain @UIApplicationMain
@ -19,6 +19,8 @@ import flutter_local_notifications
WorkmanagerPlugin.register(with: self.registrar(forPlugin: "be.tramckrijte.workmanager.WorkmanagerPlugin")!) WorkmanagerPlugin.register(with: self.registrar(forPlugin: "be.tramckrijte.workmanager.WorkmanagerPlugin")!)
WorkmanagerPlugin.registerTask(withIdentifier: "workmanager.background.task")
if #available(iOS 10.0, *) { if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
} }
@ -26,8 +28,8 @@ import flutter_local_notifications
WorkmanagerPlugin.setPluginRegistrantCallback { registry in WorkmanagerPlugin.setPluginRegistrantCallback { registry in
GeneratedPluginRegistrant.register(with: registry) GeneratedPluginRegistrant.register(with: registry)
FLTSharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin")!) SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin")!)
FLTPathProviderPlugin.register(with: registry.registrar(forPlugin: "io.flutter.plugins.pathprovider.PathProviderPlugin")!) PathProviderPlugin.register(with: registry.registrar(forPlugin: "io.flutter.plugins.pathprovider.PathProviderPlugin")!)
FlutterLocalNotificationsPlugin.register(with: registry.registrar(forPlugin: "com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin")!) FlutterLocalNotificationsPlugin.register(with: registry.registrar(forPlugin: "com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin")!)
} }

View File

@ -74,5 +74,7 @@
</array> </array>
<key>ITSAppUsesNonExemptEncryption</key> <key>ITSAppUsesNonExemptEncryption</key>
<false/> <false/>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
</dict> </dict>
</plist> </plist>

View File

@ -49,7 +49,7 @@ latest_testflight_build_number
# Prep the xcodeproject from Flutter without building (`--config-only`) # Prep the xcodeproject from Flutter without building (`--config-only`)
sh( sh(
"flutter", "build", "ios", "--config-only", "/Users/runner/work/Hacki/Hacki/submodules/flutter/bin/flutter", "build", "ios", "--config-only",
"--release", "--no-pub", "--no-codesign", "--release", "--no-pub", "--no-codesign",
"--build-number", new_build_number.to_s "--build-number", new_build_number.to_s
) )

View File

@ -41,20 +41,25 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
await _authRepository.loggedIn.then((bool loggedIn) async { await _authRepository.loggedIn.then((bool loggedIn) async {
if (loggedIn) { if (loggedIn) {
final String? username = await _authRepository.username; final String? username = await _authRepository.username;
final User user = User? user = await _storiesRepository.fetchUser(id: username!);
await _storiesRepository.fetchUserBy(userId: username!);
/// According to Hacker News' API documentation,
/// if user has no public activity (posting a comment or story),
/// then it will not be available from the API.
user ??= User.emptyWithId(username);
emit( emit(
state.copyWith( state.copyWith(
isLoggedIn: true, isLoggedIn: true,
user: user, user: user,
status: AuthStatus.loaded,
), ),
); );
} else { } else {
emit( emit(
state.copyWith( state.copyWith(
status: AuthStatus.loaded,
isLoggedIn: false, isLoggedIn: false,
status: AuthStatus.loaded,
), ),
); );
} }
@ -84,11 +89,10 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
); );
if (successful) { if (successful) {
final User user = final User? user = await _storiesRepository.fetchUser(id: event.username);
await _storiesRepository.fetchUserBy(userId: event.username);
emit( emit(
state.copyWith( state.copyWith(
user: user, user: user ?? User.emptyWithId(event.username),
isLoggedIn: true, isLoggedIn: true,
status: AuthStatus.loaded, status: AuthStatus.loaded,
), ),

View File

@ -17,11 +17,13 @@ part 'stories_state.dart';
class StoriesBloc extends Bloc<StoriesEvent, StoriesState> { class StoriesBloc extends Bloc<StoriesEvent, StoriesState> {
StoriesBloc({ StoriesBloc({
required PreferenceCubit preferenceCubit, required PreferenceCubit preferenceCubit,
required FilterCubit filterCubit,
OfflineRepository? offlineRepository, OfflineRepository? offlineRepository,
StoriesRepository? storiesRepository, StoriesRepository? storiesRepository,
PreferenceRepository? preferenceRepository, PreferenceRepository? preferenceRepository,
Logger? logger, Logger? logger,
}) : _preferenceCubit = preferenceCubit, }) : _preferenceCubit = preferenceCubit,
_filterCubit = filterCubit,
_offlineRepository = _offlineRepository =
offlineRepository ?? locator.get<OfflineRepository>(), offlineRepository ?? locator.get<OfflineRepository>(),
_storiesRepository = _storiesRepository =
@ -37,6 +39,7 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> {
on<StoryRead>(onStoryRead); on<StoryRead>(onStoryRead);
on<StoriesLoaded>(onStoriesLoaded); on<StoriesLoaded>(onStoriesLoaded);
on<StoriesDownload>(onDownload); on<StoriesDownload>(onDownload);
on<StoriesCancelDownload>(onCancelDownload);
on<StoryDownloaded>(onStoryDownloaded); on<StoryDownloaded>(onStoryDownloaded);
on<StoriesExitOffline>(onExitOffline); on<StoriesExitOffline>(onExitOffline);
on<StoriesPageSizeChanged>(onPageSizeChanged); on<StoriesPageSizeChanged>(onPageSizeChanged);
@ -44,6 +47,7 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> {
} }
final PreferenceCubit _preferenceCubit; final PreferenceCubit _preferenceCubit;
final FilterCubit _filterCubit;
final OfflineRepository _offlineRepository; final OfflineRepository _offlineRepository;
final StoriesRepository _storiesRepository; final StoriesRepository _storiesRepository;
final PreferenceRepository _preferenceRepository; final PreferenceRepository _preferenceRepository;
@ -55,75 +59,72 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> {
static const int _tabletSmallPageSize = 15; static const int _tabletSmallPageSize = 15;
static const int _tabletLargePageSize = 25; static const int _tabletLargePageSize = 25;
/// Types of story to be shown in the tab bar.
static const Set<StoryType> types = <StoryType>{
StoryType.top,
StoryType.best,
StoryType.latest,
StoryType.ask,
StoryType.show,
};
Future<void> onInitialize( Future<void> onInitialize(
StoriesInitialize event, StoriesInitialize event,
Emitter<StoriesState> emit, Emitter<StoriesState> emit,
) async { ) async {
_streamSubscription ??= _streamSubscription ??=
_preferenceCubit.stream.listen((PreferenceState event) { _preferenceCubit.stream.listen((PreferenceState event) {
final bool isComplexTile = event.showComplexStoryTile; final bool isComplexTile = event.complexStoryTileEnabled;
final int pageSize = _getPageSize(isComplexTile: isComplexTile); final int pageSize = getPageSize(isComplexTile: isComplexTile);
if (pageSize != state.currentPageSize) { if (pageSize != state.currentPageSize) {
add(StoriesPageSizeChanged(pageSize: pageSize)); add(StoriesPageSizeChanged(pageSize: pageSize));
} }
}); });
final bool hasCachedStories = await _offlineRepository.hasCachedStories; final bool hasCachedStories = await _offlineRepository.hasCachedStories;
final bool isComplexTile = _preferenceCubit.state.showComplexStoryTile; final bool isComplexTile = _preferenceCubit.state.complexStoryTileEnabled;
final int pageSize = _getPageSize(isComplexTile: isComplexTile); final int pageSize = getPageSize(isComplexTile: isComplexTile);
emit( emit(
const StoriesState.init().copyWith( const StoriesState.init().copyWith(
offlineReading: hasCachedStories, isOfflineReading: hasCachedStories &&
// Only go into offline mode in the next session.
state.downloadStatus == StoriesDownloadStatus.initial,
currentPageSize: pageSize, currentPageSize: pageSize,
downloadStatus: state.downloadStatus,
storiesDownloaded: state.storiesDownloaded,
storiesToBeDownloaded: state.storiesToBeDownloaded,
), ),
); );
for (final StoryType type in types) { for (final StoryType type in StoryType.values) {
await loadStories(of: type, emit: emit); await loadStories(type: type, emit: emit);
} }
} }
Future<void> loadStories({ Future<void> loadStories({
required StoryType of, required StoryType type,
required Emitter<StoriesState> emit, required Emitter<StoriesState> emit,
}) async { }) async {
if (state.offlineReading) { if (state.isOfflineReading) {
final List<int> ids = await _offlineRepository.getCachedStoryIds(of: of); final List<int> ids =
await _offlineRepository.getCachedStoryIds(type: type);
emit( emit(
state state
.copyWithStoryIdsUpdated(of: of, to: ids) .copyWithStoryIdsUpdated(type: type, to: ids)
.copyWithCurrentPageUpdated(of: of, to: 0), .copyWithCurrentPageUpdated(type: type, to: 0),
); );
_offlineRepository _offlineRepository
.getCachedStoriesStream( .getCachedStoriesStream(
ids: ids.sublist(0, min(ids.length, state.currentPageSize)), ids: ids.sublist(0, min(ids.length, state.currentPageSize)),
) )
.listen((Story story) { .listen((Story story) {
add(StoryLoaded(story: story, type: of)); add(StoryLoaded(story: story, type: type));
}).onDone(() { }).onDone(() {
add(StoriesLoaded(type: of)); add(StoriesLoaded(type: type));
}); });
} else { } else {
final List<int> ids = await _storiesRepository.fetchStoryIds(of: of); final List<int> ids = await _storiesRepository.fetchStoryIds(type: type);
emit( emit(
state state
.copyWithStoryIdsUpdated(of: of, to: ids) .copyWithStoryIdsUpdated(type: type, to: ids)
.copyWithCurrentPageUpdated(of: of, to: 0), .copyWithCurrentPageUpdated(type: type, to: 0),
); );
_storiesRepository _storiesRepository
.fetchStoriesStream(ids: ids.sublist(0, state.currentPageSize)) .fetchStoriesStream(ids: ids.sublist(0, state.currentPageSize))
.listen((Story story) { .listen((Story story) {
add(StoryLoaded(story: story, type: of)); add(StoryLoaded(story: story, type: type));
}).onDone(() { }).onDone(() {
add(StoriesLoaded(type: of)); add(StoriesLoaded(type: type));
}); });
} }
} }
@ -134,35 +135,37 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> {
) async { ) async {
emit( emit(
state.copyWithStatusUpdated( state.copyWithStatusUpdated(
of: event.type, type: event.type,
to: StoriesStatus.loading, to: StoriesStatus.loading,
), ),
); );
if (state.offlineReading) { if (state.isOfflineReading) {
emit( emit(
state.copyWithStatusUpdated( state.copyWithStatusUpdated(
of: event.type, type: event.type,
to: StoriesStatus.loaded, to: StoriesStatus.loaded,
), ),
); );
} else { } else {
emit(state.copyWithRefreshed(of: event.type)); emit(state.copyWithRefreshed(type: event.type));
await loadStories(of: event.type, emit: emit); await loadStories(type: event.type, emit: emit);
} }
} }
void onLoadMore(StoriesLoadMore event, Emitter<StoriesState> emit) { void onLoadMore(StoriesLoadMore event, Emitter<StoriesState> emit) {
emit( emit(
state.copyWithStatusUpdated( state.copyWithStatusUpdated(
of: event.type, type: event.type,
to: StoriesStatus.loading, to: StoriesStatus.loading,
), ),
); );
final int currentPage = state.currentPageByType[event.type]!; final int currentPage = state.currentPageByType[event.type]!;
final int len = state.storyIdsByType[event.type]!.length; final int len = state.storyIdsByType[event.type]!.length;
emit(state.copyWithCurrentPageUpdated(of: event.type, to: currentPage + 1)); emit(
state.copyWithCurrentPageUpdated(type: event.type, to: currentPage + 1),
);
final int currentPageSize = state.currentPageSize; final int currentPageSize = state.currentPageSize;
final int lower = currentPageSize * (currentPage + 1); final int lower = currentPageSize * (currentPage + 1);
int upper = currentPageSize + lower; int upper = currentPageSize + lower;
@ -172,7 +175,7 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> {
upper = len; upper = len;
} }
if (state.offlineReading) { if (state.isOfflineReading) {
_offlineRepository _offlineRepository
.getCachedStoriesStream( .getCachedStoriesStream(
ids: state.storyIdsByType[event.type]!.sublist( ids: state.storyIdsByType[event.type]!.sublist(
@ -212,7 +215,7 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> {
} else { } else {
emit( emit(
state.copyWithStatusUpdated( state.copyWithStatusUpdated(
of: event.type, type: event.type,
to: StoriesStatus.loaded, to: StoriesStatus.loaded,
), ),
); );
@ -224,17 +227,24 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> {
Emitter<StoriesState> emit, Emitter<StoriesState> emit,
) async { ) async {
final bool hasRead = await _preferenceRepository.hasRead(event.story.id); final bool hasRead = await _preferenceRepository.hasRead(event.story.id);
final bool hidden = _filterCubit.state.keywords.any(
(String keyword) =>
event.story.title.toLowerCase().contains(keyword) ||
event.story.text.toLowerCase().contains(keyword),
);
emit( emit(
state.copyWithStoryAdded( state.copyWithStoryAdded(
of: event.type, type: event.type,
story: event.story, story: event.story.copyWith(hidden: hidden),
hasRead: hasRead, hasRead: hasRead,
), ),
); );
} }
void onStoriesLoaded(StoriesLoaded event, Emitter<StoriesState> emit) { void onStoriesLoaded(StoriesLoaded event, Emitter<StoriesState> emit) {
emit(state.copyWithStatusUpdated(of: event.type, to: StoriesStatus.loaded)); emit(
state.copyWithStatusUpdated(type: event.type, to: StoriesStatus.loaded),
);
} }
Future<void> onDownload( Future<void> onDownload(
@ -252,12 +262,15 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> {
await _offlineRepository.deleteAllComments(); await _offlineRepository.deleteAllComments();
final Set<int> prioritizedIds = <int>{}; final Set<int> prioritizedIds = <int>{};
final List<StoryType> prioritizedTypes = <StoryType>[...types]
/// Prioritizing all types of stories except StoryType.latest since
/// new stories tend to have less or no comment at all.
final List<StoryType> prioritizedTypes = <StoryType>[...StoryType.values]
..remove(StoryType.latest); ..remove(StoryType.latest);
for (final StoryType type in prioritizedTypes) { for (final StoryType type in prioritizedTypes) {
final List<int> ids = await _storiesRepository.fetchStoryIds(of: type); final List<int> ids = await _storiesRepository.fetchStoryIds(type: type);
await _offlineRepository.cacheStoryIds(of: type, ids: ids); await _offlineRepository.cacheStoryIds(type: type, ids: ids);
prioritizedIds.addAll(ids); prioritizedIds.addAll(ids);
} }
@ -277,9 +290,9 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> {
final Set<int> latestIds = <int>{}; final Set<int> latestIds = <int>{};
final List<int> ids = await _storiesRepository.fetchStoryIds( final List<int> ids = await _storiesRepository.fetchStoryIds(
of: StoryType.latest, type: StoryType.latest,
); );
await _offlineRepository.cacheStoryIds(of: StoryType.latest, ids: ids); await _offlineRepository.cacheStoryIds(type: StoryType.latest, ids: ids);
latestIds.addAll(ids); latestIds.addAll(ids);
await fetchAndCacheStories( await fetchAndCacheStories(
@ -296,13 +309,41 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> {
} }
} }
Future<void> onCancelDownload(
StoriesCancelDownload event,
Emitter<StoriesState> emit,
) async {
emit(
state.copyWith(
downloadStatus: StoriesDownloadStatus.canceled,
),
);
}
Future<void> fetchAndCacheStories( Future<void> fetchAndCacheStories(
Iterable<int> ids, { Iterable<int> ids, {
required bool includingWebPage, required bool includingWebPage,
required bool isPrioritized, required bool isPrioritized,
}) async { }) async {
final List<StreamSubscription<Comment>> downloadStreams =
<StreamSubscription<Comment>>[];
for (final int id in ids) { for (final int id in ids) {
final Story? story = await _storiesRepository.fetchStoryBy(id); if (state.downloadStatus == StoriesDownloadStatus.canceled) {
_logger.d('aborting downloading');
for (final StreamSubscription<Comment> stream in downloadStreams) {
await stream.cancel();
}
_logger.d('deleting downloaded contents');
await _offlineRepository.deleteAllStoryIds();
await _offlineRepository.deleteAllStories();
await _offlineRepository.deleteAllComments();
break;
}
_logger.d('fetching story $id');
final Story? story = await _storiesRepository.fetchStory(id: id);
if (story == null) { if (story == null) {
if (isPrioritized) { if (isPrioritized) {
@ -325,15 +366,37 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> {
await _offlineRepository.cacheUrl(url: story.url); await _offlineRepository.cacheUrl(url: story.url);
} }
_storiesRepository /// Not awaiting the completion of comments stream because otherwise
/// it's going to take forever to finish downloading all the stories
/// since we need to make a single http call for each comment.
///
/// In other words, we are prioritizing the story itself instead of
/// the comments in the story.
late final StreamSubscription<Comment>? downloadStream;
downloadStream = _storiesRepository
.fetchAllChildrenComments(ids: story.kids) .fetchAllChildrenComments(ids: story.kids)
.whereType<Comment>() .whereType<Comment>()
.listen( .listen(
(Comment comment) => unawaited( (Comment comment) {
_offlineRepository.cacheComment(comment: comment), if (state.downloadStatus == StoriesDownloadStatus.canceled) {
), _logger.d('aborting downloading from comments stream');
) downloadStream?.cancel();
.onDone(() => add(StoryDownloaded(skipped: false))); return;
}
_logger.d('fetched comment ${comment.id}');
unawaited(
_offlineRepository.cacheComment(comment: comment),
);
},
)..onDone(() {
_logger.d(
'''finished downloading story ${story.id} with ${story.descendants} comments''',
);
add(StoryDownloaded(skipped: false));
});
downloadStreams.add(downloadStream);
} }
} }
@ -374,7 +437,6 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> {
StoriesPageSizeChanged event, StoriesPageSizeChanged event,
Emitter<StoriesState> emit, Emitter<StoriesState> emit,
) async { ) async {
emit(const StoriesState.init());
add(StoriesInitialize()); add(StoriesInitialize());
} }
@ -386,7 +448,7 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> {
await _offlineRepository.deleteAllStories(); await _offlineRepository.deleteAllStories();
await _offlineRepository.deleteAllComments(); await _offlineRepository.deleteAllComments();
await _offlineRepository.deleteAllWebPages(); await _offlineRepository.deleteAllWebPages();
emit(state.copyWith(offlineReading: false)); emit(state.copyWith(isOfflineReading: false));
add(StoriesInitialize()); add(StoriesInitialize());
} }
@ -418,7 +480,7 @@ class StoriesBloc extends Bloc<StoriesEvent, StoriesState> {
bool hasRead(Story story) => state.readStoriesIds.contains(story.id); bool hasRead(Story story) => state.readStoriesIds.contains(story.id);
int _getPageSize({required bool isComplexTile}) { int getPageSize({required bool isComplexTile}) {
int pageSize = isComplexTile ? _smallPageSize : _largePageSize; int pageSize = isComplexTile ? _smallPageSize : _largePageSize;
if (deviceScreenType != DeviceScreenType.mobile) { if (deviceScreenType != DeviceScreenType.mobile) {

View File

@ -46,6 +46,13 @@ class StoriesDownload extends StoriesEvent {
List<Object?> get props => <Object?>[includingWebPage]; List<Object?> get props => <Object?>[includingWebPage];
} }
class StoriesCancelDownload extends StoriesEvent {
StoriesCancelDownload();
@override
List<Object?> get props => <Object?>[];
}
class StoryDownloaded extends StoriesEvent { class StoryDownloaded extends StoriesEvent {
StoryDownloaded({required this.skipped}); StoryDownloaded({required this.skipped});

View File

@ -11,6 +11,7 @@ enum StoriesDownloadStatus {
downloading, downloading,
finished, finished,
failure, failure,
canceled,
} }
class StoriesState extends Equatable { class StoriesState extends Equatable {
@ -20,7 +21,7 @@ class StoriesState extends Equatable {
required this.statusByType, required this.statusByType,
required this.currentPageByType, required this.currentPageByType,
required this.readStoriesIds, required this.readStoriesIds,
required this.offlineReading, required this.isOfflineReading,
required this.downloadStatus, required this.downloadStatus,
required this.currentPageSize, required this.currentPageSize,
required this.storiesDownloaded, required this.storiesDownloaded,
@ -34,7 +35,6 @@ class StoriesState extends Equatable {
StoryType.latest: <Story>[], StoryType.latest: <Story>[],
StoryType.ask: <Story>[], StoryType.ask: <Story>[],
StoryType.show: <Story>[], StoryType.show: <Story>[],
StoryType.jobs: <Story>[],
}, },
this.storyIdsByType = const <StoryType, List<int>>{ this.storyIdsByType = const <StoryType, List<int>>{
StoryType.top: <int>[], StoryType.top: <int>[],
@ -42,7 +42,6 @@ class StoriesState extends Equatable {
StoryType.latest: <int>[], StoryType.latest: <int>[],
StoryType.ask: <int>[], StoryType.ask: <int>[],
StoryType.show: <int>[], StoryType.show: <int>[],
StoryType.jobs: <int>[],
}, },
this.statusByType = const <StoryType, StoriesStatus>{ this.statusByType = const <StoryType, StoriesStatus>{
StoryType.top: StoriesStatus.initial, StoryType.top: StoriesStatus.initial,
@ -50,7 +49,6 @@ class StoriesState extends Equatable {
StoryType.latest: StoriesStatus.initial, StoryType.latest: StoriesStatus.initial,
StoryType.ask: StoriesStatus.initial, StoryType.ask: StoriesStatus.initial,
StoryType.show: StoriesStatus.initial, StoryType.show: StoriesStatus.initial,
StoryType.jobs: StoriesStatus.initial,
}, },
this.currentPageByType = const <StoryType, int>{ this.currentPageByType = const <StoryType, int>{
StoryType.top: 0, StoryType.top: 0,
@ -58,9 +56,8 @@ class StoriesState extends Equatable {
StoryType.latest: 0, StoryType.latest: 0,
StoryType.ask: 0, StoryType.ask: 0,
StoryType.show: 0, StoryType.show: 0,
StoryType.jobs: 0,
}, },
}) : offlineReading = false, }) : isOfflineReading = false,
downloadStatus = StoriesDownloadStatus.initial, downloadStatus = StoriesDownloadStatus.initial,
currentPageSize = 0, currentPageSize = 0,
readStoriesIds = const <int>{}, readStoriesIds = const <int>{},
@ -73,7 +70,7 @@ class StoriesState extends Equatable {
final Map<StoryType, int> currentPageByType; final Map<StoryType, int> currentPageByType;
final Set<int> readStoriesIds; final Set<int> readStoriesIds;
final StoriesDownloadStatus downloadStatus; final StoriesDownloadStatus downloadStatus;
final bool offlineReading; final bool isOfflineReading;
final int currentPageSize; final int currentPageSize;
final int storiesDownloaded; final int storiesDownloaded;
final int storiesToBeDownloaded; final int storiesToBeDownloaded;
@ -85,7 +82,7 @@ class StoriesState extends Equatable {
Map<StoryType, int>? currentPageByType, Map<StoryType, int>? currentPageByType,
Set<int>? readStoriesIds, Set<int>? readStoriesIds,
StoriesDownloadStatus? downloadStatus, StoriesDownloadStatus? downloadStatus,
bool? offlineReading, bool? isOfflineReading,
int? currentPageSize, int? currentPageSize,
int? storiesDownloaded, int? storiesDownloaded,
int? storiesToBeDownloaded, int? storiesToBeDownloaded,
@ -96,7 +93,7 @@ class StoriesState extends Equatable {
statusByType: statusByType ?? this.statusByType, statusByType: statusByType ?? this.statusByType,
currentPageByType: currentPageByType ?? this.currentPageByType, currentPageByType: currentPageByType ?? this.currentPageByType,
readStoriesIds: readStoriesIds ?? this.readStoriesIds, readStoriesIds: readStoriesIds ?? this.readStoriesIds,
offlineReading: offlineReading ?? this.offlineReading, isOfflineReading: isOfflineReading ?? this.isOfflineReading,
downloadStatus: downloadStatus ?? this.downloadStatus, downloadStatus: downloadStatus ?? this.downloadStatus,
currentPageSize: currentPageSize ?? this.currentPageSize, currentPageSize: currentPageSize ?? this.currentPageSize,
storiesDownloaded: storiesDownloaded ?? this.storiesDownloaded, storiesDownloaded: storiesDownloaded ?? this.storiesDownloaded,
@ -106,13 +103,13 @@ class StoriesState extends Equatable {
} }
StoriesState copyWithStoryAdded({ StoriesState copyWithStoryAdded({
required StoryType of, required StoryType type,
required Story story, required Story story,
required bool hasRead, required bool hasRead,
}) { }) {
final Map<StoryType, List<Story>> newMap = final Map<StoryType, List<Story>> newMap =
Map<StoryType, List<Story>>.from(storiesByType); Map<StoryType, List<Story>>.from(storiesByType);
newMap[of] = List<Story>.from(newMap[of]!)..add(story); newMap[type] = List<Story>.from(newMap[type]!)..add(story);
return copyWith( return copyWith(
storiesByType: newMap, storiesByType: newMap,
readStoriesIds: <int>{ readStoriesIds: <int>{
@ -123,54 +120,54 @@ class StoriesState extends Equatable {
} }
StoriesState copyWithStoryIdsUpdated({ StoriesState copyWithStoryIdsUpdated({
required StoryType of, required StoryType type,
required List<int> to, required List<int> to,
}) { }) {
final Map<StoryType, List<int>> newMap = final Map<StoryType, List<int>> newMap =
Map<StoryType, List<int>>.from(storyIdsByType); Map<StoryType, List<int>>.from(storyIdsByType);
newMap[of] = to; newMap[type] = to;
return copyWith( return copyWith(
storyIdsByType: newMap, storyIdsByType: newMap,
); );
} }
StoriesState copyWithStatusUpdated({ StoriesState copyWithStatusUpdated({
required StoryType of, required StoryType type,
required StoriesStatus to, required StoriesStatus to,
}) { }) {
final Map<StoryType, StoriesStatus> newMap = final Map<StoryType, StoriesStatus> newMap =
Map<StoryType, StoriesStatus>.from(statusByType); Map<StoryType, StoriesStatus>.from(statusByType);
newMap[of] = to; newMap[type] = to;
return copyWith( return copyWith(
statusByType: newMap, statusByType: newMap,
); );
} }
StoriesState copyWithCurrentPageUpdated({ StoriesState copyWithCurrentPageUpdated({
required StoryType of, required StoryType type,
required int to, required int to,
}) { }) {
final Map<StoryType, int> newMap = final Map<StoryType, int> newMap =
Map<StoryType, int>.from(currentPageByType); Map<StoryType, int>.from(currentPageByType);
newMap[of] = to; newMap[type] = to;
return copyWith( return copyWith(
currentPageByType: newMap, currentPageByType: newMap,
); );
} }
StoriesState copyWithRefreshed({required StoryType of}) { StoriesState copyWithRefreshed({required StoryType type}) {
final Map<StoryType, List<Story>> newStoriesMap = final Map<StoryType, List<Story>> newStoriesMap =
Map<StoryType, List<Story>>.from(storiesByType); Map<StoryType, List<Story>>.from(storiesByType);
newStoriesMap[of] = <Story>[]; newStoriesMap[type] = <Story>[];
final Map<StoryType, List<int>> newStoryIdsMap = final Map<StoryType, List<int>> newStoryIdsMap =
Map<StoryType, List<int>>.from(storyIdsByType); Map<StoryType, List<int>>.from(storyIdsByType);
newStoryIdsMap[of] = <int>[]; newStoryIdsMap[type] = <int>[];
final Map<StoryType, StoriesStatus> newStatusMap = final Map<StoryType, StoriesStatus> newStatusMap =
Map<StoryType, StoriesStatus>.from(statusByType); Map<StoryType, StoriesStatus>.from(statusByType);
newStatusMap[of] = StoriesStatus.loading; newStatusMap[type] = StoriesStatus.loading;
final Map<StoryType, int> newCurrentPageMap = final Map<StoryType, int> newCurrentPageMap =
Map<StoryType, int>.from(currentPageByType); Map<StoryType, int>.from(currentPageByType);
newCurrentPageMap[of] = 0; newCurrentPageMap[type] = 0;
return copyWith( return copyWith(
storiesByType: newStoriesMap, storiesByType: newStoriesMap,
storyIdsByType: newStoryIdsMap, storyIdsByType: newStoryIdsMap,
@ -186,7 +183,7 @@ class StoriesState extends Equatable {
statusByType, statusByType,
currentPageByType, currentPageByType,
readStoriesIds, readStoriesIds,
offlineReading, isOfflineReading,
downloadStatus, downloadStatus,
currentPageSize, currentPageSize,
storiesDownloaded, storiesDownloaded,

View File

@ -1,9 +1,13 @@
import 'package:hacki/extensions/extensions.dart';
abstract class Constants { abstract class Constants {
static const String endUserAgreementLink = static const String endUserAgreementLink =
'https://www.termsfeed.com/live/c1417f5c-a48b-4bd7-93b2-9cd4577bfc45'; 'https://github.com/Livinglist/Hacki/blob/master/assets/eula.md';
static const String privacyPolicyLink =
'https://github.com/Livinglist/Hacki/blob/master/assets/privacy_policy.md';
static const String hackerNewsLogoLink = static const String hackerNewsLogoLink =
'https://pbs.twimg.com/profile_images/469397708986269696/iUrYEOpJ_400x400.png'; 'https://pbs.twimg.com/profile_images/469397708986269696/iUrYEOpJ_400x400.png';
static const String portfolioLink = 'https://livinglist.github.io'; static const String portfolioLink = 'https://github.com/Livinglist';
static const String githubLink = 'https://github.com/Livinglist/Hacki'; static const String githubLink = 'https://github.com/Livinglist/Hacki';
static const String appStoreLink = static const String appStoreLink =
'https://apps.apple.com/us/app/hacki/id1602043763?action=write-review'; 'https://apps.apple.com/us/app/hacki/id1602043763?action=write-review';
@ -12,6 +16,11 @@ abstract class Constants {
static const String sponsorLink = 'https://github.com/sponsors/Livinglist'; static const String sponsorLink = 'https://github.com/sponsors/Livinglist';
static const String guidelineLink = static const String guidelineLink =
'https://news.ycombinator.com/newsguidelines.html'; 'https://news.ycombinator.com/newsguidelines.html';
static const String githubIssueLink =
'$githubLink/issues/new?title=Found+a+bug+in+Hacki&body=Please+describe+the+problem.';
static const String wikipediaLink = 'https://en.wikipedia.org/wiki/';
static const String wiktionaryLink = 'https://en.wiktionary.org/wiki/';
static const String supportEmail = 'georgefung98@gmail.com';
static const String _imagePath = 'assets/images'; static const String _imagePath = 'assets/images';
static const String hackerNewsLogoPath = '$_imagePath/hacker_news_logo.png'; static const String hackerNewsLogoPath = '$_imagePath/hacker_news_logo.png';
@ -22,23 +31,27 @@ abstract class Constants {
'$_imagePath/comment_tile_right_slide.png'; '$_imagePath/comment_tile_right_slide.png';
static const String commentTileTopTapPath = static const String commentTileTopTapPath =
'$_imagePath/comment_tile_top_tap.png'; '$_imagePath/comment_tile_top_tap.png';
static const String logFilename = 'hacki_log.txt';
static const String previousLogFileName = 'old_hacki_log.txt';
/// Feature ids for feature discovery. /// Feature ids for feature discovery.
static const String featureAddStoryToFavList = 'add_story_to_fav_list'; static const String featureAddStoryToFavList = 'add_story_to_fav_list';
static const String featureOpenStoryInWebView = 'open_story_in_web_view'; static const String featureOpenStoryInWebView = 'open_story_in_web_view';
static const String featureLogIn = 'log_in'; static const String featureLogIn = 'log_in';
static const String featurePinToTop = 'pin_to_top'; static const String featurePinToTop = 'pin_to_top';
static const String featureJumpUpButton = 'jump_up_button';
static const String featureJumpDownButton = 'jump_down_button';
static const List<String> happyFaces = <String>[ static final String happyFace = <String>[
'(๑•̀ㅂ•́)و✧', '(๑•̀ㅂ•́)و✧',
'( ͡• ͜ʖ ͡•)', '( ͡• ͜ʖ ͡•)',
'( ͡~ ͜ʖ ͡°)', '( ͡~ ͜ʖ ͡°)',
'٩(˘◡˘)۶', '٩(˘◡˘)۶',
'(─‿‿─)', '(─‿‿─)',
'(¬‿¬)', '(¬‿¬)',
]; ].pickRandomly()!;
static const List<String> sadFaces = <String>[ static final String sadFace = <String>[
'ಥ_ಥ', 'ಥ_ಥ',
'(╯°□°)╯︵ ┻━┻', '(╯°□°)╯︵ ┻━┻',
r'¯\_(ツ)_/¯', r'¯\_(ツ)_/¯',
@ -48,10 +61,20 @@ abstract class Constants {
'(ㆆ_ㆆ)', '(ㆆ_ㆆ)',
'ʕ•́ᴥ•̀ʔっ', 'ʕ•́ᴥ•̀ʔっ',
'(ㆆ_ㆆ)', '(ㆆ_ㆆ)',
]; ].pickRandomly()!;
static final String magicWord = <String>[
'to be over the rainbow!',
'to infinity and beyond!',
'to see the future.',
].pickRandomly()!;
static final String errorMessage = 'Something went wrong...$sadFace';
static final String loginErrorMessage =
'''Failed to log in $sadFace, this could happen if your account requires a CAPTCHA, please try logging in inside a browser to see if this is the case, if so, you may try logging in here again later after CAPTCHA is no longer needed.''';
} }
abstract class RegExpConstants { abstract class RegExpConstants {
static const String linkSuffix = r'(\)|])(.)*$'; static const String linkSuffix = r'(\)|]|,|\*)(.)*$';
static const String number = '[0-9]+'; static const String number = '[0-9]+';
} }

View File

@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:hacki/config/constants.dart';
import 'package:hacki/screens/screens.dart'; import 'package:hacki/screens/screens.dart';
/// Custom router. /// Custom router.
@ -39,8 +40,8 @@ class CustomRouter {
appBar: AppBar( appBar: AppBar(
title: const Text('Error'), title: const Text('Error'),
), ),
body: const Center( body: Center(
child: Text('Something went wrong!'), child: Text(Constants.errorMessage),
), ),
), ),
); );

View File

@ -0,0 +1,41 @@
import 'dart:convert';
import 'dart:io';
import 'package:logger/logger.dart';
/// Writes the log output to a file.
/// Temporary solution to not being able to access
// ignore: comment_references
/// the original [FileOutput] from [Logger]
class CustomFileOutput extends LogOutput {
CustomFileOutput({
required this.file,
this.overrideExisting = false,
this.encoding = utf8,
});
final File file;
final bool overrideExisting;
final Encoding encoding;
IOSink? _sink;
@override
void init() {
_sink = file.openWrite(
mode: overrideExisting ? FileMode.writeOnly : FileMode.writeOnlyAppend,
encoding: encoding,
);
}
@override
void output(OutputEvent event) {
_sink?.writeAll(event.lines, '\n');
_sink?.writeln();
}
@override
Future<void> destroy() async {
await _sink?.flush();
await _sink?.close();
}
}

View File

@ -1,8 +1,11 @@
import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart'; import 'package:get_it/get_it.dart';
import 'package:hacki/config/custom_log_filter.dart'; import 'package:hacki/config/custom_log_filter.dart';
import 'package:hacki/repositories/repositories.dart'; import 'package:hacki/repositories/repositories.dart';
import 'package:hacki/services/services.dart'; import 'package:hacki/services/services.dart';
import 'package:hacki/utils/utils.dart';
import 'package:logger/logger.dart'; import 'package:logger/logger.dart';
/// Global [GetIt.instance]. /// Global [GetIt.instance].
@ -10,8 +13,16 @@ final GetIt locator = GetIt.instance;
/// Set up [GetIt] locator. /// Set up [GetIt] locator.
Future<void> setUpLocator() async { Future<void> setUpLocator() async {
final File logOutputFile = await LogUtil.initLogFile();
locator locator
..registerSingleton<Logger>(Logger(filter: CustomLogFilter())) ..registerSingleton<Logger>(
Logger(
filter: CustomLogFilter(),
printer: LogUtil.logPrinter,
output: LogUtil.logOutput(logOutputFile),
),
)
..registerSingleton<StoriesRepository>(StoriesRepository()) ..registerSingleton<StoriesRepository>(StoriesRepository())
..registerSingleton<PreferenceRepository>(PreferenceRepository()) ..registerSingleton<PreferenceRepository>(PreferenceRepository())
..registerSingleton<SearchRepository>(SearchRepository()) ..registerSingleton<SearchRepository>(SearchRepository())
@ -21,7 +32,8 @@ Future<void> setUpLocator() async {
..registerSingleton<OfflineRepository>(OfflineRepository()) ..registerSingleton<OfflineRepository>(OfflineRepository())
..registerSingleton<DraftCache>(DraftCache()) ..registerSingleton<DraftCache>(DraftCache())
..registerSingleton<CommentCache>(CommentCache()) ..registerSingleton<CommentCache>(CommentCache())
..registerSingleton<LocalNotification>(LocalNotification()) ..registerSingleton<LocalNotificationService>(LocalNotificationService())
..registerSingleton(AppReviewService())
..registerSingleton<RouteObserver<ModalRoute<dynamic>>>( ..registerSingleton<RouteObserver<ModalRoute<dynamic>>>(
RouteObserver<ModalRoute<dynamic>>(), RouteObserver<ModalRoute<dynamic>>(),
); );

View File

@ -3,7 +3,6 @@ import 'dart:async';
import 'package:bloc/bloc.dart'; import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart'; import 'package:equatable/equatable.dart';
import 'package:hacki/config/locator.dart'; import 'package:hacki/config/locator.dart';
import 'package:hacki/cubits/cubits.dart';
import 'package:hacki/services/services.dart'; import 'package:hacki/services/services.dart';
part 'collapse_state.dart'; part 'collapse_state.dart';
@ -11,16 +10,13 @@ part 'collapse_state.dart';
class CollapseCubit extends Cubit<CollapseState> { class CollapseCubit extends Cubit<CollapseState> {
CollapseCubit({ CollapseCubit({
required int commentId, required int commentId,
required CommentsCubit? commentsCubit,
CollapseCache? collapseCache, CollapseCache? collapseCache,
}) : _commentId = commentId, }) : _commentId = commentId,
_collapseCache = collapseCache ?? locator.get<CollapseCache>(), _collapseCache = collapseCache ?? locator.get<CollapseCache>(),
_commentsCubit = commentsCubit,
super(const CollapseState.init()); super(const CollapseState.init());
final int _commentId; final int _commentId;
final CollapseCache _collapseCache; final CollapseCache _collapseCache;
final CommentsCubit? _commentsCubit;
late final StreamSubscription<Map<int, Set<int>>> _streamSubscription; late final StreamSubscription<Map<int, Set<int>>> _streamSubscription;
void init() { void init() {
@ -47,16 +43,7 @@ class CollapseCubit extends Cubit<CollapseState> {
), ),
); );
} else { } else {
if (_commentsCubit == null) return;
final Set<int> collapsedCommentIds = _collapseCache.collapse(_commentId); final Set<int> collapsedCommentIds = _collapseCache.collapse(_commentId);
final int lastCommentId = _commentsCubit!.state.comments.last.id;
final bool shouldLoadMore = _commentId == lastCommentId ||
collapsedCommentIds.contains(lastCommentId);
if (shouldLoadMore) {
_commentsCubit!.loadMore();
}
emit( emit(
state.copyWith( state.copyWith(

View File

@ -1,33 +1,40 @@
import 'dart:async'; import 'dart:async';
import 'dart:math';
import 'package:bloc/bloc.dart'; import 'package:bloc/bloc.dart';
import 'package:collection/collection.dart'; import 'package:collection/collection.dart';
import 'package:equatable/equatable.dart'; import 'package:equatable/equatable.dart';
import 'package:flutter/services.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter_linkify/flutter_linkify.dart';
import 'package:hacki/config/locator.dart'; import 'package:hacki/config/locator.dart';
import 'package:hacki/cubits/cubits.dart';
import 'package:hacki/main.dart'; import 'package:hacki/main.dart';
import 'package:hacki/models/models.dart'; import 'package:hacki/models/models.dart';
import 'package:hacki/repositories/repositories.dart'; import 'package:hacki/repositories/repositories.dart';
import 'package:hacki/screens/screens.dart'; import 'package:hacki/screens/screens.dart';
import 'package:hacki/services/services.dart'; import 'package:hacki/services/services.dart';
import 'package:hacki/utils/utils.dart';
import 'package:linkify/linkify.dart';
import 'package:logger/logger.dart'; import 'package:logger/logger.dart';
import 'package:rxdart/rxdart.dart';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
part 'comments_state.dart'; part 'comments_state.dart';
class CommentsCubit extends Cubit<CommentsState> { class CommentsCubit extends Cubit<CommentsState> {
CommentsCubit({ CommentsCubit({
required FilterCubit filterCubit,
required CollapseCache collapseCache, required CollapseCache collapseCache,
CommentCache? commentCache, CommentCache? commentCache,
OfflineRepository? offlineRepository, OfflineRepository? offlineRepository,
StoriesRepository? storiesRepository, StoriesRepository? storiesRepository,
SembastRepository? sembastRepository, SembastRepository? sembastRepository,
Logger? logger, Logger? logger,
required bool offlineReading, required bool isOfflineReading,
required Item item, required Item item,
required FetchMode defaultFetchMode, required FetchMode defaultFetchMode,
required CommentsOrder defaultCommentsOrder, required CommentsOrder defaultCommentsOrder,
}) : _collapseCache = collapseCache, }) : _filterCubit = filterCubit,
_collapseCache = collapseCache,
_commentCache = commentCache ?? locator.get<CommentCache>(), _commentCache = commentCache ?? locator.get<CommentCache>(),
_offlineRepository = _offlineRepository =
offlineRepository ?? locator.get<OfflineRepository>(), offlineRepository ?? locator.get<OfflineRepository>(),
@ -38,13 +45,14 @@ class CommentsCubit extends Cubit<CommentsState> {
_logger = logger ?? locator.get<Logger>(), _logger = logger ?? locator.get<Logger>(),
super( super(
CommentsState.init( CommentsState.init(
offlineReading: offlineReading, isOfflineReading: isOfflineReading,
item: item, item: item,
fetchMode: defaultFetchMode, fetchMode: defaultFetchMode,
order: defaultCommentsOrder, order: defaultCommentsOrder,
), ),
); );
final FilterCubit _filterCubit;
final CollapseCache _collapseCache; final CollapseCache _collapseCache;
final CommentCache _commentCache; final CommentCache _commentCache;
final OfflineRepository _offlineRepository; final OfflineRepository _offlineRepository;
@ -61,8 +69,6 @@ class CommentsCubit extends Cubit<CommentsState> {
final Map<int, StreamSubscription<Comment>> _streamSubscriptions = final Map<int, StreamSubscription<Comment>> _streamSubscriptions =
<int, StreamSubscription<Comment>>{}; <int, StreamSubscription<Comment>>{};
static const int _pageSize = 20;
@override @override
void emit(CommentsState state) { void emit(CommentsState state) {
if (!isClosed) { if (!isClosed) {
@ -73,12 +79,12 @@ class CommentsCubit extends Cubit<CommentsState> {
Future<void> init({ Future<void> init({
bool onlyShowTargetComment = false, bool onlyShowTargetComment = false,
bool useCommentCache = false, bool useCommentCache = false,
List<Comment>? targetParents, List<Comment>? targetAncestors,
}) async { }) async {
if (onlyShowTargetComment && (targetParents?.isNotEmpty ?? false)) { if (onlyShowTargetComment && (targetAncestors?.isNotEmpty ?? false)) {
emit( emit(
state.copyWith( state.copyWith(
comments: targetParents, comments: targetAncestors,
onlyShowTargetComment: true, onlyShowTargetComment: true,
status: CommentsStatus.allLoaded, status: CommentsStatus.allLoaded,
), ),
@ -86,9 +92,11 @@ class CommentsCubit extends Cubit<CommentsState> {
_streamSubscription = _storiesRepository _streamSubscription = _storiesRepository
.fetchAllCommentsRecursivelyStream( .fetchAllCommentsRecursivelyStream(
ids: targetParents!.last.kids, ids: targetAncestors!.last.kids,
level: targetParents.last.level + 1, level: targetAncestors.last.level + 1,
) )
.asyncMap(_toBuildableComment)
.whereNotNull()
.listen(_onCommentFetched) .listen(_onCommentFetched)
..onDone(_onDone); ..onDone(_onDone);
@ -104,40 +112,40 @@ class CommentsCubit extends Cubit<CommentsState> {
); );
final Item item = state.item; final Item item = state.item;
final Item updatedItem = state.offlineReading final Item updatedItem = state.isOfflineReading
? item ? item
: await _storiesRepository.fetchItemBy(id: item.id) ?? item; : await _storiesRepository.fetchItem(id: item.id).then(_toBuildable) ??
final List<int> kids = sortKids(updatedItem.kids); item;
final List<int> kids = _sortKids(updatedItem.kids);
emit(state.copyWith(item: updatedItem)); emit(state.copyWith(item: updatedItem));
if (state.offlineReading) { late final Stream<Comment> commentStream;
_streamSubscription = _offlineRepository
.getCachedCommentsStream(ids: kids) if (state.isOfflineReading) {
.listen(_onCommentFetched) commentStream = _offlineRepository.getCachedCommentsStream(ids: kids);
..onDone(_onDone);
} else { } else {
switch (state.fetchMode) { switch (state.fetchMode) {
case FetchMode.lazy: case FetchMode.lazy:
_streamSubscription = _storiesRepository commentStream = _storiesRepository.fetchCommentsStream(
.fetchCommentsStream( ids: kids,
ids: kids, getFromCache: useCommentCache ? _commentCache.getComment : null,
getFromCache: useCommentCache ? _commentCache.getComment : null, );
)
.listen(_onCommentFetched)
..onDone(_onDone);
break; break;
case FetchMode.eager: case FetchMode.eager:
_streamSubscription = _storiesRepository commentStream = _storiesRepository.fetchAllCommentsRecursivelyStream(
.fetchAllCommentsRecursivelyStream( ids: kids,
ids: kids, getFromCache: useCommentCache ? _commentCache.getComment : null,
getFromCache: useCommentCache ? _commentCache.getComment : null, );
)
.listen(_onCommentFetched)
..onDone(_onDone);
break; break;
} }
} }
_streamSubscription = commentStream
.asyncMap(_toBuildableComment)
.whereNotNull()
.listen(_onCommentFetched)
..onDone(_onDone);
} }
Future<void> refresh() async { Future<void> refresh() async {
@ -147,7 +155,7 @@ class CommentsCubit extends Cubit<CommentsState> {
), ),
); );
if (state.offlineReading) { if (state.isOfflineReading) {
emit( emit(
state.copyWith( state.copyWith(
status: CommentsStatus.allLoaded, status: CommentsStatus.allLoaded,
@ -173,25 +181,26 @@ class CommentsCubit extends Cubit<CommentsState> {
final Item item = state.item; final Item item = state.item;
final Item updatedItem = final Item updatedItem =
await _storiesRepository.fetchItemBy(id: item.id) ?? item; await _storiesRepository.fetchItem(id: item.id) ?? item;
final List<int> kids = sortKids(updatedItem.kids); final List<int> kids = _sortKids(updatedItem.kids);
late final Stream<Comment> commentStream;
if (state.fetchMode == FetchMode.lazy) { if (state.fetchMode == FetchMode.lazy) {
_streamSubscription = _storiesRepository commentStream = _storiesRepository.fetchCommentsStream(
.fetchCommentsStream( ids: kids,
ids: kids, );
)
.listen(_onCommentFetched)
..onDone(_onDone);
} else { } else {
_streamSubscription = _storiesRepository commentStream = _storiesRepository.fetchAllCommentsRecursivelyStream(
.fetchAllCommentsRecursivelyStream( ids: kids,
ids: kids, );
)
.listen(_onCommentFetched)
..onDone(_onDone);
} }
_streamSubscription = commentStream
.asyncMap(_toBuildableComment)
.whereNotNull()
.listen(_onCommentFetched)
..onDone(_onDone);
emit( emit(
state.copyWith( state.copyWith(
item: updatedItem, item: updatedItem,
@ -200,7 +209,7 @@ class CommentsCubit extends Cubit<CommentsState> {
} }
void loadAll(Story story) { void loadAll(Story story) {
HapticFeedback.lightImpact(); HapticFeedbackUtil.light();
emit( emit(
state.copyWith( state.copyWith(
onlyShowTargetComment: false, onlyShowTargetComment: false,
@ -211,7 +220,11 @@ class CommentsCubit extends Cubit<CommentsState> {
} }
/// [comment] is only used for lazy fetching. /// [comment] is only used for lazy fetching.
void loadMore({Comment? comment}) { void loadMore({
Comment? comment,
void Function(Comment)? onCommentFetched,
VoidCallback? onDone,
}) {
if (comment == null && state.status == CommentsStatus.loading) return; if (comment == null && state.status == CommentsStatus.loading) return;
switch (state.fetchMode) { switch (state.fetchMode) {
@ -227,23 +240,18 @@ class CommentsCubit extends Cubit<CommentsState> {
final StreamSubscription<Comment> streamSubscription = final StreamSubscription<Comment> streamSubscription =
_storiesRepository _storiesRepository
.fetchCommentsStream(ids: comment.kids) .fetchCommentsStream(ids: comment.kids)
.asyncMap(_toBuildableComment)
.whereNotNull()
.listen((Comment cmt) { .listen((Comment cmt) {
_collapseCache.addKid(cmt.id, to: cmt.parent); _collapseCache.addKid(cmt.id, to: cmt.parent);
_commentCache.cacheComment(cmt); _commentCache.cacheComment(cmt);
_sembastRepository.cacheComment(cmt); _sembastRepository.cacheComment(cmt);
final List<LinkifyElement> elements = _linkify(
cmt.text,
);
final BuildableComment buildableComment =
BuildableComment.fromComment(cmt, elements: elements);
emit( emit(
state.copyWith( state.copyWith(
comments: <Comment>[...state.comments]..insert( comments: <Comment>[...state.comments]..insert(
state.comments.indexOf(comment) + offset + 1, state.comments.indexOf(comment) + offset + 1,
buildableComment.copyWith(level: level), cmt.copyWith(level: level),
), ),
), ),
); );
@ -264,17 +272,19 @@ class CommentsCubit extends Cubit<CommentsState> {
case FetchMode.eager: case FetchMode.eager:
if (_streamSubscription != null) { if (_streamSubscription != null) {
emit(state.copyWith(status: CommentsStatus.loading)); emit(state.copyWith(status: CommentsStatus.loading));
_streamSubscription?.resume(); _streamSubscription
?..resume()
..onData(onCommentFetched);
} }
break; break;
} }
} }
Future<void> loadParentThread() async { Future<void> loadParentThread() async {
unawaited(HapticFeedback.lightImpact()); HapticFeedbackUtil.light();
emit(state.copyWith(fetchParentStatus: CommentsStatus.loading)); emit(state.copyWith(fetchParentStatus: CommentsStatus.loading));
final Story? parent = final Item? parent =
await _storiesRepository.fetchParentStory(id: state.item.id); await _storiesRepository.fetchItem(id: state.item.parent);
if (parent == null) { if (parent == null) {
return; return;
@ -292,10 +302,33 @@ class CommentsCubit extends Cubit<CommentsState> {
} }
} }
Future<void> loadRootThread() async {
HapticFeedbackUtil.light();
emit(state.copyWith(fetchRootStatus: CommentsStatus.loading));
final Story? parent = await _storiesRepository
.fetchParentStory(id: state.item.id)
.then(_toBuildableStory);
if (parent == null) {
return;
} else {
await HackiApp.navigatorKey.currentState?.pushNamed(
ItemScreen.routeName,
arguments: ItemScreenArgs(item: parent),
);
emit(
state.copyWith(
fetchRootStatus: CommentsStatus.loaded,
),
);
}
}
void onOrderChanged(CommentsOrder? order) { void onOrderChanged(CommentsOrder? order) {
if (order == null) return; if (order == null) return;
if (state.order == order) return; if (state.order == order) return;
HapticFeedback.selectionClick(); HapticFeedbackUtil.selection();
_streamSubscription?.cancel(); _streamSubscription?.cancel();
for (final StreamSubscription<Comment> s in _streamSubscriptions.values) { for (final StreamSubscription<Comment> s in _streamSubscriptions.values) {
s.cancel(); s.cancel();
@ -309,7 +342,7 @@ class CommentsCubit extends Cubit<CommentsState> {
if (fetchMode == null) return; if (fetchMode == null) return;
if (state.fetchMode == fetchMode) return; if (state.fetchMode == fetchMode) return;
_collapseCache.resetCollapsedComments(); _collapseCache.resetCollapsedComments();
HapticFeedback.selectionClick(); HapticFeedbackUtil.selection();
_streamSubscription?.cancel(); _streamSubscription?.cancel();
for (final StreamSubscription<Comment> s in _streamSubscriptions.values) { for (final StreamSubscription<Comment> s in _streamSubscriptions.values) {
s.cancel(); s.cancel();
@ -319,7 +352,82 @@ class CommentsCubit extends Cubit<CommentsState> {
init(useCommentCache: true); init(useCommentCache: true);
} }
List<int> sortKids(List<int> kids) { void jump(
ItemScrollController itemScrollController,
ItemPositionsListener itemPositionsListener,
) {
final int totalComments = state.comments.length;
final List<Comment> onScreenComments = itemPositionsListener
.itemPositions.value
// The header is also a part of the list view,
// thus ignoring it here.
.where((ItemPosition e) => e.index >= 1 && e.itemLeadingEdge < 0.7)
.sorted((ItemPosition a, ItemPosition b) => a.index.compareTo(b.index))
.map(
(ItemPosition e) => e.index <= state.comments.length
? state.comments.elementAt(e.index - 1)
: null,
)
.whereNotNull()
.toList();
/// The index of last comment visible on screen.
final int lastVisibleIndex = state.comments.indexOf(onScreenComments.last);
final int startIndex = min(lastVisibleIndex + 1, totalComments);
for (int i = startIndex; i < totalComments; i++) {
final Comment cmt = state.comments.elementAt(i);
if (cmt.isRoot && (cmt.deleted || cmt.dead) == false) {
itemScrollController.scrollTo(
index: i + 1,
alignment: 0.15,
duration: const Duration(milliseconds: 400),
);
return;
}
}
}
void jumpUp(
ItemScrollController itemScrollController,
ItemPositionsListener itemPositionsListener,
) {
final List<Comment> onScreenComments = itemPositionsListener
.itemPositions.value
// The header is also a part of the list view,
// thus ignoring it here.
.where((ItemPosition e) => e.index >= 1 && e.itemLeadingEdge > 0)
.sorted((ItemPosition a, ItemPosition b) => a.index.compareTo(b.index))
.map(
(ItemPosition e) => e.index <= state.comments.length
? state.comments.elementAt(e.index - 1)
: null,
)
.whereNotNull()
.toList();
/// The index of first comment visible on screen.
final int firstVisibleIndex = state.comments.indexOf(
onScreenComments.firstOrNull ?? state.comments.last,
);
final int startIndex = max(0, firstVisibleIndex - 1);
for (int i = startIndex; i >= 0; i--) {
final Comment cmt = state.comments.elementAt(i);
if (cmt.isRoot && (cmt.deleted || cmt.dead) == false) {
itemScrollController.scrollTo(
index: i + 1,
alignment: 0.15,
duration: const Duration(milliseconds: 400),
);
return;
}
}
}
List<int> _sortKids(List<int> kids) {
switch (state.order) { switch (state.order) {
case CommentsOrder.natural: case CommentsOrder.natural:
return kids; return kids;
@ -340,76 +448,69 @@ class CommentsCubit extends Cubit<CommentsState> {
); );
} }
void _onCommentFetched(Comment? comment) { void _onCommentFetched(BuildableComment? comment) {
if (comment != null) { if (comment != null) {
_collapseCache.addKid(comment.id, to: comment.parent); _collapseCache.addKid(comment.id, to: comment.parent);
_commentCache.cacheComment(comment); _commentCache.cacheComment(comment);
_sembastRepository.cacheComment(comment); _sembastRepository.cacheComment(comment);
final List<LinkifyElement> elements = _linkify( final bool hidden = _filterCubit.state.keywords.any(
comment.text, (String keyword) => comment.text.toLowerCase().contains(keyword),
); );
final BuildableComment buildableComment =
BuildableComment.fromComment(comment, elements: elements);
final List<Comment> updatedComments = <Comment>[ final List<Comment> updatedComments = <Comment>[
...state.comments, ...state.comments,
buildableComment comment.copyWith(hidden: hidden),
]; ];
emit(state.copyWith(comments: updatedComments)); emit(state.copyWith(comments: updatedComments));
if (state.fetchMode == FetchMode.eager) {
if (updatedComments.length >=
_pageSize + _pageSize * state.currentPage &&
updatedComments.length <=
_pageSize * 2 + _pageSize * state.currentPage) {
final bool isHidden = _collapseCache.isHidden(comment.id);
if (!isHidden) {
_streamSubscription?.pause();
emit(
state.copyWith(
status: CommentsStatus.loaded,
),
);
}
emit(
state.copyWith(
currentPage: state.currentPage + 1,
),
);
}
}
} }
} }
static List<LinkifyElement> _linkify( static Future<Item?> _toBuildable(Item? item) async {
String text, { if (item == null) return null;
LinkifyOptions options = const LinkifyOptions(),
List<Linkifier> linkifiers = const <Linkifier>[
UrlLinkifier(),
EmailLinkifier(),
],
}) {
List<LinkifyElement> list = <LinkifyElement>[TextElement(text)];
if (text.isEmpty) { switch (item.runtimeType) {
return <LinkifyElement>[]; case Comment:
return _toBuildableComment(item as Comment);
case Story:
return _toBuildableStory(item as Story);
} }
if (linkifiers.isEmpty) { return null;
return list; }
static Future<BuildableComment?> _toBuildableComment(Comment? comment) async {
if (comment == null) return null;
final List<LinkifyElement> elements =
await compute<String, List<LinkifyElement>>(
LinkifierUtil.linkify,
comment.text,
);
final BuildableComment buildableComment =
BuildableComment.fromComment(comment, elements: elements);
return buildableComment;
}
static Future<BuildableStory?> _toBuildableStory(Story? story) async {
if (story == null) {
return null;
} else if (story.text.isEmpty) {
return BuildableStory.fromTitleOnlyStory(story);
} }
for (final Linkifier linkifier in linkifiers) { final List<LinkifyElement> elements =
list = linkifier.parse(list, options); await compute<String, List<LinkifyElement>>(
} LinkifierUtil.linkify,
story.text,
);
return list; final BuildableStory buildableStory =
BuildableStory.fromStory(story, elements: elements);
return buildableStory;
} }
@override @override

View File

@ -14,21 +14,23 @@ class CommentsState extends Equatable {
required this.comments, required this.comments,
required this.status, required this.status,
required this.fetchParentStatus, required this.fetchParentStatus,
required this.fetchRootStatus,
required this.order, required this.order,
required this.fetchMode, required this.fetchMode,
required this.onlyShowTargetComment, required this.onlyShowTargetComment,
required this.offlineReading, required this.isOfflineReading,
required this.currentPage, required this.currentPage,
}); });
CommentsState.init({ CommentsState.init({
required this.offlineReading, required this.isOfflineReading,
required this.item, required this.item,
required this.fetchMode, required this.fetchMode,
required this.order, required this.order,
}) : comments = <Comment>[], }) : comments = <Comment>[],
status = CommentsStatus.init, status = CommentsStatus.init,
fetchParentStatus = CommentsStatus.init, fetchParentStatus = CommentsStatus.init,
fetchRootStatus = CommentsStatus.init,
onlyShowTargetComment = false, onlyShowTargetComment = false,
currentPage = 0; currentPage = 0;
@ -36,10 +38,11 @@ class CommentsState extends Equatable {
final List<Comment> comments; final List<Comment> comments;
final CommentsStatus status; final CommentsStatus status;
final CommentsStatus fetchParentStatus; final CommentsStatus fetchParentStatus;
final CommentsStatus fetchRootStatus;
final CommentsOrder order; final CommentsOrder order;
final FetchMode fetchMode; final FetchMode fetchMode;
final bool onlyShowTargetComment; final bool onlyShowTargetComment;
final bool offlineReading; final bool isOfflineReading;
final int currentPage; final int currentPage;
CommentsState copyWith({ CommentsState copyWith({
@ -47,22 +50,24 @@ class CommentsState extends Equatable {
List<Comment>? comments, List<Comment>? comments,
CommentsStatus? status, CommentsStatus? status,
CommentsStatus? fetchParentStatus, CommentsStatus? fetchParentStatus,
CommentsStatus? fetchRootStatus,
CommentsOrder? order, CommentsOrder? order,
FetchMode? fetchMode, FetchMode? fetchMode,
bool? onlyShowTargetComment, bool? onlyShowTargetComment,
bool? offlineReading, bool? isOfflineReading,
int? currentPage, int? currentPage,
}) { }) {
return CommentsState( return CommentsState(
item: item ?? this.item, item: item ?? this.item,
comments: comments ?? this.comments, comments: comments ?? this.comments,
fetchParentStatus: fetchParentStatus ?? this.fetchParentStatus, fetchParentStatus: fetchParentStatus ?? this.fetchParentStatus,
fetchRootStatus: fetchRootStatus ?? this.fetchRootStatus,
status: status ?? this.status, status: status ?? this.status,
order: order ?? this.order, order: order ?? this.order,
fetchMode: fetchMode ?? this.fetchMode, fetchMode: fetchMode ?? this.fetchMode,
onlyShowTargetComment: onlyShowTargetComment:
onlyShowTargetComment ?? this.onlyShowTargetComment, onlyShowTargetComment ?? this.onlyShowTargetComment,
offlineReading: offlineReading ?? this.offlineReading, isOfflineReading: isOfflineReading ?? this.isOfflineReading,
currentPage: currentPage ?? this.currentPage, currentPage: currentPage ?? this.currentPage,
); );
} }
@ -72,13 +77,14 @@ class CommentsState extends Equatable {
@override @override
List<Object?> get props => <Object?>[ List<Object?> get props => <Object?>[
item, item,
comments,
status, status,
fetchParentStatus, fetchParentStatus,
fetchRootStatus,
order, order,
fetchMode, fetchMode,
onlyShowTargetComment, onlyShowTargetComment,
offlineReading, isOfflineReading,
currentPage, currentPage,
comments,
]; ];
} }

View File

@ -3,6 +3,7 @@ export 'collapse/collapse_cubit.dart';
export 'comments/comments_cubit.dart'; export 'comments/comments_cubit.dart';
export 'edit/edit_cubit.dart'; export 'edit/edit_cubit.dart';
export 'fav/fav_cubit.dart'; export 'fav/fav_cubit.dart';
export 'filter/filter_cubit.dart';
export 'history/history_cubit.dart'; export 'history/history_cubit.dart';
export 'notification/notification_cubit.dart'; export 'notification/notification_cubit.dart';
export 'pin/pin_cubit.dart'; export 'pin/pin_cubit.dart';
@ -13,6 +14,7 @@ export 'reminder/reminder_cubit.dart';
export 'search/search_cubit.dart'; export 'search/search_cubit.dart';
export 'split_view/split_view_cubit.dart'; export 'split_view/split_view_cubit.dart';
export 'submit/submit_cubit.dart'; export 'submit/submit_cubit.dart';
export 'tab/tab_cubit.dart';
export 'time_machine/time_machine_cubit.dart'; export 'time_machine/time_machine_cubit.dart';
export 'user/user_cubit.dart'; export 'user/user_cubit.dart';
export 'vote/vote_cubit.dart'; export 'vote/vote_cubit.dart';

View File

@ -73,7 +73,7 @@ class FavCubit extends Cubit<FavState> {
), ),
); );
final Item? item = await _storiesRepository.fetchItemBy(id: id); final Item? item = await _storiesRepository.fetchItem(id: id);
if (item == null) return; if (item == null) return;
@ -160,6 +160,13 @@ class FavCubit extends Cubit<FavState> {
}); });
} }
void removeAll() {
_preferenceRepository
..clearAllFavs(username: '')
..clearAllFavs(username: _authBloc.state.username);
emit(FavState.init());
}
void _onItemLoaded(Item item) { void _onItemLoaded(Item item) {
emit( emit(
state.copyWith( state.copyWith(

View File

@ -42,9 +42,9 @@ class FavState extends Equatable {
@override @override
List<Object?> get props => <Object?>[ List<Object?> get props => <Object?>[
favIds,
favItems,
status, status,
currentPage, currentPage,
favIds,
favItems,
]; ];
} }

View File

@ -0,0 +1,40 @@
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:hacki/config/locator.dart';
import 'package:hacki/repositories/repositories.dart';
part 'filter_state.dart';
class FilterCubit extends Cubit<FilterState> {
FilterCubit({PreferenceRepository? preferenceRepository})
: _preferenceRepository =
preferenceRepository ?? locator.get<PreferenceRepository>(),
super(FilterState.init()) {
init();
}
final PreferenceRepository _preferenceRepository;
void init() {
_preferenceRepository.filterKeywords.then(
(List<String> keywords) => emit(
state.copyWith(
keywords: keywords.toSet(),
),
),
);
}
void addKeyword(String keyword) {
final Set<String> updated = Set<String>.from(state.keywords)..add(keyword);
emit(state.copyWith(keywords: updated));
_preferenceRepository.updateFilterKeywords(updated.toList(growable: false));
}
void removeKeyword(String keyword) {
final Set<String> updated = Set<String>.from(state.keywords)
..remove(keyword);
emit(state.copyWith(keywords: updated));
_preferenceRepository.updateFilterKeywords(updated.toList(growable: false));
}
}

View File

@ -0,0 +1,20 @@
part of 'filter_cubit.dart';
class FilterState extends Equatable {
const FilterState({
required this.keywords,
});
FilterState.init() : keywords = <String>{};
final Set<String> keywords;
FilterState copyWith({Set<String>? keywords}) {
return FilterState(
keywords: keywords ?? this.keywords,
);
}
@override
List<Object?> get props => <Object?>[keywords];
}

View File

@ -28,7 +28,7 @@ class HistoryCubit extends Cubit<HistoryState> {
final String username = authState.username; final String username = authState.username;
_storiesRepository _storiesRepository
.fetchSubmitted(of: username) .fetchSubmitted(userId: username)
.then((List<int>? submittedIds) { .then((List<int>? submittedIds) {
emit( emit(
state.copyWith( state.copyWith(
@ -94,7 +94,7 @@ class HistoryCubit extends Cubit<HistoryState> {
); );
_storiesRepository _storiesRepository
.fetchSubmitted(of: username) .fetchSubmitted(userId: username)
.then((List<int>? submittedIds) { .then((List<int>? submittedIds) {
emit(state.copyWith(submittedIds: submittedIds)); emit(state.copyWith(submittedIds: submittedIds));
if (submittedIds != null) { if (submittedIds != null) {

View File

@ -42,9 +42,9 @@ class HistoryState extends Equatable {
@override @override
List<Object?> get props => <Object?>[ List<Object?> get props => <Object?>[
submittedIds,
submittedItems,
status, status,
currentPage, currentPage,
submittedIds,
submittedItems,
]; ];
} }

View File

@ -30,16 +30,16 @@ class NotificationCubit extends Cubit<NotificationState> {
_authBloc.stream.listen((AuthState authState) { _authBloc.stream.listen((AuthState authState) {
if (authState.isLoggedIn && authState.username != _username) { if (authState.isLoggedIn && authState.username != _username) {
// Get the user setting. // Get the user setting.
if (_preferenceCubit.state.showNotification) { if (_preferenceCubit.state.notificationEnabled) {
Future<void>.delayed(const Duration(seconds: 2), init); Future<void>.delayed(const Duration(seconds: 2), init);
} }
// Listen for setting changes in the future. // Listen for setting changes in the future.
_preferenceCubit.stream.listen((PreferenceState prefState) { _preferenceCubit.stream.listen((PreferenceState prefState) {
final bool isActive = _timer?.isActive ?? false; final bool isActive = _timer?.isActive ?? false;
if (prefState.showNotification && !isActive) { if (prefState.notificationEnabled && !isActive) {
init(); init();
} else if (!prefState.showNotification) { } else if (!prefState.notificationEnabled) {
_timer?.cancel(); _timer?.cancel();
} }
}); });
@ -81,7 +81,7 @@ class NotificationCubit extends Cubit<NotificationState> {
for (final int id in commentsToBeLoaded) { for (final int id in commentsToBeLoaded) {
Comment? comment = await _sembastRepository.getComment(id: id); Comment? comment = await _sembastRepository.getComment(id: id);
comment ??= await _storiesRepository.fetchCommentBy(id: id); comment ??= await _storiesRepository.fetchComment(id: id);
if (comment != null) { if (comment != null) {
emit( emit(
state.copyWith( state.copyWith(
@ -126,7 +126,8 @@ class NotificationCubit extends Cubit<NotificationState> {
} }
Future<void> refresh() async { Future<void> refresh() async {
if (_authBloc.state.isLoggedIn && _preferenceCubit.state.showNotification) { if (_authBloc.state.isLoggedIn &&
_preferenceCubit.state.notificationEnabled) {
emit( emit(
state.copyWith( state.copyWith(
status: NotificationStatus.loading, status: NotificationStatus.loading,
@ -158,7 +159,7 @@ class NotificationCubit extends Cubit<NotificationState> {
for (final int id in commentsToBeLoaded) { for (final int id in commentsToBeLoaded) {
Comment? comment = await _sembastRepository.getComment(id: id); Comment? comment = await _sembastRepository.getComment(id: id);
comment ??= await _storiesRepository.fetchCommentBy(id: id); comment ??= await _storiesRepository.fetchComment(id: id);
if (comment != null) { if (comment != null) {
emit(state.copyWith(comments: <Comment>[...state.comments, comment])); emit(state.copyWith(comments: <Comment>[...state.comments, comment]));
} }
@ -183,7 +184,7 @@ class NotificationCubit extends Cubit<NotificationState> {
Future<void> _fetchReplies() { Future<void> _fetchReplies() {
return _storiesRepository return _storiesRepository
.fetchSubmitted(of: _authBloc.state.username) .fetchSubmitted(userId: _authBloc.state.username)
.then((List<int>? submittedItems) async { .then((List<int>? submittedItems) async {
if (submittedItems != null) { if (submittedItems != null) {
final List<int> subscribedItems = submittedItems.sublist( final List<int> subscribedItems = submittedItems.sublist(
@ -192,7 +193,7 @@ class NotificationCubit extends Cubit<NotificationState> {
); );
for (final int id in subscribedItems) { for (final int id in subscribedItems) {
await _storiesRepository.fetchItemBy(id: id).then((Item? item) async { await _storiesRepository.fetchItem(id: id).then((Item? item) async {
final List<int> kids = item?.kids ?? <int>[]; final List<int> kids = item?.kids ?? <int>[];
final List<int> previousKids = final List<int> previousKids =
(await _sembastRepository.kids(of: id)) ?? <int>[]; (await _sembastRepository.kids(of: id)) ?? <int>[];
@ -215,7 +216,7 @@ class NotificationCubit extends Cubit<NotificationState> {
]..sort((int lhs, int rhs) => rhs.compareTo(lhs)), ]..sort((int lhs, int rhs) => rhs.compareTo(lhs)),
); );
await _storiesRepository await _storiesRepository
.fetchCommentBy(id: newCommentId) .fetchComment(id: newCommentId)
.then((Comment? comment) { .then((Comment? comment) {
if (comment != null && !comment.dead && !comment.deleted) { if (comment != null && !comment.dead && !comment.deleted) {
_sembastRepository _sembastRepository

View File

@ -77,11 +77,11 @@ class NotificationState extends Equatable {
@override @override
List<Object?> get props => <Object?>[ List<Object?> get props => <Object?>[
comments,
unreadCommentsIds,
allCommentsIds,
currentPage, currentPage,
offset, offset,
status, status,
comments,
unreadCommentsIds,
allCommentsIds,
]; ];
} }

View File

@ -33,7 +33,7 @@ class PollCubit extends Cubit<PollState> {
if (pollOptionsIds.isEmpty || refresh) { if (pollOptionsIds.isEmpty || refresh) {
final Story? updatedStory = final Story? updatedStory =
await _storiesRepository.fetchStoryBy(_story.id); await _storiesRepository.fetchStory(id: _story.id);
if (updatedStory != null) { if (updatedStory != null) {
pollOptionsIds = updatedStory.parts; pollOptionsIds = updatedStory.parts;

View File

@ -4,18 +4,23 @@ import 'package:hacki/config/locator.dart';
import 'package:hacki/extensions/extensions.dart'; import 'package:hacki/extensions/extensions.dart';
import 'package:hacki/models/models.dart'; import 'package:hacki/models/models.dart';
import 'package:hacki/repositories/repositories.dart'; import 'package:hacki/repositories/repositories.dart';
import 'package:logger/logger.dart';
part 'preference_state.dart'; part 'preference_state.dart';
class PreferenceCubit extends Cubit<PreferenceState> { class PreferenceCubit extends Cubit<PreferenceState> {
PreferenceCubit({PreferenceRepository? storageRepository}) PreferenceCubit({
: _preferenceRepository = PreferenceRepository? preferenceRepository,
storageRepository ?? locator.get<PreferenceRepository>(), Logger? logger,
}) : _preferenceRepository =
preferenceRepository ?? locator.get<PreferenceRepository>(),
_logger = logger ?? locator.get<Logger>(),
super(PreferenceState.init()) { super(PreferenceState.init()) {
init(); init();
} }
final PreferenceRepository _preferenceRepository; final PreferenceRepository _preferenceRepository;
final Logger _logger;
void init() { void init() {
for (final BooleanPreference p for (final BooleanPreference p
@ -32,6 +37,7 @@ class PreferenceCubit extends Cubit<PreferenceState> {
initPreference<int>(p).then<int?>((int? value) { initPreference<int>(p).then<int?>((int? value) {
final Preference<dynamic> updatedPreference = p.copyWith(val: value); final Preference<dynamic> updatedPreference = p.copyWith(val: value);
emit(state.copyWithPreference(updatedPreference)); emit(state.copyWithPreference(updatedPreference));
return null; return null;
}); });
} }
@ -50,17 +56,12 @@ class PreferenceCubit extends Cubit<PreferenceState> {
} }
} }
void toggle(BooleanPreference preference) {
final BooleanPreference updatedPreference =
preference.copyWith(val: !preference.val) as BooleanPreference;
emit(state.copyWithPreference(updatedPreference));
_preferenceRepository.setBool(preference.key, !preference.val);
}
void update<T>(Preference<T> preference, {required T to}) { void update<T>(Preference<T> preference, {required T to}) {
final T value = to; final T value = to;
final Preference<T> updatedPreference = preference.copyWith(val: value); final Preference<T> updatedPreference = preference.copyWith(val: value);
_logger.i('updating $preference to $value');
emit(state.copyWithPreference(updatedPreference)); emit(state.copyWithPreference(updatedPreference));
switch (T) { switch (T) {

View File

@ -48,25 +48,42 @@ class PreferenceState extends Equatable {
.val; .val;
} }
bool get showNotification => _isOn<NotificationModePreference>(); bool get notificationEnabled => _isOn<NotificationModePreference>();
bool get showComplexStoryTile => _isOn<DisplayModePreference>(); bool get complexStoryTileEnabled => _isOn<DisplayModePreference>();
bool get showWebFirst => _isOn<NavigationModePreference>(); bool get eyeCandyEnabled => _isOn<EyeCandyModePreference>();
bool get showEyeCandy => _isOn<EyeCandyModePreference>(); bool get trueDarkEnabled => _isOn<TrueDarkModePreference>();
bool get useTrueDark => _isOn<TrueDarkModePreference>(); bool get readerEnabled => _isOn<ReaderModePreference>();
bool get useReader => _isOn<ReaderModePreference>(); bool get markReadStoriesEnabled => _isOn<MarkReadStoriesModePreference>();
bool get markReadStories => _isOn<MarkReadStoriesModePreference>(); bool get metadataEnabled => _isOn<MetadataModePreference>();
bool get showMetadata => _isOn<MetadataModePreference>(); bool get urlEnabled => _isOn<StoryUrlModePreference>();
bool get showUrl => _isOn<StoryUrlModePreference>(); bool get tapAnywhereToCollapseEnabled => _isOn<CollapseModePreference>();
bool get tapAnywhereToCollapse => _isOn<CollapseModePreference>(); bool get swipeGestureEnabled => _isOn<SwipeGesturePreference>();
List<StoryType> get tabs {
final String result =
preferences.singleWhereType<TabOrderPreference>().val.toString();
final List<int> tabIndexes = List<int>.generate(
result.length,
(int index) => result.codeUnitAt(index) - 48,
);
final List<StoryType> tabs = tabIndexes
.map((int index) => StoryType.values.elementAt(index))
.toList();
if (tabs.length < StoryType.values.length) {
tabs.insert(0, StoryType.values.first);
}
return tabs;
}
FetchMode get fetchMode => FetchMode.values FetchMode get fetchMode => FetchMode.values
.elementAt(preferences.singleWhereType<FetchModePreference>().val); .elementAt(preferences.singleWhereType<FetchModePreference>().val);
@ -77,6 +94,9 @@ class PreferenceState extends Equatable {
FontSize get fontSize => FontSize.values FontSize get fontSize => FontSize.values
.elementAt(preferences.singleWhereType<FontSizePreference>().val); .elementAt(preferences.singleWhereType<FontSizePreference>().val);
Font get font =>
Font.values.elementAt(preferences.singleWhereType<FontPreference>().val);
@override @override
List<Object?> get props => <Object?>[ List<Object?> get props => <Object?>[
...preferences.map<dynamic>((Preference<dynamic> e) => e.val), ...preferences.map<dynamic>((Preference<dynamic> e) => e.val),

View File

@ -15,19 +15,19 @@ class SearchCubit extends Cubit<SearchState> {
final SearchRepository _searchRepository; final SearchRepository _searchRepository;
StreamSubscription<Story>? streamSubscription; StreamSubscription<Item>? streamSubscription;
void search(String query) { void search(String query) {
streamSubscription?.cancel(); streamSubscription?.cancel();
emit( emit(
state.copyWith( state.copyWith(
results: <Story>[], results: <Item>[],
status: SearchStatus.loading, status: SearchStatus.loading,
params: state.params.copyWith(query: query, page: 0), params: state.params.copyWith(query: query, page: 0),
), ),
); );
streamSubscription = streamSubscription =
_searchRepository.search(params: state.params).listen(_onStoryFetched) _searchRepository.search(params: state.params).listen(_onItemFetched)
..onDone(() { ..onDone(() {
emit(state.copyWith(status: SearchStatus.loaded)); emit(state.copyWith(status: SearchStatus.loaded));
}); });
@ -43,7 +43,7 @@ class SearchCubit extends Cubit<SearchState> {
), ),
); );
streamSubscription = streamSubscription =
_searchRepository.search(params: state.params).listen(_onStoryFetched) _searchRepository.search(params: state.params).listen(_onItemFetched)
..onDone(() { ..onDone(() {
emit(state.copyWith(status: SearchStatus.loaded)); emit(state.copyWith(status: SearchStatus.loaded));
}); });
@ -69,6 +69,8 @@ class SearchCubit extends Cubit<SearchState> {
} }
void removeFilter<T extends SearchFilter>() { void removeFilter<T extends SearchFilter>() {
if (state.params.contains<T>() == false) return;
emit( emit(
state.copyWith( state.copyWith(
params: state.params.copyWithFilterRemoved<T>(), params: state.params.copyWithFilterRemoved<T>(),
@ -78,6 +80,16 @@ class SearchCubit extends Cubit<SearchState> {
search(state.params.query); search(state.params.query);
} }
void onToggled(TypeTagFilter filter) {
if (state.params.contains<TypeTagFilter>() &&
state.params.get<TypeTagFilter>() == filter) {
removeFilter<TypeTagFilter>();
} else {
removeFilter<TypeTagFilter>();
addFilter<TypeTagFilter>(filter);
}
}
void onSortToggled() { void onSortToggled() {
emit( emit(
state.copyWith( state.copyWith(
@ -90,10 +102,44 @@ class SearchCubit extends Cubit<SearchState> {
search(state.params.query); search(state.params.query);
} }
void _onStoryFetched(Story story) { void onDateTimeRangeUpdated(DateTime start, DateTime end) {
final DateTime updatedStart = start.copyWith(
second: 0,
millisecond: 0,
microsecond: 0,
);
final DateTime updatedEnd = end.copyWith(
second: 0,
millisecond: 0,
microsecond: 0,
);
final DateTime? existingStart =
state.params.get<DateTimeRangeFilter>()?.startTime;
final DateTime? existingEnd =
state.params.get<DateTimeRangeFilter>()?.endTime;
if (existingStart == updatedStart && existingEnd == updatedEnd) return;
addFilter(
DateTimeRangeFilter(
startTime: updatedStart,
endTime: updatedEnd,
),
);
}
void onPostedByChanged(String? username) {
if (username == null) {
removeFilter<PostedByFilter>();
} else {
addFilter(PostedByFilter(author: username));
}
}
void _onItemFetched(Item item) {
emit( emit(
state.copyWith( state.copyWith(
results: List<Story>.from(state.results)..add(story), results: List<Item>.from(state.results)..add(item),
), ),
); );
} }

View File

@ -16,15 +16,15 @@ class SearchState extends Equatable {
SearchState.init() SearchState.init()
: status = SearchStatus.initial, : status = SearchStatus.initial,
results = <Story>[], results = <Item>[],
params = SearchParams.init(); params = SearchParams.init();
final List<Story> results; final List<Item> results;
final SearchStatus status; final SearchStatus status;
final SearchParams params; final SearchParams params;
SearchState copyWith({ SearchState copyWith({
List<Story>? results, List<Item>? results,
SearchStatus? status, SearchStatus? status,
SearchParams? params, SearchParams? params,
}) { }) {

View File

@ -0,0 +1,46 @@
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:hacki/config/locator.dart';
import 'package:hacki/cubits/cubits.dart';
import 'package:hacki/models/models.dart';
import 'package:logger/logger.dart';
part 'tab_state.dart';
class TabCubit extends Cubit<TabState> {
TabCubit({
required PreferenceCubit preferenceCubit,
Logger? logger,
}) : _preferenceCubit = preferenceCubit,
_logger = logger ?? locator.get<Logger>(),
super(TabState.init());
final PreferenceCubit _preferenceCubit;
final Logger _logger;
void init() {
final List<StoryType> tabs = _preferenceCubit.state.tabs;
_logger.i('updating tabs to $tabs');
emit(state.copyWith(tabs: tabs));
}
void update(int startIndex, int endIndex) {
_logger.d('updating ${state.tabs} by moving $startIndex to $endIndex');
final StoryType tab = state.tabs.elementAt(startIndex);
final List<StoryType> updatedTabs = List<StoryType>.from(state.tabs)
..insert(endIndex, tab)
..removeAt(startIndex < endIndex ? startIndex : startIndex + 1);
_logger.d(updatedTabs);
emit(state.copyWith(tabs: updatedTabs));
// Check to make sure there's no duplicate.
if (updatedTabs.toSet().length == StoryType.values.length) {
_preferenceCubit.update<int>(
TabOrderPreference(),
to: StoryType.convertToSettingsValue(updatedTabs),
);
}
}
}

View File

@ -0,0 +1,18 @@
part of 'tab_cubit.dart';
class TabState extends Equatable {
const TabState({required this.tabs});
TabState.init() : tabs = <StoryType>[];
final List<StoryType> tabs;
TabState copyWith({
List<StoryType>? tabs,
}) {
return TabState(tabs: tabs ?? this.tabs);
}
@override
List<Object?> get props => <Object?>[tabs];
}

View File

@ -20,20 +20,20 @@ class TimeMachineCubit extends Cubit<TimeMachineState> {
final CommentCache _commentCache; final CommentCache _commentCache;
Future<void> activateTimeMachine(Comment comment) async { Future<void> activateTimeMachine(Comment comment) async {
emit(state.copyWith(parents: <Comment>[])); emit(state.copyWith(ancestors: <Comment>[]));
final List<Comment> parents = <Comment>[]; final List<Comment> ancestors = <Comment>[];
Comment? parent = _commentCache.getComment(comment.parent); Comment? parent = _commentCache.getComment(comment.parent);
parent ??= await _sembastRepository.getCachedComment(id: comment.parent); parent ??= await _sembastRepository.getCachedComment(id: comment.parent);
while (parent != null) { while (parent != null) {
parents.insert(0, parent); ancestors.insert(0, parent);
final int parentId = parent.parent; final int parentId = parent.parent;
parent = _commentCache.getComment(parentId); parent = _commentCache.getComment(parentId);
parent ??= await _sembastRepository.getCachedComment(id: parentId); parent ??= await _sembastRepository.getCachedComment(id: parentId);
} }
emit(state.copyWith(parents: parents)); emit(state.copyWith(ancestors: ancestors));
} }
} }

View File

@ -1,18 +1,18 @@
part of 'time_machine_cubit.dart'; part of 'time_machine_cubit.dart';
class TimeMachineState extends Equatable { class TimeMachineState extends Equatable {
const TimeMachineState({required this.parents}); const TimeMachineState({required this.ancestors});
TimeMachineState.init() : parents = <Comment>[]; TimeMachineState.init() : ancestors = <Comment>[];
final List<Comment> parents; final List<Comment> ancestors;
TimeMachineState copyWith({ TimeMachineState copyWith({
List<Comment>? parents, List<Comment>? ancestors,
}) { }) {
return TimeMachineState(parents: parents ?? this.parents); return TimeMachineState(ancestors: ancestors ?? this.ancestors);
} }
@override @override
List<Object?> get props => <Object?>[parents]; List<Object?> get props => <Object?>[ancestors];
} }

View File

@ -16,8 +16,13 @@ class UserCubit extends Cubit<UserState> {
void init({required String userId}) { void init({required String userId}) {
emit(state.copyWith(status: UserStatus.loading)); emit(state.copyWith(status: UserStatus.loading));
_storiesRepository.fetchUserBy(userId: userId).then((User user) { _storiesRepository.fetchUser(id: userId).then((User? user) {
emit(state.copyWith(user: user, status: UserStatus.loaded)); emit(
state.copyWith(
user: user ?? User.emptyWithId(userId),
status: UserStatus.loaded,
),
);
}).onError((_, __) { }).onError((_, __) {
emit(state.copyWith(status: UserStatus.failure)); emit(state.copyWith(status: UserStatus.failure));
return; return;

View File

@ -2,6 +2,8 @@ import 'dart:math';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hacki/config/constants.dart';
import 'package:hacki/styles/styles.dart';
extension ContextExtension on BuildContext { extension ContextExtension on BuildContext {
T? tryRead<T>() { T? tryRead<T>() {
@ -12,6 +14,30 @@ extension ContextExtension on BuildContext {
} }
} }
void showSnackBar({
required String content,
VoidCallback? action,
String? label,
}) {
ScaffoldMessenger.of(this).showSnackBar(
SnackBar(
backgroundColor: Palette.deepOrange,
content: Text(content),
action: action != null && label != null
? SnackBarAction(
label: label,
onPressed: action,
textColor: Theme.of(this).textTheme.bodyLarge?.color,
)
: null,
),
);
}
void showErrorSnackBar() => showSnackBar(
content: Constants.errorMessage,
);
Rect? get rect { Rect? get rect {
final RenderBox? box = findRenderObject() as RenderBox?; final RenderBox? box = findRenderObject() as RenderBox?;
final Rect? rect = final Rect? rect =
@ -22,12 +48,12 @@ extension ContextExtension on BuildContext {
static double _screenWidth = 0; static double _screenWidth = 0;
static double _storyTileHeight = 0; static double _storyTileHeight = 0;
static int _storyTileMaxLines = 4; static int _storyTileMaxLines = 4;
static const double _screenWidthLowerBound = 430, static const double _screenWidthLowerBound = 430;
_screenWidthUpperBound = 850, static const double _screenWidthUpperBound = 850;
_picHeightLowerBound = 110, static const double _picHeightLowerBound = 110;
_picHeightUpperBound = 128, static const double _picHeightUpperBound = 128;
_smallPicHeight = 100, static const double _smallPicHeight = 100;
_picHeightFactor = 0.3; static const double _picHeightFactor = 0.3;
double get storyTileHeight { double get storyTileHeight {
final double screenWidth = final double screenWidth =

View File

@ -1,5 +1,5 @@
extension DateTimeExtension on DateTime { extension DateTimeExtension on DateTime {
String toReadableString() { String toTimeAgoString() {
final DateTime now = DateTime.now(); final DateTime now = DateTime.now();
final Duration diff = now.difference(this); final Duration diff = now.difference(this);
if (diff.inDays > 365) { if (diff.inDays > 365) {

View File

@ -1,7 +1,19 @@
import 'dart:developer' as dev; import 'package:hacki/config/locator.dart';
import 'package:logger/logger.dart';
extension ObjectExtension on Object { extension ObjectExtension on Object {
void log({String identifier = ''}) { void log([String identifier = '']) {
dev.log('$identifier ${toString()}', level: 2000); locator.get<Logger>().d('$identifier ${toString()}');
}
void logInfo({String identifier = ''}) {
locator.get<Logger>().i('$identifier ${toString()}');
}
void logError({
String identifier = '',
StackTrace? stackTrace,
}) {
locator.get<Logger>().e(identifier, this, stackTrace ?? StackTrace.current);
} }
} }

View File

@ -1,9 +1,16 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hacki/blocs/auth/auth_bloc.dart';
import 'package:hacki/cubits/cubits.dart'; import 'package:hacki/cubits/cubits.dart';
import 'package:hacki/extensions/extensions.dart';
import 'package:hacki/main.dart'; import 'package:hacki/main.dart';
import 'package:hacki/models/models.dart';
import 'package:hacki/screens/item/models/models.dart';
import 'package:hacki/screens/item/widgets/widgets.dart';
import 'package:hacki/screens/screens.dart' show ItemScreen, ItemScreenArgs; import 'package:hacki/screens/screens.dart' show ItemScreen, ItemScreenArgs;
import 'package:hacki/styles/styles.dart'; import 'package:hacki/styles/styles.dart';
import 'package:hacki/utils/utils.dart';
import 'package:share_plus/share_plus.dart';
extension StateExtension on State { extension StateExtension on State {
void showSnackBar({ void showSnackBar({
@ -11,22 +18,15 @@ extension StateExtension on State {
VoidCallback? action, VoidCallback? action,
String? label, String? label,
}) { }) {
ScaffoldMessenger.of(context).showSnackBar( context.showSnackBar(
SnackBar( content: content,
backgroundColor: Palette.deepOrange, action: action,
content: Text(content), label: label,
action: action != null && label != null
? SnackBarAction(
label: label,
onPressed: action,
textColor: Theme.of(context).textTheme.bodyText1?.color,
)
: null,
behavior: SnackBarBehavior.floating,
),
); );
} }
void showErrorSnackBar() => context.showErrorSnackBar();
Future<void>? goToItemScreen({ Future<void>? goToItemScreen({
required ItemScreenArgs args, required ItemScreenArgs args,
bool forceNewScreen = false, bool forceNewScreen = false,
@ -44,4 +44,192 @@ extension StateExtension on State {
return Future<void>.value(); return Future<void>.value();
} }
void onMoreTapped(Item item, Rect? rect) {
HapticFeedbackUtil.light();
if (item.dead || item.deleted) {
return;
}
final bool isBlocked =
context.read<BlocklistCubit>().state.blocklist.contains(item.by);
showModalBottomSheet<MenuAction>(
context: context,
isScrollControlled: true,
builder: (BuildContext context) {
return SafeArea(
child: MorePopupMenu(
item: item,
isBlocked: isBlocked,
onLoginTapped: onLoginTapped,
),
);
},
).then((MenuAction? action) {
if (action != null) {
switch (action) {
case MenuAction.upvote:
break;
case MenuAction.downvote:
break;
case MenuAction.fav:
onFavTapped(item);
break;
case MenuAction.share:
onShareTapped(item, rect);
break;
case MenuAction.flag:
onFlagTapped(item);
break;
case MenuAction.block:
onBlockTapped(item, isBlocked: isBlocked);
break;
case MenuAction.cancel:
break;
}
}
});
}
void onFavTapped(Item item) {
final FavCubit favCubit = context.read<FavCubit>();
final bool isFav = favCubit.state.favIds.contains(item.id);
if (isFav) {
favCubit.removeFav(item.id);
} else {
favCubit.addFav(item.id);
}
}
Future<void> onShareTapped(Item item, Rect? rect) async {
late final String? linkToShare;
if (item.url.isNotEmpty) {
linkToShare = await showModalBottomSheet<String>(
context: context,
builder: (BuildContext context) {
return SafeArea(
child: ColoredBox(
color: Theme.of(context).canvasColor,
child: Material(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ListTile(
onTap: () => Navigator.pop(context, item.url),
title: const Text('Link to article'),
),
ListTile(
onTap: () => Navigator.pop(
context,
'https://news.ycombinator.com/item?id=${item.id}',
),
title: const Text('Link to HN'),
),
],
),
),
),
);
},
);
} else {
linkToShare = 'https://news.ycombinator.com/item?id=${item.id}';
}
if (linkToShare != null) {
await Share.share(
linkToShare,
sharePositionOrigin: rect,
);
}
}
void onFlagTapped(Item item) {
showDialog<bool>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Flag this comment?'),
content: Text(
'Flag this comment posted by ${item.by}?',
style: const TextStyle(
color: Palette.grey,
),
),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text(
'Cancel',
),
),
TextButton(
onPressed: () => Navigator.pop(context, true),
child: const Text(
'Yes',
),
),
],
);
},
).then((bool? yesTapped) {
if (yesTapped ?? false) {
context.read<AuthBloc>().add(AuthFlag(item: item));
showSnackBar(content: 'Comment flagged!');
}
});
}
void onBlockTapped(Item item, {required bool isBlocked}) {
showDialog<bool>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('${isBlocked ? 'Unblock' : 'Block'} this user?'),
content: Text(
'Do you want to ${isBlocked ? 'unblock' : 'block'} ${item.by}'
' and ${isBlocked ? 'display' : 'hide'} '
'comments posted by this user?',
style: const TextStyle(
color: Palette.grey,
),
),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text(
'Cancel',
),
),
TextButton(
onPressed: () => Navigator.pop(context, true),
child: const Text(
'Yes',
),
),
],
);
},
).then((bool? yesTapped) {
if (yesTapped ?? false) {
if (isBlocked) {
context.read<BlocklistCubit>().removeFromBlocklist(item.by);
} else {
context.read<BlocklistCubit>().addToBlocklist(item.by);
}
showSnackBar(content: 'User ${isBlocked ? 'unblocked' : 'blocked'}!');
}
});
}
void onLoginTapped() {
showDialog<void>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return const LoginDialog();
},
);
}
} }

View File

@ -1,4 +1,8 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:hacki/config/constants.dart';
import 'package:hacki/models/models.dart';
import 'package:hacki/screens/widgets/custom_linkify/custom_linkify.dart';
import 'package:hacki/utils/utils.dart';
extension WidgetModifier on Widget { extension WidgetModifier on Widget {
Widget padded([EdgeInsetsGeometry value = const EdgeInsets.all(12)]) { Widget padded([EdgeInsetsGeometry value = const EdgeInsets.all(12)]) {
@ -7,4 +11,62 @@ extension WidgetModifier on Widget {
child: this, child: this,
); );
} }
Widget contextMenuBuilder(
BuildContext context,
EditableTextState editableTextState, {
required Item item,
}) {
final int start = editableTextState.textEditingValue.selection.base.offset;
final int end = editableTextState.textEditingValue.selection.end;
final List<ContextMenuButtonItem> items = <ContextMenuButtonItem>[
...editableTextState.contextMenuButtonItems,
];
if (start != -1 && end != -1) {
String selectedText = item.text.substring(start, end);
if (item is Buildable) {
final Iterable<EmphasisElement> emphasisElements =
(item as Buildable).elements.whereType<EmphasisElement>();
int count = 1;
while (selectedText.contains(' ') && count <= emphasisElements.length) {
final int s = (start + count * 2).clamp(0, item.text.length);
final int e = (end + count * 2).clamp(0, item.text.length);
selectedText = item.text.substring(s, e);
count++;
}
count = 1;
while (selectedText.contains(' ') && count <= emphasisElements.length) {
final int s = (start - count * 2).clamp(0, item.text.length);
final int e = (end - count * 2).clamp(0, item.text.length);
selectedText = item.text.substring(s, e);
count++;
}
}
items.addAll(<ContextMenuButtonItem>[
ContextMenuButtonItem(
onPressed: () => LinkUtil.launch(
'''${Constants.wikipediaLink}$selectedText''',
),
label: 'Wikipedia',
),
ContextMenuButtonItem(
onPressed: () => LinkUtil.launch(
'''${Constants.wiktionaryLink}$selectedText''',
),
label: 'Wiktionary',
),
]);
}
return AdaptiveTextSelectionToolbar.buttonItems(
anchors: editableTextState.contextMenuAnchors,
buttonItems: items,
);
}
} }

View File

@ -2,6 +2,8 @@ import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'package:adaptive_theme/adaptive_theme.dart'; import 'package:adaptive_theme/adaptive_theme.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:equatable/equatable.dart';
import 'package:feature_discovery/feature_discovery.dart'; import 'package:feature_discovery/feature_discovery.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@ -16,11 +18,12 @@ import 'package:hacki/config/locator.dart';
import 'package:hacki/cubits/cubits.dart'; import 'package:hacki/cubits/cubits.dart';
import 'package:hacki/models/models.dart'; import 'package:hacki/models/models.dart';
import 'package:hacki/screens/screens.dart'; import 'package:hacki/screens/screens.dart';
import 'package:hacki/services/custom_bloc_observer.dart';
import 'package:hacki/services/fetcher.dart'; import 'package:hacki/services/fetcher.dart';
import 'package:hacki/styles/styles.dart'; import 'package:hacki/styles/styles.dart';
import 'package:hacki/utils/theme_util.dart';
import 'package:hive/hive.dart'; import 'package:hive/hive.dart';
import 'package:hydrated_bloc/hydrated_bloc.dart'; import 'package:hydrated_bloc/hydrated_bloc.dart';
import 'package:logger/logger.dart';
import 'package:path_provider/path_provider.dart'; import 'package:path_provider/path_provider.dart';
import 'package:rxdart/rxdart.dart' show BehaviorSubject; import 'package:rxdart/rxdart.dart' show BehaviorSubject;
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
@ -36,11 +39,30 @@ final BehaviorSubject<String?> siriSuggestionSubject =
late final bool isTesting; late final bool isTesting;
void notificationReceiver(NotificationResponse details) =>
selectNotificationSubject.add(details.payload);
Future<void> main({bool testing = false}) async { Future<void> main({bool testing = false}) async {
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
isTesting = testing; isTesting = testing;
final Directory tempDir = await getTemporaryDirectory();
final String tempPath = tempDir.path;
Hive.init(tempPath);
await setUpLocator();
EquatableConfig.stringify = true;
FlutterError.onError = (FlutterErrorDetails details) {
locator.get<Logger>().e(
details.summary,
details.exceptionAsString(),
details.stack,
);
};
final HydratedStorage storage = await HydratedStorage.build( final HydratedStorage storage = await HydratedStorage.build(
storageDirectory: kIsWeb storageDirectory: kIsWeb
? HydratedStorage.webStorageDirectory ? HydratedStorage.webStorageDirectory
@ -58,8 +80,8 @@ Future<void> main({bool testing = false}) async {
FlutterLocalNotificationsPlugin(); FlutterLocalNotificationsPlugin();
const AndroidInitializationSettings initializationSettingsAndroid = const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('@mipmap/ic_launcher'); AndroidInitializationSettings('@mipmap/ic_launcher');
const IOSInitializationSettings initializationSettingsIOS = const DarwinInitializationSettings initializationSettingsIOS =
IOSInitializationSettings(); DarwinInitializationSettings();
const InitializationSettings initializationSettings = const InitializationSettings initializationSettings =
InitializationSettings( InitializationSettings(
android: initializationSettingsAndroid, android: initializationSettingsAndroid,
@ -67,7 +89,8 @@ Future<void> main({bool testing = false}) async {
); );
await flutterLocalNotificationsPlugin.initialize( await flutterLocalNotificationsPlugin.initialize(
initializationSettings, initializationSettings,
onSelectNotification: selectNotificationSubject.add, onDidReceiveBackgroundNotificationResponse: notificationReceiver,
onDidReceiveNotificationResponse: notificationReceiver,
); );
await flutterLocalNotificationsPlugin await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation< .resolvePlatformSpecificImplementation<
@ -88,13 +111,19 @@ Future<void> main({bool testing = false}) async {
}, },
); );
} else if (Platform.isAndroid) { } else if (Platform.isAndroid) {
SystemChrome.setSystemUIOverlayStyle( final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
const SystemUiOverlayStyle( final AndroidDeviceInfo androidInfo = await deviceInfoPlugin.androidInfo;
statusBarColor: Palette.transparent, final int sdk = androidInfo.version.sdkInt;
systemNavigationBarColor: Palette.transparent,
systemNavigationBarDividerColor: Palette.transparent, if (sdk > 28) {
), SystemChrome.setSystemUIOverlayStyle(
); const SystemUiOverlayStyle(
statusBarColor: Palette.transparent,
systemNavigationBarColor: Palette.transparent,
systemNavigationBarDividerColor: Palette.transparent,
),
);
}
await SystemChrome.setEnabledSystemUIMode( await SystemChrome.setEnabledSystemUIMode(
SystemUiMode.edgeToEdge, SystemUiMode.edgeToEdge,
@ -102,24 +131,24 @@ Future<void> main({bool testing = false}) async {
); );
} }
final Directory tempDir = await getTemporaryDirectory();
final String tempPath = tempDir.path;
Hive.init(tempPath);
await setUpLocator();
final AdaptiveThemeMode? savedThemeMode = await AdaptiveTheme.getThemeMode(); final AdaptiveThemeMode? savedThemeMode = await AdaptiveTheme.getThemeMode();
final SharedPreferences prefs = await SharedPreferences.getInstance(); final SharedPreferences prefs = await SharedPreferences.getInstance();
final bool trueDarkMode = final bool trueDarkMode =
prefs.getBool(const TrueDarkModePreference().key) ?? false; prefs.getBool(const TrueDarkModePreference().key) ?? false;
final Font font = Font.values.elementAt(
prefs.getInt(FontPreference().key) ?? Font.roboto.index,
);
//Uncomment this line to log events from bloc/cubit.
//Bloc.observer = CustomBlocObserver();
Bloc.observer = CustomBlocObserver();
HydratedBloc.storage = storage; HydratedBloc.storage = storage;
runApp( runApp(
HackiApp( HackiApp(
savedThemeMode: savedThemeMode, savedThemeMode: savedThemeMode,
trueDarkMode: trueDarkMode, trueDarkMode: trueDarkMode,
font: font,
), ),
); );
} }
@ -129,9 +158,11 @@ class HackiApp extends StatelessWidget {
super.key, super.key,
this.savedThemeMode, this.savedThemeMode,
required this.trueDarkMode, required this.trueDarkMode,
required this.font,
}); });
final AdaptiveThemeMode? savedThemeMode; final AdaptiveThemeMode? savedThemeMode;
final Font font;
final bool trueDarkMode; final bool trueDarkMode;
static final GlobalKey<NavigatorState> navigatorKey = static final GlobalKey<NavigatorState> navigatorKey =
@ -145,9 +176,14 @@ class HackiApp extends StatelessWidget {
lazy: false, lazy: false,
create: (BuildContext context) => PreferenceCubit(), create: (BuildContext context) => PreferenceCubit(),
), ),
BlocProvider<FilterCubit>(
lazy: false,
create: (BuildContext context) => FilterCubit(),
),
BlocProvider<StoriesBloc>( BlocProvider<StoriesBloc>(
create: (BuildContext context) => StoriesBloc( create: (BuildContext context) => StoriesBloc(
preferenceCubit: context.read<PreferenceCubit>(), preferenceCubit: context.read<PreferenceCubit>(),
filterCubit: context.read<FilterCubit>(),
), ),
), ),
BlocProvider<AuthBloc>( BlocProvider<AuthBloc>(
@ -201,15 +237,22 @@ class HackiApp extends StatelessWidget {
lazy: false, lazy: false,
create: (BuildContext context) => EditCubit(), create: (BuildContext context) => EditCubit(),
), ),
BlocProvider<TabCubit>(
create: (BuildContext context) => TabCubit(
preferenceCubit: context.read<PreferenceCubit>(),
)..init(),
)
], ],
child: AdaptiveTheme( child: AdaptiveTheme(
light: ThemeData( light: ThemeData(
primarySwatch: Palette.orange, primarySwatch: Palette.orange,
fontFamily: font.name,
), ),
dark: ThemeData( dark: ThemeData(
brightness: Brightness.dark, brightness: Brightness.dark,
primarySwatch: Palette.orange, primarySwatch: Palette.orange,
canvasColor: trueDarkMode ? Palette.black : null, canvasColor: trueDarkMode ? Palette.black : null,
fontFamily: font.name,
), ),
initial: savedThemeMode ?? AdaptiveThemeMode.system, initial: savedThemeMode ?? AdaptiveThemeMode.system,
builder: (ThemeData theme, ThemeData darkTheme) { builder: (ThemeData theme, ThemeData darkTheme) {
@ -217,6 +260,7 @@ class HackiApp extends StatelessWidget {
brightness: Brightness.dark, brightness: Brightness.dark,
primarySwatch: Palette.orange, primarySwatch: Palette.orange,
canvasColor: Palette.black, canvasColor: Palette.black,
fontFamily: font.name,
); );
return FutureBuilder<AdaptiveThemeMode?>( return FutureBuilder<AdaptiveThemeMode?>(
future: AdaptiveTheme.getThemeMode(), future: AdaptiveTheme.getThemeMode(),
@ -225,12 +269,16 @@ class HackiApp extends StatelessWidget {
AsyncSnapshot<AdaptiveThemeMode?> snapshot, AsyncSnapshot<AdaptiveThemeMode?> snapshot,
) { ) {
final AdaptiveThemeMode? mode = snapshot.data; final AdaptiveThemeMode? mode = snapshot.data;
ThemeUtil.updateAndroidStatusBarSetting(
Theme.of(context).brightness,
mode,
);
return BlocBuilder<PreferenceCubit, PreferenceState>( return BlocBuilder<PreferenceCubit, PreferenceState>(
buildWhen: buildWhen:
(PreferenceState previous, PreferenceState current) => (PreferenceState previous, PreferenceState current) =>
previous.useTrueDark != current.useTrueDark, previous.trueDarkEnabled != current.trueDarkEnabled,
builder: (BuildContext context, PreferenceState prefState) { builder: (BuildContext context, PreferenceState prefState) {
final bool useTrueDark = prefState.useTrueDark && final bool useTrueDark = prefState.trueDarkEnabled &&
(mode == AdaptiveThemeMode.dark || (mode == AdaptiveThemeMode.dark ||
(mode == AdaptiveThemeMode.system && (mode == AdaptiveThemeMode.system &&
SchedulerBinding SchedulerBinding

View File

@ -1,100 +0,0 @@
import 'dart:convert';
import 'package:hacki/models/item.dart';
class Comment extends Item {
Comment({
required super.id,
required super.time,
required super.parent,
required super.score,
required super.by,
required super.text,
required super.kids,
required super.dead,
required super.deleted,
required this.level,
}) : super(
descendants: 0,
parts: <int>[],
title: '',
url: '',
type: '',
);
Comment.fromJson(Map<String, dynamic> json, {this.level = 0})
: super(
id: json['id'] as int? ?? 0,
time: json['time'] as int? ?? 0,
by: json['by'] as String? ?? '',
text: json['text'] as String? ?? '',
kids: (json['kids'] as List<dynamic>?)?.cast<int>() ?? <int>[],
parent: json['parent'] as int? ?? 0,
deleted: json['deleted'] as bool? ?? false,
score: json['score'] as int? ?? 0,
descendants: 0,
dead: json['dead'] as bool? ?? false,
parts: <int>[],
title: '',
url: '',
type: '',
);
final int level;
String get metadata => '''by $by $postedDate''';
Comment copyWith({int? level}) {
return Comment(
id: id,
time: time,
parent: parent,
score: score,
by: by,
text: text,
kids: kids,
dead: dead,
deleted: deleted,
level: level ?? this.level,
);
}
@override
Map<String, dynamic> toJson() => <String, dynamic>{
'id': id,
'time': time,
'by': by,
'text': text,
'kids': kids,
'parent': parent,
'deleted': deleted,
'dead': dead,
'score': score,
'level': level,
};
@override
String toString() {
final String prettyString =
const JsonEncoder.withIndent(' ').convert(this);
return 'Comment $prettyString';
}
@override
List<Object?> get props => <Object?>[
id,
score,
descendants,
time,
by,
title,
url,
kids,
dead,
parts,
deleted,
parent,
text,
type,
];
}

28
lib/models/font.dart Normal file
View File

@ -0,0 +1,28 @@
enum Font {
roboto('Roboto'),
robotoSlab('Roboto Slab', isSerif: true),
ubuntu('Ubuntu'),
ubuntuMono('Ubuntu Mono'),
notoSerif('Noto Serif', isSerif: true);
const Font(this.uiLabel, {this.isSerif = false});
final String uiLabel;
final bool isSerif;
static Font fromString(String? val) {
switch (val) {
case 'robotoSlab':
return Font.robotoSlab;
case 'ubuntu':
return Font.ubuntu;
case 'ubuntuMono':
return Font.ubuntuMono;
case 'notoSerif':
return Font.notoSerif;
case 'roboto':
default:
return Font.roboto;
}
}
}

View File

@ -0,0 +1,5 @@
import 'package:hacki/screens/widgets/custom_linkify/custom_linkify.dart';
mixin Buildable {
List<LinkifyElement> get elements;
}

View File

@ -1,8 +1,10 @@
import 'package:flutter_linkify/flutter_linkify.dart'; import 'package:hacki/models/item/buildable.dart';
import 'package:hacki/models/comment.dart'; import 'package:hacki/models/item/comment.dart';
import 'package:hacki/models/models.dart'; import 'package:linkify/linkify.dart';
class BuildableComment extends Comment { /// [BuildableComment] is a subtype of [Comment] which stores
/// the corresponding [LinkifyElement] for faster widget building.
class BuildableComment extends Comment with Buildable {
BuildableComment({ BuildableComment({
required super.id, required super.id,
required super.time, required super.time,
@ -13,6 +15,7 @@ class BuildableComment extends Comment {
required super.kids, required super.kids,
required super.dead, required super.dead,
required super.deleted, required super.deleted,
required super.hidden,
required super.level, required super.level,
required this.elements, required this.elements,
}); });
@ -29,7 +32,9 @@ class BuildableComment extends Comment {
dead: comment.dead, dead: comment.dead,
deleted: comment.deleted, deleted: comment.deleted,
level: comment.level, level: comment.level,
hidden: comment.hidden,
); );
@override
final List<LinkifyElement> elements; final List<LinkifyElement> elements;
} }

View File

@ -0,0 +1,48 @@
import 'package:hacki/models/item/buildable.dart';
import 'package:hacki/models/item/story.dart';
import 'package:linkify/linkify.dart';
/// [BuildableStory] is a subtype of [Story] which stores
/// the corresponding [LinkifyElement] for faster widget building.
class BuildableStory extends Story with Buildable {
const BuildableStory({
required super.id,
required super.time,
required super.score,
required super.by,
required super.text,
required super.kids,
required super.descendants,
required super.title,
required super.type,
required super.url,
required super.parts,
required super.hidden,
required this.elements,
});
BuildableStory.fromStory(Story story, {required this.elements})
: super(
id: story.id,
time: story.time,
score: story.score,
by: story.by,
text: story.text,
kids: story.kids,
descendants: story.descendants,
title: story.title,
type: story.type,
url: story.url,
parts: story.parts,
hidden: story.hidden,
);
BuildableStory.fromTitleOnlyStory(Story story)
: this.fromStory(
story,
elements: const <LinkifyElement>[],
);
@override
final List<LinkifyElement> elements;
}

View File

@ -0,0 +1,53 @@
import 'package:hacki/models/item/item.dart';
class Comment extends Item {
Comment({
required super.id,
required super.time,
required super.parent,
required super.score,
required super.by,
required super.text,
required super.kids,
required super.dead,
required super.deleted,
required super.hidden,
required this.level,
}) : super(
descendants: 0,
parts: <int>[],
title: '',
url: '',
type: '',
);
Comment.fromJson(super.json, {this.level = 0}) : super.fromJson();
final int level;
String get metadata => '''by $by $timeAgo''';
bool get isRoot => level == 0;
Comment copyWith({
int? level,
bool? hidden,
}) {
return Comment(
id: id,
time: time,
parent: parent,
score: score,
by: by,
text: text,
kids: kids,
dead: dead,
deleted: deleted,
hidden: hidden ?? this.hidden,
level: level ?? this.level,
);
}
@override
bool? get stringify => false;
}

View File

@ -1,6 +1,17 @@
import 'package:equatable/equatable.dart'; import 'package:equatable/equatable.dart';
import 'package:hacki/extensions/date_time_extension.dart'; import 'package:hacki/extensions/date_time_extension.dart';
import 'package:hacki/models/item/comment.dart';
import 'package:hacki/models/item/poll_option.dart';
import 'package:hacki/models/item/story.dart';
export 'buildable.dart';
export 'buildable_comment.dart';
export 'buildable_story.dart';
export 'comment.dart';
export 'poll_option.dart';
export 'story.dart';
/// [Item] is the base type of [Story], [Comment] and [PollOption].
class Item extends Equatable { class Item extends Equatable {
const Item({ const Item({
required this.id, required this.id,
@ -17,6 +28,7 @@ class Item extends Equatable {
required this.type, required this.type,
required this.parts, required this.parts,
required this.descendants, required this.descendants,
required this.hidden,
}); });
Item.empty() Item.empty()
@ -28,9 +40,10 @@ class Item extends Equatable {
title = '', title = '',
url = '', url = '',
kids = <int>[], kids = <int>[],
dead = false,
parts = <int>[], parts = <int>[],
dead = false,
deleted = false, deleted = false,
hidden = false,
parent = 0, parent = 0,
text = '', text = '',
type = ''; type = '';
@ -44,12 +57,13 @@ class Item extends Equatable {
title = json['title'] as String? ?? '', title = json['title'] as String? ?? '',
text = json['text'] as String? ?? '', text = json['text'] as String? ?? '',
url = json['url'] as String? ?? '', url = json['url'] as String? ?? '',
kids = <int>[], kids = (json['kids'] as List<dynamic>?)?.cast<int>() ?? <int>[],
dead = json['dead'] as bool? ?? false, dead = json['dead'] as bool? ?? false,
deleted = json['deleted'] as bool? ?? false, deleted = json['deleted'] as bool? ?? false,
parent = json['parent'] as int? ?? 0, parent = json['parent'] as int? ?? 0,
parts = <int>[], parts = (json['parts'] as List<dynamic>?)?.cast<int>() ?? <int>[],
type = json['type'] as String? ?? ''; type = json['type'] as String? ?? '',
hidden = json['hidden'] as bool? ?? false;
final int id; final int id;
final int time; final int time;
@ -62,6 +76,11 @@ class Item extends Equatable {
final bool deleted; final bool deleted;
final bool dead; final bool dead;
/// Whether or not the item should be hidden.
/// true if any of filter keywords set by user presents in [text]
/// or [title].
final bool hidden;
final String by; final String by;
final String text; final String text;
final String url; final String url;
@ -71,8 +90,8 @@ class Item extends Equatable {
final List<int> kids; final List<int> kids;
final List<int> parts; final List<int> parts;
String get postedDate => String get timeAgo =>
DateTime.fromMillisecondsSinceEpoch(time * 1000).toReadableString(); DateTime.fromMillisecondsSinceEpoch(time * 1000).toTimeAgoString();
bool get isPoll => type == 'poll'; bool get isPoll => type == 'poll';
@ -97,6 +116,7 @@ class Item extends Equatable {
'deleted': deleted, 'deleted': deleted,
'type': type, 'type': type,
'parts': parts, 'parts': parts,
'parent': parent,
}; };
} }
@ -116,5 +136,6 @@ class Item extends Equatable {
type, type,
parts, parts,
descendants, descendants,
hidden,
]; ];
} }

View File

@ -0,0 +1,67 @@
import 'dart:convert';
import 'package:hacki/models/item/item.dart';
class PollOption extends Item {
const PollOption({
required super.id,
required super.score,
required super.time,
required super.parent,
required super.by,
required super.title,
required super.text,
required super.type,
required super.url,
required super.kids,
required super.parts,
required this.ratio,
}) : super(
descendants: 0,
dead: false,
deleted: false,
hidden: false,
);
PollOption.empty()
: ratio = 0,
super.empty();
PollOption.fromJson(super.json)
: ratio = 0,
super.fromJson();
final double ratio;
PollOption copyWith({double? ratio}) {
return PollOption(
id: id,
score: score,
time: time,
parent: parent,
by: by,
title: title,
text: text,
type: type,
url: url,
kids: kids,
parts: parts,
ratio: ratio ?? this.ratio,
);
}
@override
Map<String, dynamic> toJson() {
return <String, dynamic>{
...super.toJson(),
'ratio': ratio,
};
}
@override
String toString() {
final String prettyString =
const JsonEncoder.withIndent(' ').convert(this);
return 'PollOption $prettyString';
}
}

View File

@ -0,0 +1,81 @@
import 'package:hacki/config/constants.dart';
import 'package:hacki/models/item/item.dart';
class Story extends Item {
const Story({
required super.descendants,
required super.id,
required super.score,
required super.time,
required super.by,
required super.title,
required super.type,
required super.url,
required super.text,
required super.kids,
required super.parts,
required super.hidden,
}) : super(
dead: false,
deleted: false,
parent: 0,
);
Story.empty() : super.empty();
Story.placeholder()
: super(
id: 0,
score: 0,
descendants: 0,
time: 1171872000,
by: 'Y Combinator',
title: 'Hacker News Guidelines',
url: Constants.guidelineLink,
kids: <int>[],
dead: false,
parts: <int>[],
deleted: false,
parent: 0,
text: '',
type: '',
hidden: false,
);
Story.fromJson(super.json) : super.fromJson();
Story copyWith({bool? hidden}) {
return Story(
descendants: descendants,
id: id,
score: score,
time: time,
by: by,
title: title,
type: type,
url: url,
text: text,
kids: kids,
parts: parts,
hidden: hidden ?? this.hidden,
);
}
String get metadata =>
'''$score point${score > 1 ? 's' : ''} by $by $timeAgo | $descendants comment${descendants > 1 ? 's' : ''}''';
String get screenReaderLabel =>
'''$title, at $readableUrl, by $by $timeAgo. This story has $score point${score > 1 ? 's' : ''} and $descendants comment${descendants > 1 ? 's' : ''}''';
String get simpleMetadata =>
'''$score point${score > 1 ? 's' : ''} $descendants comment${descendants > 1 ? 's' : ''} $timeAgo''';
String get readableUrl {
final Uri url = Uri.parse(this.url);
final String authority = url.authority.replaceFirst('www.', '');
return authority;
}
@override
String toString() => 'Story $id';
}

View File

@ -1,12 +1,10 @@
export 'buildable_comment.dart';
export 'comment.dart';
export 'comments_order.dart'; export 'comments_order.dart';
export 'fetch_mode.dart'; export 'fetch_mode.dart';
export 'font.dart';
export 'font_size.dart'; export 'font_size.dart';
export 'item.dart'; export 'item/item.dart';
export 'poll_option.dart';
export 'post_data.dart'; export 'post_data.dart';
export 'preference.dart'; export 'preference.dart';
export 'search_params.dart'; export 'search_params.dart';
export 'story.dart'; export 'story_type.dart';
export 'user.dart'; export 'user.dart';

Some files were not shown because too many files have changed in this diff Show More